2013-04-18 95 views
2
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
    { 
     CGPoint location = [touch locationInView:[touch view]]; 
     location = [[CCDirector sharedDirector] convertToGL:location]; 

     shootVector = ccpSub(location, cannon.position); 
     CGFloat shootAngle = ccpToAngle(shootVector); 
     cocosAngle = CC_RADIANS_TO_DEGREES(-1 * shootAngle); 

     CGFloat rotateDiff = cocosAngle;  
     if (rotateDiff > 180) 
      rotateDiff -= 360; 
     if (rotateDiff < -180) 
      rotateDiff += 360;  

     CGFloat rotateSpeed = 360; 
     rotateDuration = fabs(rotateDiff/rotateSpeed); 

     [cannon runAction:[CCSequence actions: 
          [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle], 
          [CCCallFunc actionWithTarget:nil selector:nil], 
          nil]]; 

     top.rotation = cannon.rotation; 
    } 

上方触摸代码原因旋转360度的移动,相反,我想我的旋转大炮在180次以下..如何旋转180度cocos2d-iphone的精灵?

任何帮助,将不胜感激。

+0

最多可创建和Minmum degres,并与他们比较,如果rotateDiff> MaxDegree然后设置为MAX如果rotateDiff

回答

2

使用CCRotateBy命令代替CCRotateTo将导致一定量的而不是去到一定位置的旋转。以同样的方式你会CCRotateTo叫它如下:

[CCRotateBy actionWithDuration:rotateDuration angle:180] 
+0

不适用于我.. 它旋转180度甚至我触摸一次,我不想要,而是我想限制我的旋转到180度时,手指持续接触CANNON。 您能否创建演示并检查一次? –

+0

http://www.cocos2d-iphone.org/forum/topic/10836节省了我的时间。 –