26

我正在使用 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);
4

6 回答 6

7

我看到了,因为没有一个答案对你有用。我将提供一个简单的代码来帮助您处理视差滚动背景。

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

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


}

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

于 2014-02-13T13:08:52.380 回答
1

从类 Constructor 调用此方法。我从示例中找到了这个技巧:github 上提供了“shotingblock-master”...

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

}

在我的情况下,它工作得很好。可能会对您有所帮助。

于 2013-05-22T07:55:08.510 回答
0

It looks like the CCRepeatForever action is only running it goBack, which means that it's not reversing. Try the following:

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);
于 2013-05-14T03:42:26.297 回答
0

尝试使用这个:

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 的幂。

于 2013-05-07T05:24:38.413 回答
0

请查看以下链接了解视差垂直无限背景: http: //kalpeshsantoki.blogspot.in/2014/07/create-vertical-endless-parallax.html

    CGSize winSize = CCDirector.sharedDirector().displaySize();

    //I made graphics for screen 720*1200....so I made this dynamic scale to support multiple screens
    float sX = winSize.width / 720.0f;
    float sY = winSize.height / 1200.0f;
    background = CCVerticalParallaxNode.node(sX, sY, true);

    background.addEntity(1f, "background.png", 0);
    background.addEntity(3, "road_simple.png", winSize.width / 2);
    background.addEntity(1.7f, "road_side.png", 0);
    addChild(background);
于 2014-07-26T18:04:44.077 回答
0

这是实现它的诀窍。您可以使用大 png 并对其进行处理查看 coocs2d-android 库中提供的示例测试代码

CCSprite background = CCSprite.sprite("background.png");

// create a void node, a parent node
CCParallaxNode voidNode = CCParallaxNode.node();

// background image is moved at a ratio of 0.4x, 0.5y
voidNode.addChild(background, -1, 0.4f, 0.5f, 0, 0);

// write your own code for the parallax node 
CCIntervalAction goUp = CCMoveBy.action(4, CGPoint.make(0,-200));
CCIntervalAction goDown = goUp.reverse();
CCIntervalAction go = CCMoveBy.action(8, CGPoint.make(-1000, 0));
CCIntervalAction goBack = go.reverse();
CCIntervalAction seq = CCSequence.actions(goUp, go, goDown, goBack);
voidNode.runAction(CCRepeatForever.action(seq));

addChild(voidNode);
于 2013-07-31T12:43:16.983 回答