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

目前,我正在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级讲师

reply all(1)
黄舟

Answer: A M
On github, I found a similar solution source code, so you can call the following method in 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));
    }
}
}

Answer: M.Tahir Ashraf
You can try the following code:

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

At the same time, be sure to ensure that the height and width of the image are both powers of 2.


Answer: Oleg Vaskevich
It seems that CCRepeatForever action can only run goBack instructions, which means that the operation is irreversible, so you can try the following code:

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);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template