libgdx – 纹理有时不会出现

当我运行应用程序时,球有时向下移动,但有时不移动。 我没有改变任何脚本。

我想要的是球应该来自屏幕外的顶部,但是当它在屏幕上可见时,它应该得到一个随机的xSpeed。

为了testing它,我首先在屏幕中间添加了炸弹。 但正如我所说的,球有时候并没有显示出来。 我在我的三星Galaxy S6的边缘运行游戏,球就在那里。 那么当我再次运行它不在那里。

这就是我编码。

private static final int SPRITE_SIZE = 32; // It has 32px width and height private static final int SPRITE_COUNT = 4; // there are 4 sprites private static final float MAX_SPEED = 1f; // maxspeed private static final float MIN_SPEED = 0.25f; // min speed private Vector2 position; private Animation anim; private Random random; private float speedX, speedY; public Bomb() { random = new Random(); int rand = random.nextInt(2); TextureRegion tmp = new TextureRegion(Asset.sprites, SPRITE_SIZE * SPRITE_COUNT, rand * SPRITE_SIZE); anim = new Animation(tmp, SPRITE_COUNT, 0.75f); position = new Vector2((MainGame.WIDTH/2) - (SPRITE_SIZE/2), MainGame.HEIGHT); speedX = 0; speedY = -MAX_SPEED; } public void update(float dt) { anim.update(dt); if(speedX == 0) { if(position.y + SPRITE_SIZE < MainGame.HEIGHT) { speedX = random.nextFloat() * (MAX_SPEED - MIN_SPEED) + MIN_SPEED; } position.add(speedX, speedY); } public void render(SpriteBatch sb) { sb.begin(); sb.draw(anim.getFrame(), position.x, position.y); sb.end(); } 

编辑:也许这是一个问题?

Asset.java

 public class Asset { public static Texture sprites; public static void init() { sprites = new Texture(Gdx.files.internal("sprite.png")); } public static void dispose() { sprites.dispose(); } } 

Animation.java

 public class Animation { private Array<TextureRegion> frames; private float maxFrameTime; private float currentFrameTime; private int frameCount; private int frame; public Animation(TextureRegion region, int frameCount, float cycleTime) { frames = new Array<TextureRegion>(); int frameWidth = region.getRegionWidth() / frameCount; for(int i = 0; i < frameCount; i++) { frames.add(new TextureRegion(region, i * frameWidth, 0, frameWidth, region.getRegionHeight())); } this.frameCount = frameCount; maxFrameTime = cycleTime / frameCount; frame = 0; } public void update(float dt) { currentFrameTime += dt; if(currentFrameTime > maxFrameTime) { frame++; currentFrameTime = 0; } if(frame >= frameCount) { frame = 0; } } public TextureRegion getFrame() { return frames.get(frame); } public void dispose() { for (int i = 0; i < frames.size; i++) { frames.get(i).getTexture().dispose(); } } } 

问题在于这一行。

 TextureRegion tmp = new TextureRegion(Asset.sprites, SPRITE_SIZE * SPRITE_COUNT, rand * SPRITE_SIZE); 

我使用了错误的参数,所以我忘了宽度和高度。

我改变了这个。

 TextureRegion tmp = new TextureRegion(Asset.sprites, 0, rand * SPRITE_SIZE, SPRITE_SIZE*SPRITE_COUNT, SPRITE_SIZE);