【问题标题】:Current Position of SKSpritenode in method TouchesBegan方法 TouchesBegan 中 SKSpritenode 的当前位置
【发布时间】:2014-02-25 17:38:10
【问题描述】:

这是我的第一个问题,我希望它不是太愚蠢,我已经搜索但找不到答案,希望你能帮助我!

我创建一个 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*/                             
     }

好吧,就是这样,可能很简单,或者根本不可能这样做。对不起,我刚开始编码。谢谢你们的时间!

【问题讨论】:

  • 你所拥有的东西很不一样。你能解释一下你想要达到的目标吗?也许为我们发布更多代码?
  • 您可以通过将 miChar 设为实例 var 或通过名称 (childNodeWithName:) 获取它来获取 miChar。另一方面......您的while括号内的表达式将始终为真,并且会在您的情况下导致while(YES)循环。(可能是错误的逻辑)此外,这个位置验证看起来更像是您想要做的事情更新:。
  • 谢谢!知道了,我会在更新中尝试,对不起,我知道那不是太多代码,只是想简单一点。大约在此时......你是对的,它总是是的,除非 miChar 的位置被刷新(因为它在不断移动),这是我想要实现的,能够看到那个新位置。我会在更新中尝试,更有意义!太棒了!

标签: sprite-kit touchesbegan skspritenode


【解决方案1】:

我这个问题here 我展示了touchesBegan 在执行循环中作为主线程的一部分在一个一致的地方运行。我相信所有操作也都在主线程中运行,除非您另外指定,因此在您的代码执行时没有任何更新它的危险(默认情况下)。

【讨论】:

    猜你喜欢
    • 2013-05-04
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2021-09-22
    • 2014-03-11
    • 1970-01-01
    相关资源
    最近更新 更多