cocos2d的无限循环

时间:2012-10-23 12:59:23

标签: ios loops cocos2d-iphone a-star

我正在使用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方法中使用此代码,那么它永远不会因某种原因而移动..我在哪里可以使用此代码使我的敌人精灵无限移动?感谢..

1 个答案:

答案 0 :(得分: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