Intro to Crafty - Entity Instances
Third code demo slide from a crafty js presentation: https://github.com/ajacksified/Craftyjs-Presentation
by ajacksified
HTML
<script src="https://raw.github.com/louisstow/Crafty/bcb8e2e9e5cd3c3dc63eab9e7ecc80e6b0b5fd9a/crafty-min.js"></script>
<h1>Demo 3: Crafty.c Entity Instances</h1>
<button id="addBox">Add Box</button>
<div id="cr-stage"></div>
JavaScript
var width = 400,
height = 300;
Crafty.init(width, height);
Crafty.c("Box", {
init: function() {
this.addComponent("2D, Canvas, Color");
},
makeBox: function(color){
var x = Math.random() * 100 >> 0;
var y = Math.random() * 100 >> 0;
this.attr({x: x, y: y, w: 20, h: 20}).color(color);
return this;
}
});
$("#addBox").bind("click", function(){
var box = Crafty.e("Box").makeBox("#FF0000");
});