0
\$\begingroup\$

I am trying to make my sprite start moving from the moment it is created and do its animation. It draws on the screen right and animates but doesn't move. It should start moving towards the direction it is facin this means right and if collision detected to stop the animation do whatever and continue the animation. Here is my code:

public class grt extends ApplicationAdapter {
 Misheva mshv;



@Override
public void create () {
    mshv = new Misheva();
    mshv.init();


}

@Override
public void render () {
    mshv.drawIt();
    mshv.act(Gdx.graphics.getDeltaTime());

}

@Override
public void dispose() {
    mshv.disposer();

     }

 }

Misheva.java

public class Misheva extends Actor {
int state; 
TextureAtlas atl;
TextureRegion [] walk;
SpriteBatch batch;
Animation anim;
float stateTime;
float elapsed = 0;
int horizontalspeed = 10;


public void reset (){
    state = 0;
    stateTime = 0;
}

@Override
public void act(float delta) {
    super.act(delta);
    stateTime += delta;
    switch(state){
    case 0: setX(getX()-horizontalspeed*delta);

    }
}


public void drawIt(){
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    elapsed += Gdx.graphics.getDeltaTime();
    batch.draw(anim.getKeyFrame(elapsed,true), 0, 0);
    batch.end();

}

public void init (){
    atl = new TextureAtlas(Gdx.files.internal("data/tomaton.atlas"));
    batch = new SpriteBatch();
    walk = new TextureRegion[3];
    walk[0]=atl.findRegion("tomato");
    walk[1]=atl.findRegion("t_walk2");
    walk[2]=atl.findRegion("t_walk1");
    anim = new Animation(1/10f,walk);       
}

protected void disposer(){
    batch.dispose();
    atl.dispose();
    }


 }
\$\endgroup\$
2
  • \$\begingroup\$ Where do you call "reset"? Since the only place you set state=0 is in that method, and then you check for that in act in order to make it move. Personally I dislike using scene2d for GameGraphics, I only use it in UI since it can be quite restricting. Pretty sure you should set state=0 in your init() method aswell, since u want it to start right away. \$\endgroup\$
    – Green_qaue
    Commented Sep 26, 2015 at 13:18
  • \$\begingroup\$ nope even if I call it it still does not move \$\endgroup\$
    – nitheism
    Commented Sep 26, 2015 at 14:34

1 Answer 1

0
\$\begingroup\$

You are always drawing the texture region at 0,0. So you don't use the position of the actor at all.

To fix this use:

batch.draw(anim.getKeyFrame(elapsed,true), getX(), getY());

Depending on the coordinate system you use (y up/down) you need to adapt the value from getY().



As a side note, the code you try to run is quite inefficient and even wrong if you draw more than one element.

Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

This should only be called once in your render method (so best call it at the beginning of your grt class render method), since it clears the complete screen, so if you draw more than one element, you would only see the last one, since you clear it before you draw the next.

batch.begin();
batch.end();

Should only be called once too, for example when you start to draw your sprites.

Then you should take a look at the Actions class, as it provides methods to move actors (there are predefined actions).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .