我是Cocos2d的初学者,我想在离开屏幕5秒延迟时显示硬币精灵。所以这就是我在主游戏层中写的连续添加7个硬币的内容:
- (void)coinSidewaysRowOne {
if (coinSide1 == FALSE)
{
coinSide1 = TRUE;
NSLog(@"coinSide1 = TRUE");
int originalX = 500;
for(int i = 0; i < 8; i++)
{
CCSprite *coinHorizontal = [CCSprite spriteWithFile:@"bubble.png"];
coinHorizontal.position = ccp(originalX, 150);
originalX += 20;
[self addChild:coinHorizontal];
[coinArray addObject:coinHorizontal];
}
}
}
然后,在我的updateRunning方法中,我添加了这个,所以当硬币在屏幕外产生时,它们向左移动并消失:
// Move coins off the screen and make them move away
for (CCSprite *coin in coinArray) {
// apply background scroll speed
float backgroundScrollSpeedX = [[GameMechanics sharedGameMechanics] backGroundScrollSpeedX];
float xSpeed = 1.09 * backgroundScrollSpeedX;
// move the coin until it leaves the left edge of the screen
if (coin.position.x > (coin.contentSize.width * (-1)))
{
coin.position = ccp(coin.position.x - (xSpeed*delta), coin.position.y);
}
}
所以现在,当我运行时,硬币从右侧移入并从左侧移出屏幕。 如何制作它,以便当硬币向左移动并离开屏幕时,延迟5秒钟,然后让新硬币从右边回到屏幕,就像它最初那样。
谢谢!
答案 0 :(得分:-1)
您可以调用一个函数,将该精灵添加回屏幕,延迟时间为5秒。
您必须按如下方式添加一些代码:
for (CCSprite *coin in coinArray)
{
// apply background scroll speed
float backgroundScrollSpeedX = [[GameMechanics sharedGameMechanics] backGroundScrollSpeedX];
float xSpeed = 1.09 * backgroundScrollSpeedX;
// move the coin until it leaves the left edge of the screen
if (coin.position.x > (coin.contentSize.width * (-1)))
{
coin.position = ccp(coin.position.x - (xSpeed*delta), coin.position.y);
}
else
{
[self performSelector:@selector(showSpriteAgain:) withObject:coin afterdelay:5.0f];
}
}
并制作一个将该精灵再次添加到屏幕的功能:
-(void) showSpriteAgain:(CCSprite *)coin
{
coin.position = ccp(coin.position.x+screenSize.width,coin.position.y);
}
我认为这就是你要找的东西。