I quote a brillianbt man when I say "its better then sex with a n00b"
AS: Game part 1
I was thinking of an AS: to do and couldnt really find anything that hasnt already been covered (Damn you inglor making everything so good!).
Anyways I though I might go ahead and make a little project to do using the AS: topics as help. In the end of this we will be making a nice little game using many of the topics covered in AS: MAIN and hopefully will give everyone a good understanding on practical uses of the AS: MAIN topics.
To start...
so lets begin shall we?
For starts make a hero. Draw him and then convert him to a movieClip. Lets give him the instance name hero.Also make sure his registration point is in the middle of his x axis at his feet. Also make sure he is facing right side on.Now in this tutorial we wont bother with animating him (because this is a purely AS tutorial and I dont want to waste much time). It should be easy for you to slot the animation code in anyways.
Type the following code into the actions panel
onClipEvent (enterFrame) {
}
Im sure we know what this does if not then this should be useful.
"Moving" on
Okay to make our guy move we are going to make a function! If you are unsure with these please read this first.
On the main time line put the following code
function hero_move(target, speed):Void {
with (target) {
_x -= Key.isDown(Key.LEFT)*speed-Key.isDown(Key.
RIGHT)*speed;
}
}
Basically what this does is makes a new function called hero_move . The parameters for the function are target and speed. There is no return. When the function is run it tells the target to respond to this.
_x -= Key.isDown(Key.LEFT)*speed-Key.isDown(Key.
RIGHT)*speed;
If you are unsure of binary increasement then read
this .
Now we have our function written we can go and call it from our hero.
Yey finally something that works
Thats right we are about to have amoving man (WOOPEDY DOO). Go onto the hero and replace his current code with the following:
onClipEvent (enterFrame) {
_level0.hero_move(this,10);
}
Dun dun dun OMG it is soooo complicated. Basically it just calls the function and sets the target to this (itself) and speed to 10. I think setting speed to be 10 all the time kind of eliminates the point of having the variable even accessable as a parmeter so lets make that a variable. Replace the code with this:
onClipEvent (load) {
var speed:Number = 10;
}
onClipEvent (enterFrame) {
_level0.hero_move(this, 10);
}
if you are unsure of variables and how they work here is a VERY vague explanation of them.
(I cant be assed with HTML ANYMORE)
http://www.newground../topic.php?id=297928
Now you can press Ctrl + ENTER and voila a moving man! (left and right arrows)
Sure hes a moving man but hes not very interesting yet is he?
A step UP!
Now here is where it gets WILD!
Lets make our little guy jump!
Another function I think...
On the main time line (again frame 1) put the following:
function hero_jump(target, height):Void {
with (target) {
if (Key.isDown(Key.UP)) {
if (!jumping) {
gravity = height;
jumping = true;
}
}
if (jumping) {
this._y -= gravity;
gravity--;
if (gravity == -height) {
jumping = false;
}
}
}
}
This is again pretty straight forward and all you need do is read it carefully.
So now we have our hero_jump function we can call it in our little man...
add this to his onClipEvent...
_level0.hero_jump(this, height);
and this to the onLoad
var height:Number = 20
horrah we have an all moving all jumping man! But wait...
on testing the movie dont you find it appauling that he jumps and moves oh so very slowly and jaggedly...
Just set the fps to 30fps and we're good to go!
Beutiful. Well no. Where the hell is the floor?!
Getting "on top" of things
Well now we have our beloved man and his super powers of jumping! Wait a minuit... Where is the floor?!
Ever heard of a hitTest? Well if not then you should seriously think about reading this.
http://www.macromedi..t_dictionary534.html
So lets make a floor. We could make a naf flat floor (no alliteration intended) but that is boring. Lets draw a nice curvey line for a floor. Here is an excellent tutorial covering most of what we are about to do.
http://www.newground../topic.php?id=293653
<-------- BRILLIANT EXAMPLE
Draw your wavy floor (not too steep waves) and make the instance name floor *shocking*.
(obvously on the main stage) Now here is where you MUST be careful. Go into the hero MC and draw a little square right at his feet. Convert it into and MC and give it the instance name feet.
Now add this function to the timeline:
function hero_land(target):Void {
with (target) {
while (_root.land.hitTest(point.x, point.y, true)) {
_y -= 1;
point.y -= 1;
}
_y += 10;
var point:Object = new Object();
point.x = feet._x;
point.y = feet._y;
localToGlobal(point);
}
}
If you dont know what is going on here read the link I mentioned above and for further info on the while loop read this.
http://www.newground../topic.php?id=311428
or
http://www.newground../topic.php?id=296699
for all the loops (almost)
Now put the following code into the onEnterFrame of the hero...
_level0.hero_land(this);
OMG that is smarts!!!
okay now you have an all jumping all landing all cooly hero
well you can mess around with the variables and stuff whilst I write the next part of this tutorial please ask any questions / critisms below and please I like praise heh.
Coming soon -- next part of this tutorial
LINKS
the one useful link to rule them all
A PLUG
A bookmark
And most importantly
THE LINK
full chars too