阅读364 返回首页    go 技术社区[云栖]


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

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

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

实现效果:


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);
    }
}



最后更新:2017-04-02 16:47:36

  上一篇:go android自定义ProgressBar(仿淘宝)的加载效果
  下一篇:go Android超时机制的处理(很不错)