Hi All,
Apologies for the vague subject line I'm having problems with getting animations to work correctly for me.
I have managed to get my jump and double jump working correctly. I am now trying to get my character running and then rolling and then back to running. The problem is I want to keep left or right pressed the whole time and then the animation to go into the roll then back to run (when I let go of the roll button) I'm just getting confused and can't see the forest for the tree.
i am using libgdx statemachine with ashley, i have a inputsystem for the movement and all the movement code is in the player class. i think im just getting confused with how to get it all working together correctly.
i have given some sample code below
PlayerState Enum
run(){
@Override
public void update(PlayerAgent entity) {
if (entity.stateComp.stateChanged) {
entity.stateComp.stateChanged = false;
Gdx.app.log("PlayerState:", "Run State");
//entity.spineComp.animationState.setAnimation(0, "Run", true);
entity.setAnimation(entity.spineComp.playerStates.get(EntityStateComponent.EntityMoveStates.run), true);
}
if (entity.bodyComp.body.getLinearVelocity().x == 0 && entity.bodyComp.body.getLinearVelocity().y == 0) {
entity.setState(idle);
}
}
},
rollstart(){
@Override
public void update(PlayerAgent entity) {
if (entity.stateComp.stateChanged) {
entity.stateComp.stateChanged = false;
Gdx.app.log("PlayerState:", "Rollstart State");
entity.setAnimation(entity.spineComp.playerStates.get(EntityStateComponent.EntityMoveStates.rollstart), true);
}
}
},
rollend(){
@Override
public void update(PlayerAgent entity) {
if (entity.stateComp.stateChanged) {
entity.stateComp.stateChanged = false;
Gdx.app.log("PlayerState:", "Rollend State");
entity.setAnimation(entity.spineComp.playerStates.get(EntityStateComponent.EntityMoveStates.rollend), true);
}
}
...
...
InputSystem
protected void processEntity(Entity entity, float deltaTime) {
if (typeComp.type == EntityTypeComponent.EntityType.PLAYERone) {
updateInput(deltaTime);
}
}
void updateInput (float delta) {
if (pdataComp.health == 0) return;
if (rollPressed){
//do nothing for now
}else if (leftPressed) {
directionComp.direction = -1;
EntityManager.getPlayerAgent().moveLeft();
} else if (rightPressed) {
directionComp.direction = 1;
EntityManager.getPlayerAgent().moveRight();
}
if (downPressed) {
EntityManager.getPlayerAgent().moveDown();
}
if (upPressed){
//EntityManager.getPlayerAgent().jump();
}
if (touched) {
//EntityManager.getPlayerAgent().shoot();
}else if (touched = false){
//EntityManager.getPlayerAgent().spine2d.animationState.setAnimation(1, player.view.shootAnimationFinish, false);
}
}
....
....
//code from KeyUp
....
@Override
public boolean keyDown(int keycode) {
switch(keycode){
case Input.Keys.ESCAPE:
//game.setScreen(new MenuScreen(game));
Gdx.app.exit();
break;
case Input.Keys.W:
case Input.Keys.SPACE:
case Input.Keys.UP:
if (pdataComp.health == 0) return false;
upPressed = true;
if(!EntityManager.getPlayerAgent().isTouchingGround){
EntityManager.getPlayerAgent().doublejump();
}
if (EntityManager.getPlayerAgent().isTouchingGround){
EntityManager.getPlayerAgent().jump();
}
break;
case Input.Keys.R:
if(EntityManager.getPlayerAgent().stateMachine.getCurrentState() == PlayerState.run){
EntityManager.getPlayerAgent().roll();
}
break;
....
....
public boolean keyUp (int keycode) {
switch(keycode){
case Input.Keys.W:
case Input.Keys.SPACE:
case Input.Keys.UP:
if (pdataComp.health == 0) return false;
if (EntityManager.getPlayerAgent().stateMachine.getCurrentState() == PlayerState.spinjumpstart) EntityManager.getPlayerAgent().setState(PlayerState.spinjumpend);
if(bodyComp.body.getLinearVelocity().y > 0) bodyComp.body.setLinearVelocity(bodyComp.body.getLinearVelocity().x ,bodyComp.body.getLinearVelocity().y * 0.01f);
upPressed = false;
break;
case Input.Keys.R:
if (EntityManager.getPlayerAgent().stateMachine.getCurrentState() == PlayerState.rollstart) EntityManager.getPlayerAgent().setState(PlayerState.rollend);
break;
....
....
playeragent movement code
...
public void jump() {
bodyComp.body.setLinearVelocity(bodyComp.body.getLinearVelocity().x, minMaxVelocityComp.maxVelocity.y /1.0f);
setState(PlayerState.jumpstart);
setState(PlayerState.jumpmidair);
isTouchingGround = false;
}
public void doublejump(){
if (jumpComp.jumpCount == 1){
bodyComp.body.setLinearVelocity(bodyComp.body.getLinearVelocity().x, minMaxVelocityComp.maxVelocity.y /1.0f);
setState(PlayerState.spinjumpstart);
isTouchingGround = false;
}
}
public void roll() {
if (isGrounded()) {
if (stateMachine.getCurrentState() == PlayerState.run) {
if (directionComp.direction == 1) {
bodyComp.body.setLinearVelocity(10.0f, bodyComp.body.getLinearVelocity().y);
}
if (directionComp.direction == -1) {
bodyComp.body.setLinearVelocity(-10.0f, bodyComp.body.getLinearVelocity().y);
}
setState(PlayerState.rollstart);
}
}
}
i used a lot of code from the spineboy example to get the animation working...
i hope the example code helps explain what im doing. let me know if you need me to make anything clearer...
Regards
Amarino