Crafty
Crafty copied to clipboard
What is the best way to make item not stop after key is up
I want the element to not stop until a callback is fired. The element is using multiway.
player.bind("KeyUp",function(e){
var key=keytonum(e.key);
if(key){
if(key.x)
player.vx=key.x*player.stats.speed;
else if(key.y)
player.vy=key.y*player.stats.speed;
socket.emit("move",0-key.x,0-key.y,function(){
if(key.x){
player.vx=0;
} else if(key.y){
player.vy=0;
}
});
}
})
Heres the code so far but it stops before the callback
The element is using multiway.
If you want custom movement behavior, it's probably easiest to use the methods of the underlying Controllable
component, specifically linkInput()
.
If I understand your code correctly you want a kind of pacman-like behavior, where the entity is always in motion and the player can only change its direction? By default, Multiway simply assigns a direction based on the user's input. When they let go of directional keys, that method is called with e.x=0
and e.y=0
. You can replace the initially bound callback with your own custom behavior.
var player = Crafty.e("2D, Color, Multiway, etc");
player.linkInput("DirectionalInput", player._dpadName, function(e){
// Only update direction if the input direction is non-zero
if (e.x != 0 || e.y != 0){
this._updateDirection(e);
}
});
The element is using multiway.
If that's all of your movement code then you don't need Multiway
- it could interfere with your code. Use Motion
instead.
I want the element to not stop until a callback is fired.
I'm not sure what you are trying to do here, but the entity could move far more than expected until the callback is invoked from another socket.
Maybe setting the position directly would work for your use-case
player.bind("KeyUp",function(e){
var key=keytonum(e.key);
if(key){
socket.emit("move",0-key.x,0-key.y,function(){
if(key.x){
player.x += key.x*player.stats.speed;
} else if(key.y){
player.y += key.y*player.stats.speed;
}
});
}
})