简体   繁体   中英

Any sprite disappears when trying to move along y-axis

I am developing a game where a character is able to jump. He can perfectly move along x-axis using Vector2 but as soon as I add y-component (to be able to move up) my sprite just disappears. I implement his x-axis motion this way:

position.add(200 * dt, velocity.y * dt);

where 200 is a speed and dt is the time elapsed since the last frame render.

For moving up I have a method called "jump" and it's implemented this way:

public void jump () {
        velocity.y = 20;
    }

So, in my PlayScreen I call jump and the character should move up, but instead, it vanishes. I tried this with the other objects I have in the game, and they all behave the same way - they disappear as soon as I try to move them up or down. I can't really understand what's the problem.

PS my world has dimensions 136 pixels (width) * screen height of the device

UPDATE WITH MORE INFO: Here is my whole character class:

public class Astroboy{

public  Vector2 velocity;
public  Vector2 position;
public Animation<TextureRegion> heroAnim;
private TextureAtlas atlas;
public Rectangle astroRect;

public Astroboy (float x, float y){
    velocity = new Vector2(200, 0);
    position = new Vector2(x, y);
    atlas = new TextureAtlas("Anims/Hero_Anim.atlas");
    heroAnim = new Animation<TextureRegion>(0.15f, atlas.findRegions("HeroRunning") , Animation.PlayMode.LOOP); // animation
}

public void update (float dt) {
    position.add(velocity.x * dt, velocity.y * dt);
}

public void jump () {
    velocity.y = 10;
}

public Animation<TextureRegion> getAnimation () {
    return heroAnim;
}

}

A character is being drawn by SpriteBatch in PlayState screen using camera used to draw moving objects:

//SETTING CAMERAS
fixedCamera1 = new OrthographicCamera(); // camera for static background
fixedCamera1.setToOrtho(false, WORLD_WIDTH, WORLD_HEIGHT);

stageCamera1 = new OrthographicCamera(); // camera for a character in motion
stageCamera1.setToOrtho(false, WORLD_WIDTH, WORLD_HEIGHT);
//--------------------

batch.setProjectionMatrix(stageCamera1.combined);
batch.begin();
batch.draw(astroboy.getAnimation().getKeyFrame(stateTime, true), astroboy.position.x, astroboy.position.y, WORLD_WIDTH / 4, WORLD_HEIGHT / 4);
batch.end

and jump motion is implemented with just

if (Gdx.input.justTouched()) {
            astroboy.jump();
        }

At a guess, dt is higher than you think and the multiplication pushes the sprite out of bounds. As a start - use x and y variables and assign them the values 200 * dt and velocity.y * dt so you can see what's going on.

int x = 200 * dt;
int y = velocity.y * dt;
System.out.println("x="+x+", y="+y);
position.add(x, y);

Edit

Screen#render in libgdx is defined like this:

 /** Called when the screen should render itself.
  * @param delta The time in seconds since the last render. */
 public void render (float delta);

So it's not so likely that the parameter passed into dt is really high.

Still the original advice holds - step through your code in a debugger or print out values so you can see what's going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM