在cocos2d android中添加Endless parallax背景

Ris*_*waj 26 android cocos2d-android

我正在使用android进行CoCos2d.我希望使用CCParallaxNode为我的屏幕添加无尽的滚动背景.我可以添加背景并移动它但在完成移动操作后屏幕变黑.有人可以帮我吗?

我的代码是

CCParallaxNode parallaxNode;
CCSprite spacedust1;
CCSprite spacedust2;
CCSprite planetsunrise;
CCSprite galaxy;
CCSprite spacialanomaly;
CCSprite spacialanomaly2;

parallaxNode = CCParallaxNode.node();

    spacedust1 = CCSprite.sprite("bg_front_spacedust.png");
    spacedust2 = CCSprite.sprite("bg_front_spacedust.png");
    planetsunrise = CCSprite.sprite("bg_planetsunrise.png");
    galaxy = CCSprite.sprite("bg_galaxy.png");
    spacialanomaly = CCSprite.sprite("bg_spacialanomaly.png");
    spacialanomaly2 = CCSprite.sprite("bg_spacialanomaly2.png");
    // 3) Determine relative movement speeds for space dust and background
    // CGPoint cgPoint = CGPoint.ccp(0.1, 0.1);

    CGPoint dustSpeed = CGPoint.ccp(10, 10);
    CGPoint bgSpeed = CGPoint.ccp(5, 5);
    // CGPoint bgSpeed = ccp(0.05, 0.05);

    parallaxNode.addChild(spacedust1, 0, dustSpeed.x, dustSpeed.y, 0,
            winSize.height / 2);
    parallaxNode.addChild(spacedust2, 0, dustSpeed.x, dustSpeed.y,
            spacedust1.getContentSize().width, winSize.height / 2);
    parallaxNode.addChild(galaxy, -1, bgSpeed.x, bgSpeed.y, 0, 10);
    parallaxNode.addChild(planetsunrise, -1, bgSpeed.x, bgSpeed.y, 600, 5);
    parallaxNode
            .addChild(spacialanomaly, -1, bgSpeed.x, bgSpeed.y, 900, 20);
    parallaxNode.addChild(spacialanomaly2, -1, bgSpeed.x, bgSpeed.y, 1500,
            30);
    CCIntervalAction go = CCMoveBy.action(4, CGPoint.ccp(winSize.width, 0));
    CCIntervalAction goBack = go.reverse();
    CCIntervalAction seq = CCSequence.actions(go, goBack);
    CCRepeatForever action = CCRepeatForever.action(goBack);
    parallaxNode.runAction(action);
Run Code Online (Sandbox Code Playgroud)

San*_*p R 7

我知道,因为没有一个答案适合你.我将提供一个简单的代码,它将帮助您进行parralax滚动背景.

在游戏图层构造函数中添加此代码

background1 = CCSprite.sprite("bg2.png");
background2 = CCSprite.sprite("bg2.png");

background1.setPosition(CGPoint.ccp(winSize.width*0.5f,winSize.height*0.5f));
addChild(background1);

background2.setPosition(CGPoint.ccp(winSize.width+winSize.width*0.5f,winSize.height*0.5f));
addChild(background2);
Run Code Online (Sandbox Code Playgroud)

以及每毫秒安排的滚动方法.在构造函数中添加它

this.schedule("scroll");
Run Code Online (Sandbox Code Playgroud)

现在是滚动方法.

public void scroll(float dt) {

    CGPoint pos1 = background1.getPosition();
    CGPoint pos2 = background2.getPosition();

    pos1.x -= 5.0f;
    pos2.x -= 5.0f;

    if(pos1.x <=-(winSize.width*0.5f) )
    {
        pos1.x = pos2.x + winSize.width;
    }

    if(pos2.x <=-(winSize.width*0.5f) )
    {
        pos2.x = pos1.x + winSize.width;
    }

    background1.setPosition(pos1);
    background2.setPosition(pos2);


}
Run Code Online (Sandbox Code Playgroud)

如果有效,请标记我的答案.