Thank you for visiting Blazing Games

Low-friction Fun

Implementing the crates was done in the OotW30Map class but the main code is used for handling the buttons and keyboard events.


stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyboard);
north_btn.addEventListener(flash.events.MouseEvent.CLICK, northClicked);
south_btn.addEventListener(flash.events.MouseEvent.CLICK, southClicked);
east_btn.addEventListener(flash.events.MouseEvent.CLICK, eastClicked);
west_btn.addEventListener(flash.events.MouseEvent.CLICK, westClicked);
restart_btn.addEventListener(flash.events.MouseEvent.CLICK, restartClicked);
stop();

function handleKeyboard(evt:KeyboardEvent)
{
var kcode:uint = evt.keyCode;
switch (kcode) {
case Keyboard.UP: // North
commandNorth();
break;
case Keyboard.DOWN: // South
commandSouth();
break;
case Keyboard.LEFT: // West
commandWest();
break;
case Keyboard.RIGHT: // East
commandEast();
break;
}
}

function endDialog(evt:Event)
{
gotoAndPlay(1, "Title");
}

function northClicked(evt:Event)
{
commandNorth();
}

function eastClicked(evt:Event)
{
commandEast();
}
function southClicked(evt:Event)
{
commandSouth();
}
function westClicked(evt:Event)
{
commandWest();
}
function restartClicked(evt:Event)
{
map_movie.resetMap();
player_movie.setMapLocation(player_movie.MAP_START_X,
player_movie.MAP_START_Y,
player_movie.MAP_START_DIR, false);
}

function commandNorth()
{
if (player_movie.isAnimating())
return;
var px:uint = player_movie.getMapX();
var py:uint = player_movie.getMapY();
player_movie.setDirection(0);
if (map_movie.canEnter(px, py-1))
player_movie.setMapLocation(px, py-1, 0);
else
map_movie.performPush(px, py-1, 0);
if (map_movie.hasWon())
gotoAndPlay("win");
}

function commandEast()
{
if (player_movie.isAnimating())
return;
var px:uint = player_movie.getMapX();
var py:uint = player_movie.getMapY();
player_movie.setDirection(1);
if (map_movie.canEnter(px+1, py))
player_movie.setMapLocation(px+1, py, 1);
else
map_movie.performPush(px+1, py, 1);
if (map_movie.hasWon())
gotoAndPlay("win");
}

function commandSouth()
{
if (player_movie.isAnimating())
return;
var px:uint = player_movie.getMapX();
var py:uint = player_movie.getMapY();
player_movie.setDirection(2);
if (map_movie.canEnter(px, py+1))
player_movie.setMapLocation(px, py+1, 2);
else
map_movie.performPush(px, py+1, 2);
if (map_movie.hasWon())
gotoAndPlay("win");
}

function commandWest()
{
if (player_movie.isAnimating())
return;
var px:uint = player_movie.getMapX();
var py:uint = player_movie.getMapY();
player_movie.setDirection(3);
if (map_movie.canEnter(px-1, py))
player_movie.setMapLocation(px-1, py, 3);
else
map_movie.performPush(px-1, py, 3);
if (map_movie.hasWon())
gotoAndPlay("win");
}

 

From the OotW30Map class the push is implemented by verifying that the push can occur and then by setting the parameters of the push. The enter frame event is then used to actually animate the movement of the crate with the crate moving in the indicated direction until it actually hits another crate or a wall.

 

public function canPush(nx:uint, ny:uint, nd:uint):Boolean
{
var target:uint = getTile(nx, ny);
if ((target < 1) || (target > 3)) { // make sure pushing a valid column of crates
trace("not a movable column");
return false;
}
var pushSpot:uint = getTile(nx + O30.DIR_OFFSETX[nd], ny + O30.DIR_OFFSETY[nd]);
return (pushSpot == 0);
}

public function performPush(nx:uint, ny:uint, nd:uint)
{
if (canPush(nx, ny, nd)) {
_pushBox.setTileValue(getTile(nx, ny));
setTileValue(nx, ny, 0);
_pushX = nx;
_pushY = ny;
_pushD = nd;
_pushFrame = 0;
addEventListener(Event.ENTER_FRAME, animateBox);
}
}

protected function animateBox(evt:Event)
{
++_pushFrame;
_pushBox.x = _pushX * O30.MAP_TILESIZE + O30.MAP_OFFSET_X + _pushFrame*8*O30.DIR_OFFSETX[_pushD];
_pushBox.y = _pushY * O30.MAP_TILESIZE + O30.MAP_OFFSET_Y + _pushFrame*8*O30.DIR_OFFSETY[_pushD];
_pushBox.visible = true;
if (_pushFrame > 5) {
_pushFrame = 0;
_pushX += O30.DIR_OFFSETX[_pushD];
_pushY += O30.DIR_OFFSETY[_pushD];
if (getTile(_pushX + O30.DIR_OFFSETX[_pushD], _pushY + O30.DIR_OFFSETY[_pushD]) != 0) {
removeEventListener(Event.ENTER_FRAME, animateBox);
setTileValue(_pushX, _pushY, _pushBox.getTileValue());
_pushBox.visible = false;
}
}
}


Previous page
Chapter 30 Page 5

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2009 Blazing Games Inc. All Rights Reserved