搜索

首页  >  问答  >  正文

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

原问题:Adding Endless parallax background in cocos2d android

PHP中文网PHP中文网2781 天前655

全部回复(1)我来回复

  • 黄舟

    黄舟2017-04-17 11:30:07

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

    回复
    0
  • 取消回复