我有同样的问题并想通了。
尝试这个。将背景和偏移声明为成员:
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
,看看是否有效。
希望这对某人有帮助!