Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by danielhorton

Can somebody explain this code to me? It's from line 49 of player.js in the jumpnrun demo.

var accel = this.standing ? this.accelGround : this.accelAir;

I'm trying to give player the ability to walk along the y axis, but this notion of standing seems to be getting into my way.

1 decade ago by dominic

The player&039;s acceleration in the Jump'n'Run demo is based on #this.standing property. If the player is standing (touching the ground with his feet) the acceleration is higher, than if he was in the air. This means you can change the direction of the player faster when he's on the ground and you have limited control over the character when he's in the air.

The ? : is a shorthand for if…then…else. So this:
var accel = this.standing ? this.accelGround : this.accelAir;

Could also be written as
var accel;
if( this.standing ) {
	accel = this.accelGround;
}
else {
	accel = this.accelAir;
}

If you want to have a top down view (like in Zelda), the important thing is to disable the gravity that normally pulls all entities down on the y-axis: Set the gravity property in your game class (main.js) to 0.

The .standing property of entities is only useful when you have a gravity, so for a top-down game, just ignore it.

Also have a look at the Pong example game. It has no gravity and the paddles move on the y-axis.
Page 1 of 1
« first « previous next › last »