0

我正在使用 sqlboy 的A 星路径查找代码让我的敌人精灵跟随我的主角。. 敌人精灵应该无限地跟随主要精灵,但我不确定我该怎么做..

我为此使用以下代码..

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

4

1 回答 1

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
于 2012-10-23T14:00:11.660 回答