如何无限移动的背景图像中的iOS coco2d [英] How to move background images infinitely in iOS coco2d

查看:144
本文介绍了如何无限移动的背景图像中的iOS coco2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在iOS的移动Coco2d背景图片,但我有一些困难。我曾尝试提供一些网站的一些解决方案,但都没有成功地让他们能够正常工作。下面是code我目前工作: -

的背景平稳移动第一时间,但它不之后正常工作: -

code在初始化函数: -

  BG1 = [CCSprite spriteWithFile:@bg1.png];
bg1.anchorPoint = CGPointZero;
[个体经营的addChild:BG1 Z:-2];BG2 = [CCSprite spriteWithFile:@bg1.png];
[个体经营的addChild:BG2 Z:-3]。
bg2.anchorPoint = CGPointMake(480,0);//安排每一帧重复回调
[自时间表:@selector(nextFrame :)区间:.4f];


   - (无效)nextFrame:(ccTime)DT {
    ID actionMove = [CCMoveTo actionWithDuration:0.4位置:CCP(bg1.position.x - 100 * DT,bg1.position.y)]; //winSize.height/2)];    ID actionMove1 = [CCMoveTo actionWithDuration:0.4位置:CCP(bg2.position.x - 100 * DT,bg2.position.y)]; //winSize.height/2)];    ID actionMoveDone = [CCCallFuncN actionWithTarget:自我选择:@选择(spriteMoveFinished :)]。
    [BG1 runAction:[CCSequence动作:actionMove,actionMoveDone,无]];
    [BG2 runAction:[CCSequence动作:actionMove1,actionMoveDone,无]];
} - (无效)spriteMoveFinished:(ID)发送{    CCSprite *精灵=(CCSprite *)寄件人;
    如果(精灵== BG1){
        如果(bg1.position.x< -480){
            [个体经营removeChild之:BG1清理:NO];
            bg1.position = CCP(480,bg1.position.y);            [个体经营的addChild:BG1 Z:-2];
        }
    }
    否则,如果(精灵== BG2)
        如果(bg2.position.x< -480){
            [个体经营removeChild之:BG2清理:NO];
            bg2.position = CCP(bg1.position.x + 480,bg1.position.y);
            [个体经营的addChild:BG2 Z:-3]。
        }
    }
}


解决方案

我觉得这种方式有点简单。您初始化背景的init 更新移动它们

在init方法:

  //位置的背景
CCSprite * BG1 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite * BG2 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite * BG3 = [CCSprite spriteWithSpriteFrame:spriteFrame];
bg1.anchorPoint = CCP(0,0);
bg1.position = CCP(0,0);
bg2.anchorPoint = CCP(0,0);
bg2.position = CCP(bg1.contentSize.width-1,0);
bg3.anchorPoint = CCP(0,0);
bg3.position = CCP(2 * bg1.contentSize.width-1,0);
_backgrounds = @ [BG1,BG2,BG3][个体经营的addChild:BG1 Z:INT_MIN];
[个体经营的addChild:BG2 Z:INT_MIN];
[个体经营的addChild:BG3 Z:INT_MIN];

在更新方法:

  //无尽滚动的背景
对(在_backgrounds CCSprite * BG){
    bg.position = CCP(bg.position.x - 50 *三角洲,bg.position.y);
    如果(bg.position.x&下; -1 *(bg.contentSize.width)){
        bg.position = CCP(bg.position.x +(bg.contentSize.width * 2)-2,0);
    }
}

注:code是cocos2d的3.0

I have to move background images in iOS Coco2d but I am having a few difficulties. I have tried some solutions provided on some websites but have not been successful in getting them to work properly. Below is the code I am currently working on:-

The background moves smoothly the first time but it is not working properly after that:-

Code in init function :-

bg1 = [CCSprite spriteWithFile: @"bg1.png"];
bg1.anchorPoint = CGPointZero;
[self addChild:bg1 z:-2];

bg2 = [CCSprite spriteWithFile: @"bg1.png"];
[self addChild:bg2 z:-3];
bg2.anchorPoint = CGPointMake(480, 0);

// schedule a repeating callback on every frame
[self schedule:@selector(nextFrame:) interval:.4f];


- (void) nextFrame:(ccTime)dt {
    id actionMove = [CCMoveTo actionWithDuration:.4 position:ccp(bg1.position.x - 100 * dt, bg1.position.y)]; //winSize.height/2)];

    id actionMove1 = [CCMoveTo actionWithDuration:.4 position:ccp(bg2.position.x - 100 * dt, bg2.position.y)]; //winSize.height/2)];

    id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
    [bg1 runAction:[CCSequence actions:actionMove,actionMoveDone, nil]];
    [bg2 runAction:[CCSequence actions:actionMove1,actionMoveDone, nil]];   
}

-(void)spriteMoveFinished:(id)sender {

    CCSprite *sprite = (CCSprite *)sender;
    if(sprite == bg1) {
        if (bg1.position.x < -480) {
            [self removeChild:bg1 cleanup:NO];
            bg1.position = ccp( 480 , bg1.position.y );

            [self addChild:bg1 z:-2];
        }
    }
    else if(sprite == bg2)
        if (bg2.position.x < -480) {
            [self removeChild:bg2 cleanup:NO];
            bg2.position = ccp( bg1.position.x+ 480 , bg1.position.y );
            [self addChild:bg2 z:-3];
        }
    }
}

解决方案

I think this way is a little simpler. You initialize the backgrounds in initand move them in update.

In the init method:

// position backgrounds
CCSprite *bg1 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite *bg2 = [CCSprite spriteWithSpriteFrame:spriteFrame];
CCSprite *bg3 = [CCSprite spriteWithSpriteFrame:spriteFrame];
bg1.anchorPoint = ccp(0, 0);
bg1.position = ccp(0, 0);
bg2.anchorPoint = ccp(0, 0);
bg2.position = ccp(bg1.contentSize.width-1, 0);
bg3.anchorPoint = ccp(0, 0);
bg3.position = ccp(2*bg1.contentSize.width-1, 0);
_backgrounds = @[bg1, bg2, bg3];

[self addChild:bg1 z:INT_MIN];
[self addChild:bg2 z:INT_MIN];
[self addChild:bg3 z:INT_MIN];

In the update method:

// endless scrolling for backgrounds
for (CCSprite *bg in _backgrounds) {
    bg.position = ccp(bg.position.x - 50 * delta, bg.position.y);
    if (bg.position.x < -1 * (bg.contentSize.width)) {
        bg.position = ccp(bg.position.x + (bg.contentSize.width*2)-2, 0);
    }
}

Note: the code is for Cocos2d 3.0

这篇关于如何无限移动的背景图像中的iOS coco2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆