2014-02-25 29 views
0

這是我的第一個問題,我希望它只是不太愚蠢,我已經搜索,但無法找到答案,我希望你能幫助我!SKSpritenode當前位置的方法TouchesBegan

我創建並添加到我的場景一個SKSpriteNode,然後我想能夠知道它的當前位置在方法TouchesBegan,然後使用條件取決於其當前位置。

-(id)initWithSize:(CGSize)size { 
if (self = [super initWithSize:size]) { 

    SKSpriteNode *miChar = [self createCharacter]; 

    [self addChild: miChar]; 

}

這裏只是簡單的方法將其添加到我的場景,那麼我想知道miChar.position.y例如當的touchesBegan

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

    //Here is where i need the position of miChar.position.y 

    SKSpriteNode *currentPos=miChar.position.y; 

    while(currentPos.position.y > miChar.position.y){ 

     /* i want to do things here until the position is above for example 
     miChar is an SKSpriteNode that is on constant movement so i need to check its 
     position wich will be constantly changing*/        
    } 

那麼,這是它,可能是如此簡單或只是不可能這樣做..即時對不起,剛開始編碼。謝謝你的時間傢伙!

+0

你有什麼是相當。你能否更詳細地解釋你想達到的目標?也許爲我們發佈更多的代碼? – jervine10

+0

您可以通過將其作爲實例var或通過名稱抓取它(childNodeWithName :)來獲得miChar。另一方面,你的while支架中的表達式總是是真的,並且會導致while(YES)循環(可能是錯誤的邏輯) 此外,這個位置驗證看起來更像是你想要的東西做更新:。 –

+0

謝謝!得到它我會嘗試更新,對不起,我知道那不是太多的代碼,只是想簡單。 關於一段時間...你是對的,它總會是YES,除非miChar的位置被更新(因爲它在不斷移動),這正是我想要實現的,以便能夠看到新的位置。我會在更新中嘗試它,更有意義! TYY! – user3352358

回答

0

我這個問題here我發現touchesBegan作爲執行循環中主線程的一部分在一致的地方運行。我相信除非另有指定,否則所有操作都會在主線程中運行,因此在執行代碼時更新它沒有任何危險(默認情況下)。