我在插入同一精靈的多個子項並訪問它(或在運行時爲它們設置位置)時遇到問題。請提醒任何合適的方法,最好指出我的錯誤。這是我的方法。添加和訪問CCSprites
//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不工作我想的方式用它。
我應該聲明enumHurdle作爲一類? CCSprite? – 2012-01-18 06:35:20
@ToughGuy這是正確的。謝謝,我會編輯它。 – 2012-01-18 06:44:48
非常感謝您的幫助。但是現在我對加入更多精靈毫無頭緒,但是想要避免共享相同的標籤:D – 2012-01-18 07:05:34