Skip to content

Instantly share code, notes, and snippets.

@RylandAlmanza
Created November 1, 2012 08:00
Show Gist options
  • Save RylandAlmanza/3992392 to your computer and use it in GitHub Desktop.
Save RylandAlmanza/3992392 to your computer and use it in GitHub Desktop.
Crafty.c("CustomControls", {
CustomControls: function() {
this.bind("EnterFrame", function() {
var up = Crafty.keydown[Crafty.keys.UP_ARROW];
var down = Crafty.keydown[Crafty.keys.DOWN_ARROW];
var left = Crafty.keydown[Crafty.keys.LEFT_ARROW];
var right = Crafty.keydown[Crafty.keys.RIGHT_ARROW];
if (up) {
this.y_velocity = -2;
if (!this.isPlaying("walk_north")) {
this.stop().animate("walk_north", 45, -1);
}
}
if (down) {
this.y_velocity = 2;
if (!this.isPlaying("walk_south")) {
this.stop().animate("walk_south", 45, -1);
}
}
if (left) {
this.x_velocity = -2;
if (!this.isPlaying("walk_west")) {
this.stop().animate("walk_west", 45, -1);
}
}
if (right) {
this.x_velocity = 2;
if (!this.isPlaying("walk_east")) {
this.stop().animate("walk_east", 45, -1);
}
}
if (!left && !right) {
this.x_velocity = 0;
this.stop();
}
if (!up && !down) {
this.y_velocity = 0;
this.stop();
}
this.x += this.x_velocity;
this.y += this.y_velocity;
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment