Libgdx - TextureRegion.flip()
Libgdx - TextureRegion.flip()
我对 TextureRegion 有疑问。当我翻转 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);
}
我对 TextureRegion 有疑问。当我翻转 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);
}