1
\$\begingroup\$

When I run the App the Ball moves sometimes downwards, but sometimes its not moving. I dont change anything on script.

What I want is the ball should come from top outside of the screen, but when its visible in screen it should get an random xSpeed.

To test it I added first the bomb in the middle of the screen. But as I said the Ball is not showing sometimes. I run the game on my samsung galaxy s6 edge and the ball is there. Then when I run again it's not there.

That's how I coded.

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();
}

EDIT: Maybe here's a problem?

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();
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Solved the problem. \$\endgroup\$ – Eucar Apr 7 '16 at 14:12
  • \$\begingroup\$ if you resolved your problem, answer your own question for future references... \$\endgroup\$ – Alex Sifuentes Apr 7 '16 at 16:10
0
\$\begingroup\$

The problem was in this line.

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

I've used the wrong params, so I forgot width and height.

I changed to this.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.