android - 在Android上使用Cocos2D重复视差

标签 android cocos2d-android

我想在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/

相关文章:

android - 为android sdk 设置运行cocos2d 示例程序的环境。

java - Android cocos2d如何对CCscene进行截图?

java - Android编程(cocos2d),白色矩形代替图像

android - Android 中自动检测大容量存储/外部驱动器

android - 如何防止从 Playstore 安装新应用程序?但是应该有更新应用程序的选项

android - 为 Android 全屏制作 Xamarin 表单

android - 在 cocos 2D_android 中从一个场景移动到另一个场景

android - 从 Firebase 读取数据时发生 UnrecognizedPropertyException

android - 方向更改后如何将 fragment 重新加载到后台堆栈

java - 如何使用 Startactivity 并使调用 Cocos2d 层保持 Activity 状态 (Android/Java)?