2014-02-25 31 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作为执行循环中主线程的一部分在一致的地方运行。我相信除非另有指定,否则所有操作都会在主线程中运行,因此在执行代码时更新它没有任何危险(默认情况下)。