当前位置 : 主页 > 手机开发 > 其它 >

添加无尽的视差背景在cocos2d android

来源:互联网 收集:自由互联 发布时间:2021-06-13
我正在工作的CoCos2d与android.I想添加一个无尽的滚动背景到我的屏幕使用CCParallaxNode。 我可以添加背景和移动它,但完成后,该动作的屏幕变黑。 有人可以帮我吗? 我的代码是 CCParall
我正在工作的CoCos2d与android.I想添加一个无尽的滚动背景到我的屏幕使用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);
我看到,因为没有一个单一的答案为你工作。我将提供一个简单的代码,将帮助您为您的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);

和每毫秒调度的滚动方法。
在构造函数中添加

this.schedule("scroll");

现在滚动方法。

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


}

标记我的答案如果它的工作。

网友评论