【问题标题】:Infinite loop with cocos2dcocos2d的无限循环
【发布时间】:2012-10-23 12:59:22
【问题描述】:

我正在使用 sqlboy 的 A star Path Finding code 让我的敌人精灵跟随我的主角.. 敌人精灵应该无限地跟随主要精灵,但我不确定我该怎么做..

我正在使用以下代码..

curPoint = [self tileCoordForPosition:ccp(enemy.sprite.position.x, enemy.sprite.position.y)];
nextPoint = [self tileCoordForPosition:ccp(killer.sprite.position.x, killer.sprite.position.y)];

[pathFinder moveSprite:enemy.sprite from: curPoint to:nextPoint atSpeed:0.3f];

如果我在我的 init 方法中使用此代码,那么它只会被调用一次,如果我的杀手精灵移动,敌人将不会跟随它..

如果我在我的update:(ccTime)dt 方法中使用此代码,那么由于某种原因它永远不会移动。我在哪里可以使用此代码让我的敌人精灵无限移动?谢谢..

【问题讨论】:

  • update: 方法实际上是否被持续调用?

标签: ios loops cocos2d-iphone a-star


【解决方案1】:

要使用update: 方法,请使用

[self scheduleUpdate];

例如,在您的 onEnter 方法中。只是不要忘记稍后取消计划更新

[self unscheduleUpdate];

我的意思是像

@interface MyNode : CCNode
{
}
@end

@implementation MyNode

- (void) onEnter
{
    [super onEnter];

    [self scheduleUpdate];
}

- (void) onExit
{
    [super onExit];

    [self unscheduleUpdate];
}

- (void) update:(ccTime) dt
{
    // this method will be called every tick
    // if you need to update something, make it here
}

@end

【讨论】:

  • 我不确定你的意思..你能给我一个例子..我是cocos2d的新手..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 2012-12-24
  • 2013-09-05
  • 1970-01-01
相关资源
最近更新 更多