Intro to Crafty - Extending Classes

Fourth code demo slide from a crafty js presentation: https://github.com/ajacksified/Craftyjs-Presentation

by omni5cience

HTML

<script src="http://craftyjs.com/release/0.4.7/crafty-min.js"></script>
<h1>Demo 4: Extending Crafty Classes</h1>
<button id="addBox">Add Box</button>
<button id="addMovableBox">Add Movable 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;  
    }
});

Crafty.c("MovableBox", {
    init: function(){
      this.addComponent("Box, Fourway");
      this.fourway(10);  
    } 
});



$("#addBox").bind("click", function(){ 
    var box = Crafty.e("Box").makeBox("#FF0000");
});

$("#addMovableBox").bind("click", function(){ 
    var box = Crafty.e("MovableBox").makeBox("#00FF00");
});