欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Cocos2d实现:一个图片围绕一个圆心做圆运动 androidcocos2d游戏 

程序员文章站 2024-03-14 15:51:40
...

转载自:http://www.longhaiqiang.com/android-cocos2d实现:一个图片围绕一个圆心做圆运动/

实现效果:

Android Cocos2d实现:一个图片围绕一个圆心做圆运动
            
    
    
        androidcocos2d游戏 

 

public class CCRoundBy extends CCIntervalAction {
    boolean turn;// Forward or Reverse round
    float startAngle;// default
    float radius;// Round circle radius
    CGPoint center;// Round circle center point

    public boolean isTurn() {
		return turn;
	}

	public void setTurn(boolean turn) {
		this.turn = turn;
	}

	public float getStartAngle() {
		return startAngle;
	}

	public void setStartAngle(float startAngle) {
		this.startAngle = startAngle;
	}

	public float getRadius() {
		return radius;
	}

	public void setRadius(float radius) {
		this.radius = radius;
	}

	public CGPoint getCenter() {
		return center;
	}

	public void setCenter(CGPoint center) {
		this.center = center;
	}

	/** creates the action */
    public static CCRoundBy action(float duration,boolean a,CGPoint point, float r) {
        return new CCRoundBy(duration, a, point, r);
    }

    /** initializes the action */
    protected CCRoundBy(float duration,boolean a,CGPoint point, float r) {
        super(duration);
        turn = a;
        radius = r;
        center = point;
    }

    @Override
    public void start(CCNode aTarget) {
        super.start(aTarget);

        startAngle = aTarget.getRotation();
        if (turn) {
            ((CCNode)aTarget).setPosition(CGPoint.ccpAdd(center, CGPoint.ccp(-radius, 0)));
        }
        else {
            ((CCNode)aTarget).setPosition(CGPoint.ccpAdd(center, CGPoint.ccp(radius, 0)));
        }
    }

    @Override
    public void update(float t) {
        // XXX: shall I add % 360
        float rotate =  (startAngle + 360.0f * t );
        if (turn) {
            rotate *= -1;
        }
        target.setRotation(rotate);
        float fradian = (float) (rotate * Math.PI / 180.0f);
        CGPoint pos = CGPoint.ccp(center.x + radius * MathUtils.sin(fradian),
                          center.y + radius * MathUtils.cos(fradian));
        target.setPosition(pos);
    }
    @Override
    public CCIntervalAction reverse() {
        boolean result = !turn;
        return action(duration, result, center, radius);
    }
}