我想在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 个 Sprite ,这样一旦第一个完成,另一个就开始,反之亦然,但我坚持实现。
最佳答案
我也遇到了同样的问题,并解决了。
试试这个。将背景和偏移量声明为成员:
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);
请参阅 http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture 中的“重复背景” Objective C 代码
如果您需要双向使用,您也许可以从非零 _bgOffset
开始,看看是否有效。
希望这对某人有帮助!
关于android - 在Android上使用Cocos2D重复视差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8564511/