Flash Tutorials

Main | Simple AS | Overhead | Platform | ISO & TBW | Popup Poll | Change the Style | FK Games Forum | Back Home

Mario: A tile based game

Well, mario is shaping up well, but there are obvious problems with it.

Lets fix the first one, it doesn't seem to know when he has hit the ground
and also, he can jump when falling. To fix this, we need to get feedback from
the move function in mario's brain, so that we can tell if he has hit the ground.

This is accomplished by a return. Here's an example of a return:

function five(){
	return 5;
}

MyVar = five();

And now MyVar == 5.

You can be a lot more complicated though. Here's how to find the volume
of a tube:

function tube(r,l){
	var a = Math.PI * (r*r);
	return Math.round(a*l);
}

So, tube(10,1); would return 314 (PI = approx 3.14)

Gotit?

So, now you have an idea about what returns do, lets put it
into practice.

Goto Mario's brain, and find the move function in it. There should
be a comment, now we are falling. Anyway, find the if(y > 0){ it is the
place we will be editing. We need to add a little bit before it:

		var falling = false
 		if(y > 0){ // We are falling
			if(_root.map...

Go down to the very inside of the if's, and we will add a bit there as
well.

		if(y > 0){ // We are falling
			if(_root.map[bot][left] < hardTileMin || _root.map[bot][left] > hardTileMax){
				if(_root.map[bot][right] < hardTileMin || _root.map[bot][right] > hardTileMax){
					_parent._y += y;
					falling = true;
				}
			}

Now, the function knows when it is falling. All we need to do now is
to send it back in a return. Goto the bottom of the function, but make
sure you're not too far down.

 		if(x > 0){
			if(_root.map[top][right] < hardTileMin || _root.map[top][right] > hardTileMax){
				if(_root.map[bot][right] < hardTileMin || _root.map[bot][right] > hardTileMax){
					_parent._x += x;
				}
			}
		}
		return falling;
	}
}

onClipEvent(enterFrame){

That's all great, but we need to use this information somehow.
Goto the start of the onClipEvent(enterFrame){ (down a bit) and
edit as follows:

onClipEvent(enterFrame){
	falling = move(0,4); // Gravity
	
	if(Key.isDown(Key.LEFT)){

So now, we just need to look in the variable falling to see what's going
on. Of course, we need to use this.

First edit, the jumping line, that checks to see it the up key is pressed.

	if(Key.isDown(Key.UP)){
		if(falling == false && jumping == false && UPdown == false){

Now, lil mario will only jump whilst standing on ground. Lets make him 
stop jumping as soon as he hits the ground now, as he is currently showing
the jumping sprite after landing. Lets go right down to the bottom of the
onClipEvent(enterFrame){ and find the if(jumping), and make the following
ammendments:

	if(jumping){
		_parent.gotoAndStop(5);
		vel_y += 1;
		move(0,vel_y);
		if(vel_y>-14 && !falling){
			jumping = false;
		}



Zipped up current mario fla
Current mario fla
Current mario swf

Next
Counter:
Warning: include(../counter.php) [function.include]: failed to open stream: No such file or directory in /home/mack/public_html/flash_tutorials/includes/general.bot.php on line 8

Warning: include(../counter.php) [function.include]: failed to open stream: No such file or directory in /home/mack/public_html/flash_tutorials/includes/general.bot.php on line 8

Warning: include() [function.include]: Failed opening '../counter.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/mack/public_html/flash_tutorials/includes/general.bot.php on line 8

Unless otherwise specified, all textual content here was made by Ed Mack and should only be used for educational purposes. If you are cutting portions out, a credit and/or URL to this site is required. I'm not out there to sue people, but remember, I'm not paid for doing this, so if it gets stolen and put up somewhere else as "Joe Blogs 129"'s work, I may as well give up.