Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Ball: Revamped game, and Flash.

March 14th update: part 5.3 released.
March 3rd update: part 5.2 released.

Well, since lots of readers commented and mailed me part 6a is too hard and I should split it in pieces, that’s what I am going to do. This is the part 5.1, to be read after part 5.

Of course you should read parts 1 to 5 if you didn’t already.

No walls and speed limit

Well, until now we have always seen the stage perimeter limited by walls. What if there are no walls?
Without walls, when the player exits to the top, he should appear from the bottom, if he exits to the right he should appar from the left, and so on.

You can imagine that if you leave the player in a “free fall” or move him in the same direction for a long time, he may gain a lot of speed, making the game unplayable.

So we have to limit the speed if we want to remove walls.

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	if (xspeed>15) {
		xspeed = 15;
	}
	if (xspeed<-15) {
		xspeed = -15;
	}
	if (yspeed>15) {
		yspeed = 15;
	}
	if (yspeed<-15) {
		yspeed = -15;
	}
	_y = _y+yspeed;
	_x = _x+xspeed;
	if (_x<0) {
		_x += 500;
	}
	if (_y<0) {
		_y += 350;
	}
	if (_y>350) {
		_y -= 350;
	}
	if (_x>500) {
		_x -= 500;
	}
	_rotation = _rotation+xspeed;
}

Lines 25-36: Controlling that xspeed and yspeed range between -15 and 15

Lines 39-50: Controlling when the hero exits from the stage (in this case the values are equal to movie size, but they may differ if you plan to put a score bar or something else).

Invulnerability

Sometimes we do not want the player to die twice (or more) in a couple of seconds… it would be too frustrating… the solution is to grant a limited time when the player is invulnerable after his death.

We need a flag that determines invulnerability. I decided to use the _alpha because I want to show the player transparent when it is invulnerable, but of course you can use a variable of your choice.

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	if (xspeed>15) {
		xspeed = 15;
	}
	if (xspeed<-15) {
		xspeed = -15;
	}
	if (yspeed>15) {
		yspeed = 15;
	}
	if (yspeed<-15) {
		yspeed = -15;
	}
	_y = _y+yspeed;
	_x = _x+xspeed;
	if (_x<0) {
		_x += 500;
	}
	if (_y<0) {
		_y += 350;
	}
	if (_y>350) {
		_y -= 350;
	}
	if (_x>500) {
		_x -= 500;
	}
	_rotation = _rotation+xspeed;
	if (_alpha<100) {
		_alpha += 0.5;
	}
	if ((_root.wall.hitTest(_x, _y, true)) and (_alpha>=100)) {
		xspeed = 0;
		yspeed = 0;
		_x = 30;
		_y = 30;
		_alpha = 10;
	}
}

Lines 52-54: if the _alpha is less than 100 (fully opaque), it increases the _alpha by 0.5

Ok, but when will alpha be less than 100?

Line 55-61: if the player hits the object instanced as wall and the player’s _alpha is less than 100, he moves to his starting position and his x and y speed are set to 0. Moreover, his _alpha is set to 10.

So, when the game starts, the player as _alpha to 100. If the player hits the wall, he dies and his _alpha is set to 10.

Since the _alpha is less than 100, it’s increased by 0.5… _alpha now is 10.5, then 11, 11.5 and so on until 100.

If the _alpha is still less than 100 (so the player died some seconds ago) ant the player hits the wall, he doesn’t die because his alpha is still less than 100.

That’s what I call “invulnerability”

now, a new feature not covered until now…

Scrolling

Let’s imagine a laaarge stage. How can you explore it? Easy, with the scrolling.

How can we manage scrolling? It’s easy, we just keep the player fixed in the center of the movie, and move the background.

In order not to making it too CPU expansive, I suggest you to draw the background in a 1/10 scale, then resize it once on the stage.

Look at the actionscript:

onClipEvent (load) {
	yspeed = 0;
	xspeed = 0;
	wind = 0.00;
	power = 0.65;
	gravity = 0.1;
	upconstant = 0.75;
	friction = 0.99;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		xspeed = xspeed-power;
	}
	if (Key.isDown(Key.RIGHT)) {
		xspeed = xspeed+power;
	}
	if (Key.isDown(Key.UP)) {
		yspeed = yspeed-power*upconstant;
	}
	if (Key.isDown(Key.DOWN)) {
		yspeed = yspeed+power*upconstant;
	}
	xspeed = (xspeed+wind)*friction;
	yspeed = yspeed+gravity;
	_root.wall._y -=yspeed;
	_root.wall._x -=xspeed;
	_rotation = _rotation+xspeed;
	if (_root.wall.hitTest(_x, _y, true)) {
		xspeed = 0;
		yspeed = 0;
		_root.wall._x = 2500;
		_root.wall._y = 1681;
	}
}

The only difference is on lines 25 and 26: the player does not moves, the wall does it!! When in previous examples the player moved to the left, now we move the wall to the right, when the player moved down, we now move the wall up, and so on.

When the player hits the wall (line 28) we do not reset hero’s position but wall position (lines 31-32).

Can you run through all the tunnel?

I hope scrolling opened you mind about some new game concepts.

Download the full examples, give me feedback and remember to send me your games, if made starting from one of my tutorials.

Then, continue wit part 5.2

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