我的纹理区域有问题。当我翻转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/