我在插入多个相同精灵的孩子并访问它(或在运行时为它们设置位置)时遇到问题。请建议任何合适的方法,最好指出我的错误。这是我的方法。
//In the Init Method...
//int i is defined in the start.
for (i = 1; i < 4; i++)
{
hurdle = [CCSprite spriteWithFile:@"hurdle1.png"];
[self addChild:hurdle z:i tag:i];
hurdle.position = CGPointMake(150 * i, 0);
}
它将所有精灵散布在画布上。然后在一些“更新函数”中我调用它。
hurdle.position = CGPointMake(hurdle.position.x - 5, 10);
if (hurdle.position.x <= -5) {
hurdle.position = ccp(480, 10);
}
它可以工作,但正如预期的那样,只有一个实例水平移动。我希望移动所有实例,所以我正在尝试使用它....
for (i = 1; i < 4; i++){
[hurdle getChildByTag:i].position = CGPointMake(hurdle.position.x - 5, 10);
//OR
[hurdle getChildByTag:i].position = CGPointMake([hurdle getChildByTag:i].position.x - 5, 10);
}
我尝试在不同的地方获取日志,并意识到 getChildByTag 不能像我尝试使用它那样工作。