gpt4 book ai didi

java - Android AndEngine 生成随机 Sprite 奇怪的问题

转载 作者:行者123 更新时间:2023-11-30 03:04:44 26 4
gpt4 key购买 nike

首先我从加载资源开始

                this.mTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 130, 130);
this.m1TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p1.png", 0, 0);
this.m2TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p2.png", 0, 0);
this.m3TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p3.png", 0, 0);
this.m4TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p4.png", 0, 0);
this.mTextureAtlas.load();

那我做

int x;
x = MathUtils.random(1, 4);
Log.e("euatl", "is" + x);

p = null;

if(x == 1){
p = new Sprite(mCameraWidth-pX, pY, this.mB1TextureRegion, this.getVertexBufferObjectManager());
} else if(x == 2){
p = new Sprite(mCameraWidth-pX, pY, this.m2TextureRegion, this.getVertexBufferObjectManager());
} else if(x == 3){
p = new Sprite(mCameraWidth-pX, pY, this.mB3TextureRegion, this.getVertexBufferObjectManager());

} else if(x == 4){
p = new Sprite(mCameraWidth-pX, pY, this.m4TextureRegion, this.getVertexBufferObjectManager());
}

mScene.attachChild(p);

目前总是生成 p4 Sprite ,TextureAtlas 中的最后一个 Sprite 。我的 logcat 说 x 有时是随机的 1、2、3 和 4。但在手机中, Sprite 总是最后一个 (p4)

最佳答案

您需要将您的纹理放在图集的不同位置。假设您的纹理 (px.png) 大小为 130x130:

            this.mTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 4 * 130, 130);
this.m1TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p1.png", 0, 0);
this.m2TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p2.png", 130, 0);
this.m3TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p3.png", 260, 0);
this.m4TextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p4.png", 390, 0);
this.mTextureAtlas.load();

更好的解决方案:

int textureCount = 4;
TextureRegion[] mTextureRegions = new TextureRegion[textureCount];

for (int i = 0; i < mTextureRegions.length; i++)
this.mTextureRegions[i] = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mTextureAtlas, this, "p" + (i + 1) + ".png", i * 130, 0);
this.mTextureAtlas.load();

关于java - Android AndEngine 生成随机 Sprite 奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21987685/

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