把两个背景一个其他在Objective-C的iPhone游戏之后?(Putting two back

2019-09-21 03:04发布

嗨,我开发一个iPhone游戏

在我的游戏层,我有两个相同的背景图片,但我希望他们去一个接一个。

一旦游戏动物(例如企鹅)跨越第一的背景下,我想第一个背景,第二one--后去成为一个连续的背景,直到游戏结束。

我已经试过各种--- for循环,while循环等,但似乎没有任何工作

有没有人有我怎么会去这样做的想法?

感谢您的帮助,您可以提供给我。

这是所有我有很多不同的尝试后至今

- (id) init
{
    if((self = [super init]))
    {
        for ( int x = 0; x < 10000000; ++x)
        {
            CCSprite *bg = [CCSprite spriteWithFile:@"level1.png"];
            [bg setPosition:ccp(160,240)];
            ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
            [bg.texture setTexParameters:&params];
            [self addChild:bg z:0];

            CCSprite *bg2 = [CCSprite spriteWithFile:@"level1.png"];
            [bg2 setPosition:ccp(320,480)];
            ccTexParams param = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
            [bg2.texture setTexParameters:&param];
            [self addChild:bg z:0];
        }

Answer 1:

如果背景是相同的,你实际上并不需要2幅图像,你可以设置一个纹理矩形位置,它会不断地移动。 如果你调用moveBackground一个计时器或更新方法,将连续滚动。

-(void)init
{
    if((self=[super init]))
    {
        background = [CCSprite spriteWithFile:@"background.png"]; 
        ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
        [background.texture setTexParameters:&params];
        [self addChild:background z:0];
    }
}

-(void)moveBackground
{
    // Scroll background 5 pixels to the left
    int speed = 5;
    background.textureRect = CGRectMake(background.textureRect.origin.x-speed, 
                                        background.textureRect.origin.y, 
                                        background.textureRect.size.width, 
                                        background.textureRect.size.height);
}


文章来源: Putting two backgrounds one after the other in objective-c iphone game?