2

我想在 Android 上使用 Cocos2D 绘制无限重复的视差。现在,Objective C 中针对这个问题提供了一些解决方案,但我仍然坚持在 Android 中的实现。我试过使用

CCSprite background = CCSprite.sprite("background_island.png");
CCTexParams params = new CCTexParams(GL10.GL_LINEAR,GL10.GL_LINEAR,GL10.GL_REPEAT,GL10.GL_REPEAT);
            background.getTexture().setTexParameters(params);

但它只会将背景延伸到 1 个方向。我想我必须使用 2 个精灵,这样一旦第一个完成,另一个开始,反之亦然,但我坚持执行。

4

2 回答 2

3

我有同样的问题并想通了。

尝试这个。将背景和偏移声明为成员:

CCSprite _bg;
float _bgOffset;

在您的场景构造函数中:

CGSize winSize = CCDirector.sharedDirector().displaySize();
_bg = CCSprite.sprite("yourbg.png"); // needs to be square, i.e. 256x256
_bg.setTextureRect(0, 0, winSize.width, winSize.height, false);
_bg.getTexture().setTexParameters(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_REPEAT,
        GL10.GL_REPEAT);
_bg.setAnchorPoint(CGPoint.zero());
this.addChild(_bg);

在你的 update(float dt) 方法中:

if (_bgOffset > 2000000000)
    _bgOffset = 0; // don't want problems, do we?
_bgOffset += dt * PIXELS_PER_SECOND; // this can be dynamic if you want
_bg.setTextureRect(0, _bgOffset, _bg.getTextureRect().size.width,
            _bg.getTextureRect().size.height, false);

有关Objective C 代码,请参阅http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture中的“重复背景”

如果您需要双向使用,您也许可以从非零开始_bgOffset,看看是否有效。

希望这对某人有帮助!

于 2012-06-09T22:34:19.033 回答
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-28T05:00:56.050 回答