Introduction to CraftyJS : Movement in Crafty

  • Where are you running to?

    In Crafty there are a few distinct predefined movement options:

      • Twoway
      • Fourway
      • Multiway
      • No way?

    Twoway and Fourway are built using Multiway, one of the good sides of compinents!

Twoway

.twoway(Number speed[, Number jumpSpeed])

  • speed – Movement speed of the entity (in pixels)
  • jumpSpeed – Jump speed of the entity (in pixels)

W, A and D or Up, Left and Right arrow.

Fourway

.fourway(Number speed)

  • speed – Movement speed of the entity (in pixels)

W, S, A and D or Up, Down, Left and Right arrow.

Multiway

.multiway([Number speed,] Object keyBindings )

  • speed – Movement speed of the entity (in pixels)
  • keyBindings – Binds a key to a direction, the direction is specified in degrees (in pixels)

Examples:

this.multiway(3, {UP_ARROW: -90, DOWN_ARROW: 90,
RIGHT_ARROW: 0, LEFT_ARROW: 180});
this.multiway({x:3,y:1.5}, {UP_ARROW: -90, DOWN_ARROW: 90,
RIGHT_ARROW: 0, LEFT_ARROW: 180});
this.multiway({W: -90, S: 90, D: 0, A: 180});

  • Code example fourway
</pre>
// When the DOM has loaded

$(document).ready(function() {

var WIDTH = 800, HEIGHT = 640;

// Initialize Crafty
 Crafty.init(WIDTH, HEIGHT);

Crafty.background("black");

// Create a player entity
 var player = Crafty.e();

// Add some components to the entity
 player.addComponent("2D, DOM");

player.attr({
 x : 0,
 y : 0,
 w : 50,
 h : 50
 });

player.addComponent("Color").color("red");

player.addComponent("Fourway").fourway(5);

});
<pre>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s