gpt4 book ai didi

android - Libgdx - TextureRegion.flip()

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:14 37 4
gpt4 key购买 nike

我对 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);
}

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

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com