android - Libgdx -TextureRegion.flip()

标签 android libgdx 2d

我的纹理区域有问题。当我翻转TextureAtlas时,我的角色将仅向右侧方向移动(TextureAtlas被翻转的位置),并且不会返回到左侧方向。有人知道如何解决这个问题吗?

谢谢。

代码如下:

TextureAtlas atlas  = new TextureAtlas(Gdx.files.internal("texture/textures.pack"));
TextureRegion[] walkLeftFrame = new TextureRegion[5]; 
for(int i = 0 ; i<5; i++){
            walkLeftFrame[i] = atlas.findRegion("bob-0"+(i+2));
}
walkLeftAnimation = new Animation(RUNNING_FRAME_DURATION , walkLeftFrame); 

TextureRegion[] walkRightFrame = new TextureRegion[5];
for(int i=0; i<5; i++) {
            walkRightFrame[i] = atlas.findRegion("bob-0" + (i + 2));
            walkRightFrame[i].flip(true, false);
}
walkRightAnimation = new Animation(RUNNING_FRAME_DURATION, walkRightFrame);


if(bob.isFacingLeft())
              bobFrame = walkLeftAnimation.getKeyFrame(bob.getStateTime(), true);
else 
              bobFrame = walkRightAnimation.getKeyFrame(bob.getStateTime(), true);


 spriteBatch.draw(bobFrame, bob.getPosition().x * ppuX , bob.getPosition().y * ppuY , Bob.SIZE *ppuX, Bob.SIZE * ppuY);

最佳答案

这是典型的引用调用问题。

call by value

In call by value, a copy of actual arguments is passed to formal arguments of the called function and any change made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function.

call by reference
In call by reference, the location (address) of actual arguments is passed to formal arguments of the called function. This means by accessing the addresses of actual arguments we can alter them within from the called function.

这意味着您正在访问两个数组中相同的TextureRegions。这就是您所有区域都翻转的原因。

这样的事情应该有效:

TextureRegion[] walkRightFrame = new TextureRegion[5];
for(int i=0; i<5; i++) {
            walkRightFrame[i] = new TextureRegion(atlas.findRegion("bob-0" + (i + 2)));
            walkRightFrame[i].flip(true, false);
}

关于android - Libgdx -TextureRegion.flip(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38369602/

相关文章:

java - 设置光标位置

position - 起源和位置差异

c - 二维字符数组,像表格一样扫描和打印

math - 逆双线性插值?

android - 从网络解码图像时出现java.io.EOFException

java - 我的第一次登录尝试没有给我任何消息

android - 在 android 中转换时在 onstatusUpdated() 中获取媒体状态时出现空指针异常

android - 尝试在 BOOT_COMPLETED 时读取首选项

java - 解决-与TP相关的问题必须调用Libgdx SpriteBatch.end ...不要捕获错误

c - 尝试将文件读入动态分配的二维数组