I'd like to listen for and then save various keyboard inputs (arrow keys, letters A and D).
When a user hits the 'enter' key I'd like to use those saved inputs to control the movement/action of an entity.
For example. A user might hit the left arrow, then up arrow, then right arrow, then ‘enter’. The Entity he’s controlling in my game would then move left, up and then right.
Is it possible to delay the movement/action commands to an Entity in this fashion?
Thanks in advance!
Ethan
1 decade ago
by dominic
So, what you need is an array of movement commands. This array is filled when the keys are pressed and played back after you press enter. You also need to keep track if your entity is in "recording" or "playback" mode.
If you want to have continuous movement, you have to record the movement in each frame. You can do this in your entity's update function. Something like this should get you started:
moves: [],
state: 'recording',
update: function() {
if( this.state == 'recording' ) {
// if a key was pressed, append it to the
// moves array
if( ig.input.state('up') ) {
this.moves.push('up');
}
else if( ig.input.state('down') {
this.moves.push('down');
}
// handle x axis in the same way
if( ig.input.pressed('enter') ) {
// switch to playback mode
this.state = 'playback';
}
}
else if( this.state == 'playback' ) {
if( this.moves.length == 0 ) {
// no more moves. do something here
return;
}
// shift the first move from the moves array.
var currentMove = this.moves.shift();
if( currentMove == 'up' ) {
this.vel.y = -50;
}
else if( currentMove == 'down' ) {
this.vel.y = 50;
}
else {
this.vel.y = 0;
}
// handle x axis in the same way
}
// don't forget to call the parent update();
// this actually moves the entity according to
// its velocity
this.parent();
}
1 decade ago
by Joncom
I&039;m curious. What if the frame rate drops from 60 FPS to 50 FPS? Wouldn't this mean that fewer actions are processed per second? And would that mean that, since #ig.Timer
instances will have progressed the same regardless of the difference in frame rate, each playback could potentially yield very different results?
Page 1 of 1
« first
« previous
next ›
last »