Android Cocos2d旋转动画 图片围绕一个圆心做圆运动

  1. public class CCRoundBy extends CCIntervalAction {  
  2.     boolean turn;// Forward or Reverse round  
  3.     float startAngle;// default  
  4.     float radius;// Round circle radius  
  5.     CGPoint center;// Round circle center point  
  6.   
  7.     public boolean isTurn() {  
  8.         return turn;  
  9.     }  
  10.   
  11.     public void setTurn(boolean turn) {  
  12.         this.turn = turn;  
  13.     }  
  14.   
  15.     public float getStartAngle() {  
  16.         return startAngle;  
  17.     }  
  18.   
  19.     public void setStartAngle(float startAngle) {  
  20.         this.startAngle = startAngle;  
  21.     }  
  22.   
  23.     public float getRadius() {  
  24.         return radius;  
  25.     }  
  26.   
  27.     public void setRadius(float radius) {  
  28.         this.radius = radius;  
  29.     }  
  30.   
  31.     public CGPoint getCenter() {  
  32.         return center;  
  33.     }  
  34.   
  35.     public void setCenter(CGPoint center) {  
  36.         this.center = center;  
  37.     }  
  38.   
  39.     /** creates the action */  
  40.     public static CCRoundBy action(float duration,boolean a,CGPoint point, float r) {  
  41.         return new CCRoundBy(duration, a, point, r);  
  42.     }  
  43.   
  44.     /** initializes the action */  
  45.     protected CCRoundBy(float duration,boolean a,CGPoint point, float r) {  
  46.         super(duration);  
  47.         turn = a;  
  48.         radius = r;  
  49.         center = point;  
  50.     }  
  51.   
  52.     @Override  
  53.     public void start(CCNode aTarget) {  
  54.         super.start(aTarget);  
  55.   
  56.         startAngle = aTarget.getRotation();  
  57.         if (turn) {  
  58.             ((CCNode)aTarget).setPosition(CGPoint.ccpAdd(center, CGPoint.ccp(-radius, 0)));  
  59.         }  
  60.         else {  
  61.             ((CCNode)aTarget).setPosition(CGPoint.ccpAdd(center, CGPoint.ccp(radius, 0)));  
  62.         }  
  63.     }  
  64.   
  65.     @Override  
  66.     public void update(float t) {  
  67.         // XXX: shall I add % 360  
  68.         float rotate =  (startAngle + 360.0f * t );  
  69.         if (turn) {  
  70.             rotate *= -1;  
  71.         }  
  72.         target.setRotation(rotate);  
  73.         float fradian = (float) (rotate * Math.PI / 180.0f);  
  74.         CGPoint pos = CGPoint.ccp(center.x + radius * MathUtils.sin(fradian),  
  75.                           center.y + radius * MathUtils.cos(fradian));  
  76.         target.setPosition(pos);  
  77.     }  
  78.     @Override  
  79.     public CCIntervalAction reverse() {  
  80.         boolean result = !turn;  
  81.         return action(duration, result, center, radius);  
  82.     }  
  83. }  
posted @ 2013-04-28 16:47  天边的星星  阅读(1544)  评论(0编辑  收藏  举报