-1

我是 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);
        }
    }

所以现在,当我运行它时,硬币从右侧移入并从左侧移出屏幕。我该如何做到这一点,以便当硬币向左移动并离开屏幕时,有五秒钟的延迟,然后让新硬币像原来一样从右侧回到屏幕上。

谢谢!

4

1 回答 1

-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);
}

我想这就是你要找的。

于 2013-07-19T04:59:52.117 回答