Tutorial: Create a Platform Game in AS2 – Part 2
Categories: Flash
Table of Contents
Step 2: Creating the Level
Now, we have to set up blocks on stage that will account for a level. We’ll use an array to accomplish this manly feat. We’re also going to create some other level variables. Place this code at the top:
//LEVEL VARIABLES
//the current lvl
var lvlCurrent:Number = 1;
/*The key for the level arrays:
1: Regular Block
X: Main Character
*/
//this variable will hold the character
var X:String = 'MAIN';
//the array for level 1
var lvlArray1:Array = 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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
);
This is a pretty big array, but it won’t be too hard to understand, I hope. The next thing we have to do is to actually make a function which creates the level. We’ll make some more variables for this as well:
//current row that we are creating
var row:Number = 0;
Next, we have to create a MovieClip that will act as the block. I’m just going to make it a 25×25 white square without borders. Name it mcBlock, then export it for ActionScript:
Finally, we can do some more coding. Now, we have to define the function that will place the bricks on stage. Place the following code at the end of the frame:
//creating the level
//this empty movieclip will hold all of the blocks
_root.createEmptyMovieClip('blockHolder',_root.getNextHighestDepth());
function createLvl():Void{
//finding the array of the current level we're on
//this is just a way to dynamically select an Array within the document
var lvlArray:Array = _root['lvlArray'+lvlCurrent];
//we have to find how far this level will span
//this will be used so we know when to move to the next row
//there will always be 16 rows, so this is how we find it out
//of course, this will make the level formatting very strict
var lvlColumns:Number = Math.ceil(lvlArray.length/16);
//now we can create the level
for(var i:Number=0;i
This is some pretty intense code. Look it over for a bit and try to comprehend it. This type of code is what you need to make if you want to become a better developer.
This concludes this part of the tutorial. Next time, we'll add some code to these blocks and make them interact with the character. Stay tuned...
Final Product
hmm… mines not working… when i play it after part 2 theres no blocks and if you jump he falls out of the screen
January 10th, 2009 at 8:28 pm
I have the same problem as Evan…
January 20th, 2009 at 12:45 pm
worked fine for me
March 15th, 2009 at 5:24 pm
I have the same problem with Craig and Evan….soz..
April 5th, 2009 at 10:33 am
How do you make a key for an array with different mcs in it
May 10th, 2009 at 8:32 pm
It falls through, cause in this part there is no collision detection code implemented. From part 3 onwards everything works good.
I made a game slightly based on this tutoral, and it works great.
Big thanks mr Sun.
May 31st, 2009 at 1:53 pm
do u create all of this on another flash document? or…can someone tell me WHERE everything goes…i finished step 1 properly…like i can move and jump…im just stuck at this step
May 31st, 2009 at 4:39 pm
Nope. If You follow this tutorial then You should make the changes in the document You already created. (that’s what I did anyway when I was first trying it out).
It’s explained properly where You should make changes in this tutorial,
but You can also download a source if You still dont understand.
June 2nd, 2009 at 8:56 am
same prob as evan.
July 24th, 2009 at 4:43 am
why is the colour of the text different to mine i even copied it but still blue… e.g this bit is green var X:String = ‘MAIN’; and not red like yours ? and the lvl array bit like 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
that bit is just all black
ps i have adobe cs4 flash
August 11th, 2009 at 2:58 pm
On the “finished product” on this page, it wont stay on the blocks
September 28th, 2009 at 4:45 pm
Hello. I can solve your problems with the collision on the box + move + jump.. Here is the script i came up with after an hour or two:
var mainJumping:Boolean = false;
var jumpSpeedLimit:Number = 15;
var jumpSpeed:Number = jumpSpeedLimit;
var yspeed = 1;
var gravity = 1;
var jump = -8;
onEnterFrame = function () {
mcMain._y += yspeed;
yspeed += gravity;
if(mcMain.hitTest(mcBlock)){
if(Key.isDown(Key.UP)){
yspeed = jump;
} else {
yspeed = 0;
}
}
}
Make that on the layer. And now is the thing. Go to the ball / mcMain (select the ball in actionscript) And when you have the box where you can write in the script then write in this:
onClipEvent (load) {
power = 6;
radius = 6;
_x = 290;
_y = 250;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
_x -= power;
}
if (Key.isDown(Key.RIGHT)) {
_x += power;
}
}
I hope that i solved your problems.
October 1st, 2009 at 3:20 pm
Btw there is a small thing in the mcMain script. I have set cordinates on the script where the circle (Mcmain)
_x = 290;
_y = 250;
It puts the circle (mcMain) into a certain place in the game, you can change the cordinates (the numbers) or delete it 🙂 Have a nice game
October 1st, 2009 at 3:41 pm
mine just dosint genarate at all
March 6th, 2010 at 9:01 pm
so i type in the code exactly, and every time i try it out, my computer freezes up
April 21st, 2010 at 11:47 am
[…] I would like to give a little credit to Emanuele Feronato for his many AS3 blog posts and to this Mr Sun Studios post on storing level […]
May 4th, 2010 at 2:30 am