2011-07-15 57 views
1

嗨我終於在cocos2d中製作了一個可操作的遊戲杆。我能夠將精靈旋轉到操縱桿大拇指或「帽」指向的確切角度。但是,我無法將精靈移向同一個方向。用我設置旋轉代碼的方式移動精靈有沒有簡單的方法?如果你的拇指仍然按下,但是沒有移動操縱桿,還有辦法讓它移動嗎? PS這個代碼都在TouchesMoved方法中。 PPS。帽子是拇指,墊子是操縱桿背景,Sprite2是我想要移動的精靈。 (95,95)是焊盤精靈的中心。如何使用遊戲杆以特定角度移動精靈

if(capSprite.position.x>=padSprite.position.x){ 
      id a3 = [CCFlipX actionWithFlipX:NO]; 
      [sprite2 runAction:a3]; 
     } 
     if(capSprite.position.x<=padSprite.position.x){ 
      id a4 = [CCFlipX actionWithFlipX:YES]; 
      [sprite2 runAction:a4]; 
     } 


     CGPoint pos1 = ccp(95, 95); 
     CGPoint pos2 = ccp(capSprite.position.x, capSprite.position.y); 
     int offX = pos2.x-pos1.x; 
     int offY = pos2.y-pos1.y; 


     float angleRadians = atanf((float)offY/(float)offX); 
     float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians); 
     float theAngle = -1 * angleDegrees; 
     sprite2.rotation = theAngle; 

回答

0

我不熟悉cocos2d的,但我有一個快速瀏覽一下文檔和這個樣本可能是你有幫助:

if keys[key.UP]: 
     self.target.acceleration = (200 * rotation_x, 200 * rotation_y) 

我寫了一個很長的解釋回答你的第二個問題但我相信這個「​​self.target.acceleration」也可以解決這個問題。你可以在cocos2d API documentation閱讀更多。

0

我通常做的是讓角度,它與ccpForAngle(浮動)轉換成CGPoint,然後由值乘以CGPoint:

float angle = whatever; 
CGPoint anglePoint = ccpForAngle(angle); 
// You will need to play with the mult value 
angle = ccpMult(angle, 2.5); 
// This also works with box2D or probably Chipmunk. 
sprite.position = angle;