android - 如何添加无限滚动的背景?
PHP中文网
PHP中文网 2017-04-17 11:28:07
0
1
672

目前,我正在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);

原问题:Adding Endless parallax background in cocos2d android

PHP中文网
PHP中文网

认证0级讲师

全部回覆(1)
黄舟

答:A M
在github上,我找到了類似的解答源碼,因此可以在Constructor中呼叫以下方法:

private void endlessBackground() {
    // Create the two background sprites which will alternate
    _oddBackground = CCSprite.sprite("blue_background.png");
    _evenBackground = CCSprite.sprite("blue_background.png");
    // One starts dead centre and one starts exactly one screen height above
    oddBackground.setPosition(_winSize.width / 2, _winSize.height / 2);
    evenBackground.setPosition(_winSize.width / 2, _winSize.height
            + (_winSize.height / 2));
    // Schedule the scrolling action
    schedule("scroll");
    // Add sprites to the layer
    addChild(_oddBackground).addChild(_evenBackground);
}

public void scroll(float dt) {
    // move them 100*dt pixels down
    _oddBackground.setPosition(_oddBackground.getPosition().x,
            _oddBackground.getPosition().y - 150 * dt);
    _evenBackground.setPosition(_evenBackground.getPosition().x,
            _evenBackground.getPosition().y - 150 * dt);
    // reset position when they are off from view.
    if (_oddBackground.getPosition().y < -_winSize.height / 2) {
        _oddBackground.setPosition(_winSize.width / 2, _winSize.height / 2);
        _evenBackground.setPosition(_winSize.width / 2, _winSize.height
                + (_winSize.height / 2));
    }
}
}

答:M.Tahir Ashraf
可以嘗試以下程式碼:

CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("pic.png");
ccTexParams params = {GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
texture->setTexParameters(&params);

CCSprite *sprite = CCSprite::spriteWithTexture(texture, CCRectMake(0, 0, 90, 90));

同時,一定要確保圖片的高度和寬度都是2的冪。


答:Oleg Vaskevich
似乎CCRepeatForever action只能運行goBack的指令,這意味著運行是不可逆的,所以可以嘗試如下程式碼:

CCIntervalAction go = CCMoveBy.action(4, CGPoint.ccp(winSize.width, 0));
CCIntervalAction goBack = go.reverse();
CCIntervalAction seq = CCSequence.actions(go, goBack);
CCRepeatForever action = CCRepeatForever.action(seq); // change to seq instead of goBack
parallaxNode.runAction(action);
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板