Login   Register  
Icontem

File: js/game_objects/rectangle.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Pere Monfort  >  lightCyan  >  js/game_objects/rectangle.js  >  Download  
File: js/game_objects/rectangle.js
Role: Example script
Content type: text/plain
Description: game object
Class: lightCyan
Render and update game objects in a canvas element
Author: By
Last change:
Date: 2012-02-10 00:09
Size: 1,265 bytes
 

Contents

Class file image Download
lightCyan.addGameObject("rectangulo", function () {

	var nAxisX = 50;
	var nAxisY = 50;
	var nWidth = 25;
	var nHeight = 25;
	var nSpeed = 5;

	var bMoveRight = false;
	var bMoveLeft = false;
	var bMoveUp = false;
	var bMoveDown = false;

	return {
		update : function (canvas) {

			if (bMoveRight === true) {
				nAxisX += nSpeed;
			}
			if (bMoveLeft === true) {
				nAxisX -= nSpeed;
			}
			if (bMoveUp === true) {
				nAxisY -= nSpeed;
			}
			if (bMoveDown === true) {
				nAxisY += nSpeed;
			}
		},
		draw : function (canvas) {
			canvas.bufferContext.beginPath();
			canvas.bufferContext.rect(nAxisX, nAxisY, nWidth, nHeight);
			canvas.bufferContext.fillStyle = "#000";
			canvas.bufferContext.closePath();
			canvas.bufferContext.fill();
		},
		keydown : function (nKeyCode) {
			if (nKeyCode === 39) {
				bMoveRight = true;
			}
			if (nKeyCode === 37) {
				bMoveLeft = true;
			}
			if (nKeyCode === 38) {
				bMoveUp = true;
			}
			if (nKeyCode === 40) {
				bMoveDown = true;
			}
		},
		keyup : function (nKeyCode) {
			if (nKeyCode === 39) {
				bMoveRight = false;
			}
			if (nKeyCode === 37) {
				bMoveLeft = false;
			}
			if (nKeyCode === 38) {
				bMoveUp = false;
			}
			if (nKeyCode === 40) {
				bMoveDown = false;
			}
		}
	};
});