First i would like to point out that knowing Actionscript isn't near enough to make a good
game.
Even with little AS experiance you can make a killer game.
Its all about how you apply it.
You have to think out of the box, if you do then this little Tutorial will set you up.
Well i'm going to start by telling you all that personally i don't like Coding inside of
movieclips.
There are some times when puttin code onto a movie clip is best, even then i will give it
a damn good try to convert that easy (on the movieclip) code to on frame code.
The reason i like to code this way is because it keeps all the code in one tidey place.
If you have code branching off into many MC's and then MC's inside MC's
like i once did you will soon have stuff go wrong.
The reason is you forget where you put some code, and if you have some code that it
counter-acting the code you just made and you are unable to find it.
You're screwed.
So i will be teaching you to AS on frames.
Contents
Ok so today we will be making a simple RPG style game.
This is because i have no imagination at the moment.
So first we will make the code for the main character, we will start by putting in a new
layer and calling it "action script".
The frames on this layer is where we will do all the coding.
Now we need to make a movieclip for your main character.
I am of course presuming you have flash experience.
so now we have our MC(movie clip) set up we need to give it an instance name.
So we go down into the properties box and click on where it says instance.
now we will make the instance "mainCharacter" without quotes.
As you can see the first word of our instance in lowercase then every word after uses a
capital letter to distinguish between words.
This is common programing technique.
Now lets talk about the instance abit.
It is used to refer to the object in the code that you write.
So when you need to manipulate the object the path will be _root.mainCharacter
Just like my documents is c:/documents and settings/user/my documents
So we use this in everyday computing just without knowing.
Ok back to the game.
Now we are ready for the code that will make the man move.
I will comment on it as well as i possibly can and hopefully make it so anyone can
understand it.
stop() // stops the animation from going to the next frame.
speed = 15
// sets the speed variable (a variable can store one piece of information for later use in
the code.)
function onEnterFrame(){
// ok what this does is call the function. the function is "onEnterFrame" which basically
means, when the frame is entered run this code.
if(Key.isDown(Key.UP)){
// if the key "UP" is down then run the code that follows.
_root.mainCharacter._y -= speed
//the mainCharacter MC is then moved 15 pixles (the speed variable set at the begining)
everytime the frame is played. which depends on your fps
}
// end if
if(Key.isDown(Key.DOWN)){
mainCharacter._y += speed
}
// end if
[COLOR=DarkOrange]if(Key.isDown(Key.LEFT)){
mainCharacter._x -= speed
}
//end if
if(Key.isDown(Key.RIGHT)){
mainCharacter._x += speed
}
// end if
}// end function
Ok well we can test that code now.
Its best if you type it in manually, this way you get a feel for the code and it also helps
the learning progress.
Ok its typed in?
Now we can test it.
In my example ive put an object in so that you can see our man moving.
[SWF="http://img276.imageshack.us/img276/70/example9vv.swf"]
ck.us/img276/70/example9vv.swf>
ck.us/img276/70/example9vv.swf allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />
Ok so we have our main character.
And he moves.
Time to make a “portal” that he can hit and travel to a different place (frame).
So now we make a new movieclip that looks like some kind of Door, this will be the Object
that must be hit by our main character to make him travel to the next frame.
So now you have made a movieclip that looks like a door we must set its instance.
I will be using "door" and any number between 0 and 999 (like door22)
And because we want more than one door we will be using a "for" statement.
A for statement is basically a way of saying (in a language the computer can read)
"for when this is happening carry out this action"
You must add this between the last two }'s so its like this:
}
//code
}
for(i=0;i<99;i++){
// ok so this is kind of saying "every time this code runs "i" starts off as 0 and as long
as its less than 99 then add 1 onto "i" (++ basically means adds one)
if(mainCharacter.hitTest("_root.door" + i)){
// if our maincharacter hits the door + i which we know can be anything from 0 - 99 then
play the rest of the code. (an example of door + i would be door22)
door = eval(“_root.door" + i)
// This expression makes the "door" variable equal door+i so you hopefully know that means
that door now means anything from door0 to door999
}
//end if
}
//end for
if(mainCharacter.hitTest(door)){
//if mainchracter hits door (anything from door0 to door999) then goto from 2 and stop.
GotoAndStop(2)
// frame 2 is the next level.
}
//end if
Here is the code without any comments for copy and paste easyness
However i really don't think you can learn properly unless you type it all out manually.
stop();
speed = 15;
function onEnterFrame() {
if (Key.isDown(Key.UP)) {
_root.mainCharacter._y -= speed;
}
if (Key.isDown(Key.DOWN)) {
mainCharacter._y += speed;
}
if (Key.isDown(Key.LEFT)) {
mainCharacter._x -= speed;
}
if (Key.isDown(Key.RIGHT)) {
mainCharacter._x += speed;
}
for (i=0; i<99; i++) {
if (mainCharacter.hitTest("_root.door"+i)) {
door = eval("_root.door"+i);
}
if (mainCharacter.hitTest(door)) {
gotoAndStop(2);
}
}
}
So we got walking done, and the next level hitTest.
Hmm what else is needed in a RPG?
I guess a inventory system might be usefull.
So maybe we want our character to pick up items on his travel through our game.
So what will the specs of out inventory be?
1)when an item is hit by our maincharacter it will disapear from the screen
2)it will go to a boz in the corner so we can see its been collected.
First draw the item and make it a movieclip
And name its instance “sword” as i did in the screenshot,
Now we need to make a box that it will go into when hit by our mainCharacter.
Make a movieclip of a box and call it “box” as i have bellow:
Also make sure that the little “x” that indicates where the 0,0 axis is is in the middle like shown bellow:
Ok now for the code.
We will put this code between the last “}”
e.g.
}
//code
}
As always i will post the code twice, once with the comments once without (for the copy and pasters)
if(mainCharacter.hitTest(_root.sword)){
// if our maincharacter hits the sword then run the next piece of code. Otherwise don't.
sword._y = box._y
// the sword y axis equals the same as the box's y axis therefore making them both be in the same place on the screen.
sword._x = box._x
// the sword x axis equals the same as the box's x axis therefore making them both be in the same place on the screen.
}
That was an extreamly simple inventory system.
Wasnt to hard AS wise but it was still a very effective method.
The format when using keycode and an if statement would be:
if(Key.isDown(keynumber)){
//when the key is down run rest of code.
And finaly
I hope you all enjoyed my tut... this makes up or all the spamming ive done lol.
Anyways expect the next one soon.
Its a series :P
Edit: also sorry tommo my man, but if i made the platformer tut in this one aswell i would have never finnished it.
My next tut will be a platform one.
yea nice work...im pretty much good with AS(your better but...)so i understand the code...but to the newbs, this will help them alot...you really do put your AS together good.... i do get a little confused abot some things but your code was clean and non-confusing...lol
I just really wanted to show people how to use frame AS instead of the Movieclip AS.
Also i wanted to make sure that the code was easy to understand...
I find im not a very good explainer, im glad this seems to have came out ok.
Anyways time to make it look pretty with colours and stuff :P
no not in that syntax.....no....but there is a way to do it...im a little busy at the moment so i cant give you the code but if frankeh doesnt get it then i will eventaully....lol