🤑 Indie game store🙌 Free games😂 Fun games😨 Horror games
👷 Game development🎨 Assets📚 Comics
🎉 Sales🎁 Bundles

Weirdness with Arcadebody2d

A topic by Unwary created 1 year ago Views: 234 Replies: 2
Viewing posts 1 to 2

So I have the following code:

class ShipBehavior extends Sup.Behavior {

//Connect the ship to it's collision

ship_hull = this.actor.arcadeBody2D;

//Set the handling speed

speed : number = .025

awake() {

}

update() {

let x : number = this.actor.getX();

let x_speed : number = Math.abs(this.ship_hull.getVelocityX());

let new_x_speed : number = 0;

//Check in any key is pressed, and if one is, update the direction accordingly

//Handle leftward movement

if(Sup.Input.isKeyDown("A")){

new_x_speed =-this.speed;

}

//Handle rightward movement

else if(Sup.Input.isKeyDown("D")){

new_x_speed = this.speed;

}

this.ship_hull.setVelocityX(new_x_speed);

}

}

Sup.registerBehavior(ShipBehavior);


But it produces strange behavoir when run, as I move at a crawl to the left, but at a very fast (maybe 10x?) pace when I go left. this is literally the only script in the game, so the problem is going to be in here.

Thanks in advance,

Unwary

Maybe all you have to do is put a ELSE conditional to set Zero into X axis when no button is pressed. So, when the key left up, the actor stops.

//Handle rightward movement
 else if(Sup.Input.isKeyDown("D")){
 new_x_speed = this.speed;
 } else {
   // Set here, inside the else.
   new_x_speed = 0;
 }
(+1)

I discovered this, it was a case of the stupidity that you miss in the moment. The speed the object already had was read as the absolute value of. Thanks though.