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

7 years ago by camprad

I'm not able to get my game to recognize that the keypress has gone up. My guy shoots continuously and constantly moves right or left, even after I stop pressing the key. Here's my code:

	// Bind keys
ig.input.bind( ig.KEY.LEFT_ARROW, 'left' );
ig.input.bind( ig.KEY.RIGHT_ARROW, 'right' );
ig.input.bind( ig.KEY.X, 'jump' );
ig.input.bind( ig.KEY.C, 'shoot' );

// move left or right
        	var accel = this.standing ? this.accelGround : this.accelAir;
        	if( ig.input.state('left') ) {
        		this.accel.x = -accel;
        		this.flip = true;
				alert("you pressed left");
        	}else if( ig.input.state('right') ) {
        		this.accel.x = accel;
        		this.flip = false;
				alert("you pressed right");
        	}else{
        		this.accel.x = 0;
        	}
        	// jump
        	if( this.standing && ig.input.pressed('jump') ) {
        		this.vel.y = -this.jump;
                this.jumpSFX.play();
        	}
            // shoot
            if( ig.input.state('shoot') ) {
            	ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
                this.shootSFX.play();
				alert("you pressed c");
            }

7 years ago by Joncom

Try replacing alert with console.log like so:

            if( ig.input.state('left') ) {
                this.accel.x = -accel;
                this.flip = true;
                console.log("you pressed left");
            }

7 years ago by lachsen

The problem precisely is probably like this:
When the alert message is shown, the window of the games loses focus.
When this happens, the key-up event is not registered, even after you close the alert message.

This problem does not only occur with alert. It also happens for instance when you press the url bar of your browser or something like this. So it happens quite often that key up events are not registered - and it's really annoying imho.

Fortunately, there is a solution/workaround for that. You simply need to register a focus-lost/blur listener to the window and clear all pressed button states. You just need to extend ig.input like this to do so:

Add this function in the ig.Input class:

blur: function(event){
        for(var action in this.actions){
            if(this.actions[action] == true){
                this.keyups[action] = true;
                this.delayedKeyup.push(action);
                event.stopPropagation();
                event.preventDefault();
            }
        }
    }

And add this line in the 'initMouse' function:
window.addEventListener('blur', this.blur.bind(this), false );

I hope this helps.
Page 1 of 1
« first « previous next › last »