2014-03-03 41 views
0

我是一個在SpriteKit,遊戲開發newb,其實我只是在學習。所以我得到了一個點,我需要將一堆節點移動到用戶點擊位置。到目前爲止,我努力要計算一個虛擬的直角三角形,並根據兩側得​​出角度的角度和角度。不幸的是,給我留下了非常強烈的衝動,並沒有真正考慮用戶點擊位置。applyImpulse對CGPoint SpriteKit

任何想法?

在此先感謝。

+0

它不完全是你想要的,但將是一個開始http://stackoverflow.com/questions/19172140/skaction-move-forward/19172574#19172574 – DogCoffee

+0

謝謝你的建議。我確定我可以在某個地方使用它。 –

回答

4

中查找射擊彈丸在本教程由Ray Wenderlich這裏部分:

http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

變化從教程中的代碼如下:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    // 1 - Choose one of the touches to work with 
    UITouch * touch = [touches anyObject]; 
    CGPoint location = [touch locationInNode:self]; 

    // 2 - Set up initial location of projectile 
    SKSpriteNode * projectile = [self childNodeWithName:@"desirednode"]; 
    //make projectile point to your desired node. 

    // 3- Determine offset of location to projectile 
    CGPoint offset = rwSub(location, projectile.position); 

    // 4 - Bail out if you are shooting down or backwards. You can ignore this if required. 
    if (offset.x <= 0) return; 

    // 5 - OK to add now - we've double checked position 
    [self addChild:projectile]; 

    // 6 - Get the direction of where to shoot 
    CGPoint direction = rwNormalize(offset); 

    // 7 - Make it shoot far enough to be guaranteed off screen 
    float forceValue = 200; //Edit this value to get the desired force. 
    CGPoint shootAmount = rwMult(direction, forceValue); 

    //8 - Convert the point to a vector 
    CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y); 
    //This vector is the impulse you are looking for. 

    //9 - Apply impulse to node. 
    [projectile.physicsBody applyImpulse:impulseVector]; 

} 

在彈丸對象代碼代表您的節點。此外,您將需要編輯forceValue以獲得所需的衝動。

+0

謝謝你的所有幫助,這很好。 –