Crafty Game Engine
by Ali Dadmand
HTML
<script src="http://craftyjs.com/release/0.1/crafty-min.js"></script>
<h1>Elevator Action</h1>
<h3>User Arrows to Move; Space to Shoot ...</h3>
CSS
body, html { margin:0; padding:0; overflow:hidden }
JavaScript
/**
* Elevator Action
* Crafty JS
*/
Crafty.init(50, 600, 400); //start the game with 50FPS and 600 x 400 stage
Crafty.canvas();
//Initialize the sprite
Crafty.sprite(32, "http://craftyjs.com/demos/elevatoraction/images/sprite.png", {
player: [1,2,1,2],
door: [0,2,1,2],
reddoor: [0,0,1,2],
light: [6,0,1,1]
});
//initialize sounds
Crafty.audio.add({"shoot": "http://craftyjs.com/demos/elevatoraction/sounds/shoot.wav",
"quake": "http://craftyjs.com/demos/elevatoraction/sounds/quake.wav",
"spark": "http://craftyjs.com/demos/elevatoraction/sounds/spark.mp3"})
.settings({volume: 0.2});
Crafty.scene("title", function() {
Crafty.background("#111");
Crafty.sprite("http://craftyjs.com/demos/elevatoraction/images/title.png", {
title: [0,0, 430, 396]
})
Crafty.e("2D, DOM, title").attr({x: Crafty.viewport.width / 2 - 215});
Crafty.e("2D, DOM, mouse").attr({x: Crafty.viewport.width / 2 - 75, y: 290, w: 142, h: 74}).bind("click", function(e) {
Crafty.scene("main");
});
});
Crafty.scene("main", function() {
Crafty.background("#b1c7b5");
//Create the player
var player = Crafty.e("2D, player, DOM, gravity, controls, twoway, collision, animate, audio, health");
player.attr({"y":1, z: 30, facingRight: true}).gravity("floor").twoway(3)
.bind("keydown", function(e) {
if(e.keyCode === Crafty.keys.SP) {
if(!this.shoot) {
this.shoot = true;
this.delay(function() {
this.shoot = false;
}, 100);
this.stop();
Crafty.audio.play("shoot");
var bx, dir;
if(this.facingRight) {
this.sprite(5,2,1,2);
bx = this.x + 32;
dir = 'e';
} else {
this.sprite(5,0,1,2);
bx...