Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Platform game game, Flash and Game development.

To show you how hard I am going to try wiping the vaporware out of my life, I am publishing the 3rd part of the platform game tutorial.

Read parts 1 and 2 if you don’t remember what I am talking about.

In this update, I introduced two new tile types: the lava tile that kills you when you walk over it (read: when you walk over it, not when you touch it), and the horizontally moving tile, that automatically (read: automatically) connects two spots.

The code needs to be cleaned, but everything seems to work

// player default speeds
xspeed = 0;
yspeed = 0;
max_yspeed = 10;
walk_speed = 4;
climb_speed = 2;
// am I climbing?
climbing = false;
// am I jumping?
jumping = false;
// can I jump?
can_jump = true;
// gravity & jump settings
gravity = 1;
jump_power = 10;
walking_while_jumping = true;
// level creation
level = new Array();
_root.createEmptyMovieClip("lev", _root.getNextHighestDepth());
_root.createEmptyMovieClip("lad", _root.getNextHighestDepth());
_root.createEmptyMovieClip("lava", _root.getNextHighestDepth());
_root.createEmptyMovieClip("moving", _root.getNextHighestDepth());
level[0] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
level[1] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[2] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[3] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[4] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[5] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1);
level[6] = new Array(1, 0, 0, 0, 0, 0, 1, 1, 1, 4, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[7] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[8] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[9] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[10] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[11] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[12] = new Array(1, 0, 0, 0, 0, 0, 0, 1, 3, 1, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 1);
level[13] = new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
level[14] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 1, 1, 1, 1, 1);
for (y=0; y<=14; y++) {
	for (x=0; x<=24; x++) {
		if (level[y][x] == 1) {
			place_brick = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			place_brick.gotoAndStop(level[y][x]);
		}
		if (level[y][x] == 2) {
			ladder_brick = lad.attachMovie("block", "block_"+lad.getNextHighestDepth(), lad.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			ladder_brick.gotoAndStop(level[y][x]);
		}
		if (level[y][x] == 3) {
			lava_brick = lava.attachMovie("block", "block_"+lava.getNextHighestDepth(), lava.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			lava_brick.gotoAndStop(level[y][x]);
		}
		if (level[y][x] == 4) {
			moving_brick = moving.attachMovie("block", "block_"+moving.getNextHighestDepth(), moving.getNextHighestDepth(), {_x:x*20+10, _y:y*20+10});
			moving_brick.gotoAndStop(level[y][x]);
			moving_brick.dir = 1;
			moving_brick.onEnterFrame = function() {
				this._x += this.dir;
				if ((_root.lev.hitTest(this._x+10, this._y, true) and (this.dir>0)) or ((_root.lev.hitTest(this._x-10, this._y, true) and (this.dir<0)))) {
					this.dir *= -1;
				}
				if (_root.player.hitTest(this._x, this._y-11, true) or _root.player.hitTest(this._x+5, this._y-11, true) or _root.player.hitTest(this._x-5, this._y-11, true)) {
					player._x += this.dir;
				}
			};
		}
	}
}
_root.attachMovie("player", "player", _root.getNextHighestDepth(), {_x:40, _y:40});
// end of level creation
player.onEnterFrame = function() {
	if (Key.isDown(Key.LEFT)) {
		if (climbing) {
			xspeed = -climb_speed;
		} else {
			if (walking_while_jumping or can_jump) {
				xspeed = -walk_speed;
			}
		}
	}
	if (Key.isDown(Key.RIGHT)) {
		if (climbing) {
			xspeed = climb_speed;
		} else {
			if (walking_while_jumping or can_jump) {
				xspeed = walk_speed;
			}
		}
	}
	if (!feet_on_ladder()) {
		climbing = false;
	}
	if (Key.isDown(Key.UP)) {
		if (feet_on_ladder()) {
			yspeed = -climb_speed;
			climbing = true;
			jumping = false;
		}
	}
	if (Key.isDown(Key.DOWN)) {
		if (feet_on_ladder() or ladder_under_my_feet()) {
			yspeed = climb_speed;
			climbing = true;
			jumping = false;
		}
	}
	if ((Key.isDown(Key.SPACE)) and can_jump and !jumping and !climbing) {
		yspeed -= jump_power;
		jumping = true;
	}
	// adjusting y speed                        
	if (!climbing) {
		yspeed += gravity;
	}
	if (yspeed>max_yspeed) {
		yspeed = max_yspeed;
	}
	if (level_under_my_feet() and !jumping and !climbing) {
		yspeed = 0;
	}
	if (ladder_under_my_feet() and !jumping and !climbing) {
		yspeed = 0;
	}
	forecast_x = this._x+xspeed;
	forecast_y = this._y+yspeed;
	// lava control
	if (_root.lava.hitTest(forecast_x, forecast_y+this._height/2-1, true)) {
		forecast_x = 40;
		forecast_y = 40;
		jumping = false;
	}
	// floor control             
	while ((_root.lev.hitTest(forecast_x, forecast_y+this._height/2-1, true)) or (_root.moving.hitTest(forecast_x, forecast_y+this._height/2-1, true))) {
		forecast_y--;
		xspeed = 0;
		yspeed = 0;
		jumping = false;
	}
	// ceiling control
	while (_root.lev.hitTest(forecast_x, forecast_y-this._height/2, true) or (_root.lava.hitTest(forecast_x, forecast_y-this._height/2, true)) or (_root.moving.hitTest(forecast_x, forecast_y-this._height/2, true))) {
		forecast_y++;
		yspeed = 0;
	}
	// left wall control
	while (_root.lev.hitTest(forecast_x-this._width/2+1, forecast_y, true)) {
		forecast_x++;
		xspeed = 0;
	}
	// right wall control
	while (_root.lev.hitTest(forecast_x+this._width/2, forecast_y, true)) {
		forecast_x--;
		xspeed = 0;
	}
	this._x = forecast_x;
	this._y = forecast_y;
	// adjusting speeds for next frame
	xspeed = 0;
	if (climbing) {
		yspeed = 0;
	}
};
function feet_on_ladder() {
	return _root.lad.hitTest(player._x, player._y+player._height/2-1, true);
}
function level_under_my_feet() {
	return _root.lev.hitTest(player._x, player._y+player._height/2, true);
}
function ladder_under_my_feet() {
	return _root.lad.hitTest(player._x, player._y+player._height/2, true);
}

and, more important, I am learning how to fight vaporware plague.

Download the source code and give me the proof I can defeat vaporware.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.