0

好吧,我们开始吧。我有一个 cocos2d 应用程序,并且有向玩家移动的目标。当玩家移动时,我希望他们再次缓慢地将目的地转向玩家,这样他们就不会只是进入空旷的空间。是否可以更改精灵 mid-runAction 的目的地?

编辑:

这是 - (void)changeTargetDest 中的代码

- (void)changeTargetDest {
    NSMutableArray* deleteArray = [[NSMutableArray alloc] init];
    for(CCSprite* s in _targets) {
        float offX = s.position.x - player.position.x;
        float offY = s.position.y - player.position.y;
        float adjustX;
        float adjustY;
        float offDistance = sqrt(powf(offX, 2.0f) + powf(offY, 2.0f));

        if(offDistance < 15) {
            [deleteArray addObject:s];

            deaths++;
            [deathLabel setString:[NSString stringWithFormat:@"Deaths: %ld", deaths]];
            if(deaths == 0)
            [kdLabel setString:[NSString stringWithFormat:@"K/D ratio: %ld.00", score]];
            else
                [kdLabel setString:[NSString stringWithFormat:@"K/D ratio: %.2f", ((float)score / (float)deaths)]];
        }

        else {
            adjustX = offX * .99;
            adjustY = offY * .99;

            CGPoint point = CGPointMake(player.position.x + adjustX, player.position.y + adjustY);

            [s setPosition:point];
        }//else

    }//for

    for (CCSprite *target in deleteArray) {

        [_targets removeObject:target];
        [self removeChild:target cleanup:YES];                                  
    }
}

这很好用,除了一个问题。因为新位置的计算方法只是取前一个偏移量的 0.99,所以目标离玩家越近,它移动的越慢。我怎样才能使它的速度恒定?

4

1 回答 1

1

您可以在计划的方法中每隔几帧停止操作并运行一个新操作。但更好的方法是根据玩家位置计算目标的位置,并使用 setPosition 在更新方法中每帧手动更改其位置。

于 2011-11-02T09:22:55.283 回答