HTML5 Game Programming with enchant.js by Brandon McInnis Ryo Shimizu Hidekazu Furukawa Ryohei Fushimi Ryo Tanaka & Kevin Kratzer

HTML5 Game Programming with enchant.js by Brandon McInnis Ryo Shimizu Hidekazu Furukawa Ryohei Fushimi Ryo Tanaka & Kevin Kratzer

Author:Brandon McInnis, Ryo Shimizu, Hidekazu Furukawa, Ryohei Fushimi, Ryo Tanaka & Kevin Kratzer
Language: eng
Format: epub
Publisher: Apress, Berkeley, CA


Using Collision Detection for Movement

Now the map in our game exists, but we need a way to move our character around in accordance with the data. Maps in enchant.js support a method for finding out if a specific position on the map is able to be walked on or not. This is used to determine if the character can walk on the tile or not. Do the following to implement it: 1. Under the line player.anim = [...];//Down, type in the code in Listing 4-9.

Listing 4-9. Character Movement with Map Collision Detection

player.addEventListener(Event.ENTER_FRAME, function() {

//Move up

if (game.input.up) {

player.dir = DIR_UP;

player.y -= 4;

if (map.hitTest(player.x + 16, player.y + 32)) player.y += 4;

}

//Move down

else if (game.input.down) {

player.dir = DIR_DOWN;

player.y += 4;

if (map.hitTest(player.x + 16, player.y + 32)) player.y -= 4;

}

//Move left

else if (game.input.left) {

player.dir = DIR_LEFT;

player.x -= 4;

if (map.hitTest(player.x + 16, player.y + 32)) player.x += 4;

}

//Move right

else if (game.input.right) {

player.dir = DIR_RIGHT;

player.x += 4;

if (map.hitTest(player.x + 16, player.y + 32)) player.x -= 4;

}

//Frame setting

if (!game.input.up && !game.input.down &&

!game.input.left && !game.input.right) player.age = 1;//Standing Still

player.frame = player.anim[player.dir * 4 + (player.age % 4)];

});



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.