Game Development Reference
In-Depth Information
public function runGame(e:Event):void {
if (playerStarted) {
checkInput();
}
//*** added iteration #4
update();
render();
//** end added iteration #4
}
Adding the update function
The
update
function's primary task is to update the
nextX
and
nextY
attributes of the player (and
other game moving objects). Once updated, these two variables will eventually be used in
collision detection (not implemented until a later iteration).
Here is the full code for this iteration's
update
function. We'll cover the main points of the function
after you have taken a look (and hopefully typed it in).
private function update():void {
//*** added iteration #4
player.nextX = player.x + player.dx*player.moveSpeed;
player.nextY = player.y + player.dy * player.moveSpeed;
if (player.y <yMin || player.y>yMax || player.x<xMin || player.x >xMax){
player.inTunnel = true;
}else {
player.inTunnel = false;
}
if (player.inTunnel) {
switch(player.currentDirection) {
case MOVE_UP:
if (player.nextY == yTunnelMin){
player.nextY=yTunnelMax
}else if (player.nextY == yMax) {
player.inTunnel = false;
}
break;
case MOVE_DOWN:
if (player.nextY == yTunnelMax){
player.nextY = yTunnelMin;
}else if (player.nextY == yMin) {
player.inTunnel = false;
}
break;
case MOVE_LEFT:
if (player.nextX == xTunnelMin){
player.nextX = xTunnelMax;
}else if (player.nextX == xMax) {