HTML and CSS Reference
In-Depth Information
The main Crafty object also acts like the jQuery object in that it can be used to query for objects that have a
specific component or combination of components:
// will return all entities with the Enemy component
Crafty ships with a number of useful components, including basic physics; polygon-based collision detec-
tion; two-way (platformer), four-way (top-down) and touch controls; and sound support.
You can create new components by calling Crafty.c and passing in an init method and any additional
methods that should be added onto the entity.
A separate site for components has been set up: . This site enables users to sub-
mit components and makes it easy to load components directly from the web.
To get a sense of what working with Crafty looks like, look at the code in Listing 26-1 . It creates a white box
for the play area and adds in gravity and two-way controls that enable you to run around and jump. Below the
player a blue floor object is created that enables the player to run around on top of it.
Listing 26-1: A simple example in Crafty
<script src="jquery.min.js"></script><script
<script type="text/javascript">
$(function() {
// Create the player object with some initial components
var player = Crafty.e("2D, Canvas, Color, Player, Physics")
.attr({w:50, h:50, x:126, y:0});
// You can also add additional components after the fact
var floor = Crafty.e("2D, Canvas, Color, Collision, Floor")
.attr({h:30, w:400, x:0, y:380 })
Search WWH ::

Custom Search