Fliping the Character #2725
Answered
by
eonarheim
cemalgnlts
asked this question in
Q&A
-
Hi, I want to flip to the other direction when the character is going left currently I am using this method: onPreUpdate(engine) {
this.vel.x = 0;
if(engine.input.keyboard.isHeld(ex.Input.Keys.A)
|| engine.input.keyboard.isHeld(ex.Input.Keys.Left)) {
// Flip X axis.
this.graphics.current[0].graphic.flipHorizontal = true;
this.vel.x = -this.SPEED;
}
} I wanted to ask if this is the correct method. |
Beta Was this translation helpful? Give feedback.
Answered by
eonarheim
Aug 2, 2023
Replies: 1 comment 1 reply
-
@cemalgnlts Great question! What you have can work just fine. (If you use latest alpha bits there is a new top level Other approaches we use are pre defining their flipped graphics in the // my actor
onInitialize {
const leftIdle = new ex.Animation({...})
this.graphics.add('left-idle', leftIdle);
const rightIdle = leftIdle.clone();
rightIdle.flipHorizontal = true;
this.graphics.add('right-idle', rightIdle);
}
onPreUpdate(engine) {
this.vel.x = 0;
if(engine.input.keyboard.isHeld(ex.Input.Keys.A)
|| engine.input.keyboard.isHeld(ex.Input.Keys.Left)) {
this.graphics.use('left-idle')
this.vel.x = -this.SPEED;
}
if(engine.input.keyboard.isHeld(ex.Input.Keys.D)
|| engine.input.keyboard.isHeld(ex.Input.Keys.Right)) {
this.graphics.use('right-idle')
this.vel.x = this.SPEED;
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
cemalgnlts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cemalgnlts Great question!
What you have can work just fine. (If you use latest alpha bits there is a new top level
this.graphics.flipHorizontal = true
that can be used)Other approaches we use are pre defining their flipped graphics in the
onInitialize