相信各位同学偶尔会看到有些手机游戏中会出项一张图片围着某个点作圆周运动,本篇教程将教你如何实现Cocos2D-Android中图片围绕点做圆周运动 。
实现效果:
具体代码:
publicclassCCRoundByextendsCCIntervalAction{
booleanturn;//ForwardorReverseround
floatstartAngle;//default
floatradius;//Roundcircleradius
CGPointcenter;//Roundcirclecenterpoint
publicbooleanisTurn(){
returnturn;
}
publicvoidsetTurn(booleanturn){
this.turn=turn;
}
publicfloatgetStartAngle(){
returnstartAngle;
}
publicvoidsetStartAngle(floatstartAngle){
this.startAngle=startAngle;
}
publicfloatgetRadius(){
returnradius;
}
publicvoidsetRadius(floatradius){
this.radius=radius;
}
publicCGPointgetCenter(){
returncenter;
}
publicvoidsetCenter(CGPointcenter){
this.center=center;
}
/**createstheaction*/
publicstaticCCRoundByaction(floatduration,booleana,CGPointpoint,floatr){
returnnewCCRoundBy(duration,a,point,r);
}
/**initializestheaction*/
protectedCCRoundBy(floatduration,booleana,CGPointpoint,floatr){
super(duration);
turn=a;
radius=r;
center=point;
}
@Override
publicvoidstart(CCNodeaTarget){
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
publicvoidupdate(floatt){
//XXX:shallIadd%360
floatrotate=(startAngle+360.0f*t);
if(turn){
rotate*=-1;
}
target.setRotation(rotate);
floatfradian=(float)(rotate*Math.PI/180.0f);
CGPointpos=CGPoint.ccp(center.x+radius*MathUtils.sin(fradian),
center.y+radius*MathUtils.cos(fradian));
target.setPosition(pos);
}
@Override
publicCCIntervalActionreverse(){
booleanresult=!turn;
returnaction(duration,result,center,radius);
}
}
¥98.00
¥108.00
¥698.00
¥98.00