2014-06-30 94 views
0

我想使用按鈕上下移動我的Sprite。既然你不能在sprite Kit中使用UIButtons,我使用的是不同的SpriteKitNodes。節點將是箭頭圖像。但我想使用箭頭圖像來移動我的原始精靈,但只是觸摸它。我以爲我會使用SKAction,但我卡住了。是否有可能使用另一個移動一個精靈?如何使用按鈕向上和向下移動spriteKit節點

回答

0

SpriteKit中的按鈕可能沒有任何規定,但可以輕鬆將SKSpriteNode繼承爲按鈕。我已經在GitHub here上創建了這樣一個類。

根據方向按鈕使用SKAction進行移動是不可取的。相反,您需要使用方向按鈕的標誌,在這些標誌上您將移動-update:方法中的節點。

將標誌保持爲實例變量。

@implementation MyScene 
{ 
    BOOL upDirection; 
    BOOL downDirection; 
} 

-initWithSize:方法中將它們初始化爲FALSE。

這是你應如何處理的旗幟在-update方法:

-(void)update:(CFTimeInterval)currentTime 
{ 
    /* Called before each frame is rendered */ 

    if (upDirection) 
    { 
     myNode.position = CGPointMake(myNode.position.x, myNode.position.y + 5); //Increment value can be adjusted 
    } 

    if (downDirection) 
    { 
     myNode.position = CGPointMake(myNode.position.x, myNode.position.y - 5); //Decrement value can be adjusted 
    } 
} 
相關問題