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 Wrolf

Hi, I'm a little confused about the pressed and state function as I'm trying to build a top down 4 direction grid movement but want it so if the key is "pressed" once, the player wouldn't move but just face that direction until the key is held down.

I've tried several different ways but this one made the most sense to me but didn't work. Tried looking at some examples but none of them does what I want. Any pointers would be greatly appreciated.

player.js
	if (ig.input.pressed('up')){
		this.currentAnim = this.anims.idleup;
	}else if(ig.input.state('up')) {
		this.vel.y = -this.speed;
		this.vel.x = 0;
		this.colliY = this.pos.y.round() - this.colliSize;
		this.direction = "up";
		this.currentAnim = this.anims.up;
	}

1 decade ago by Joncom

The reason your code does not work is because it is very difficult to release the up key before the next frame. Your code probably does work, and on the first frame the animation is changed. However, another frame occurs before you have a chance to release, and then the "state" expression kicks in...

One possible solution is you could set a timer once the key is pressed. And then you could only let the entity move if enough time has passed since the timer started.

EntityPlayer = ig.Entity.extend({
    moveTimer: new ig.Timer(),
    moveDelay: 0.25,
    ...
    update: function() {
        if (ig.input.pressed('up')){
            this.moveTimer.reset();
            this.currentAnim = this.anims.idleup;
        } else if(ig.input.state('up') && 
                this.moveTimer.delta() > this.moveDelay) {
            this.vel.y = -this.speed;
            this.vel.x = 0;
            this.colliY = this.pos.y.round() - this.colliSize;
            this.direction = "up";
            this.currentAnim = this.anims.up;
        }
        ...
        this.parent();
    }
});

1 decade ago by Wrolf

Works like a charm Joncom, thank you for the help.
Page 1 of 1
« first « previous next › last »