Be a Supporter!

AS:Helicopter Game

  • 1,566 Views
  • 12 Replies
New Topic Respond to this Topic
Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
AS:Helicopter Game 2007-03-15 00:33:23 Reply

AS:Main

Due to the length of this tutorial(like wtf),I will be splitting it into 2 separate parts,just be aware of that.

Ok,well I wanted to make a helicopter game for a while,and the codes I tried to make all didn't work until I found out th KeyCode '1' is the Left Click on mouse,which allowed me to make a helicopter game script,and I shall share it with all you good folk.Yay :D

So here we go...

First of all,I want to post an EXAMPLE.I only have 2 blocks,and they aren't that great,and no,the helicopter is not something I made,it's just a Google image.

Now I want to post the scripts and where they will be put into.First,make a second layer,name it ActionScript or whatever you want,and then make 2 empty KeyFrames on each layer.In the first ActionScript Keyframe,post this code...

stop();
_root.dist2.selectable = false;
onMouseDown = function () {
gotoAndStop(2);
};
_root.dist2 = _root.dist;

In the second ActionScript KeyFrame,post this code...

stop();
dist = 0;
_root.dist.selectable = false;
onEnterFrame = function () {
_root.dist++;
};

Ok now in frame 1,write some text that says 'Click to Play'.Then make a dynamic text box and type 'dist2' into the Var box(it's down in the properties section).You can also add the helicopter or something without actions in it to decorate the frame a bit like in the Helicopter game(the really good version).

Ok,now go to the second frame again and make a helicopter MovieClip if you haven't already,and place it somewhere on the Left side of the stage.Add this code to the Helicopter...

onClipEvent (load) {
var grav:Number = 5;
var spe:Number = 0;
}
onClipEvent (enterFrame) {
this._y += grav;
if (Key.isDown(1)) {
_rotation = -5;
this._y -= spe;
grav--;
spe++;
} else {
_rotation = 10;
spe--;
grav++;
}
if (this._y<0) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}
if (this._y>400) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}
if (this.hitTest(_root.block)) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
removeMovieClip(_root.block3);
}
if (this.hitTest(_root.block1)) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
removeMovieClip(_root.block3);
}
}

Also,make another dynamic text box in the second KeyFrame and make the Var 'dist'.It make be a good idea to make a couple of rectangles at the top and bottom of the stage so the person knows there are boundaries.Now,make 1 block MovieClip and add this code to it....

onClipEvent (load) {
var spe2:Number = 10;
i = 2;
}
onClipEvent (enterFrame) {
this._x -= spe2;
if (this._x<0) {
_root.block._y = random(Stage.height);
duplicateMovieClip(_root.block, "block2"+i, i);
_root["block2"+i]._x -= spe2;
_root["block2"+i]._height = random(90);
this._x = 573.4;
}
}

Make sure the instance name in this MovieClip is 'block',otherwise it won't work.Now make a second Block MovieClip and add this code to it....

onClipEvent (load) {
var spe3:Number = 6;
i = 2;
}
onClipEvent (enterFrame) {
this._x -= spe3;
if (this._x<0) {
_root.block._y = random(Stage.height);
duplicateMovieClip(_root.block1, "block3"+i, i);
_root["block3"+i]._x -= spe3;
_root["block3"+i]._height = random(90);
this._x = 638.3;
}
}

Be sure that the instance name of this block is 'block1'.

Now,if you want to be cool,you would learn about these scripts so you can code one like it yourself :D


wat
a compessor wil raise the volume while lowering the db - chronamut

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS:Helicopter Game 2007-03-15 00:34:41 Reply

AS:Helicopter Game(continued)

So the frame actions are pretty simple.

stop();
_root.dist2.selectable = false;
onMouseDown = function () {
gotoAndStop(2);
};
_root.dist2 = _root.dist;

What this code does is it Stops the Movie on that frame,makes it so the dynamic textbox 'dist2' is not selectable so you can't get the little text symbol when you scroll over it(like Static Text),goes to the second frame when the Mouse is clicked,and sets the text 'dist2' to equal the same number/text as 'dist'.

And the second frames code....

stop();
dist = 0;
_root.dist.selectable = false;
onEnterFrame = function () {
_root.dist++;
};

This code also stops the Movie and makes the dynamic text 'dist' unselectable,but it also has dist=0 when you first get on the frame.Then after that,it makes 'dist'++,and if you read the Math tutorials,++ adds 1 every frame(or something).

Now,we need to move on to a bit more complicated codes,but not by much.Here is the helicopter code....

onClipEvent (load) {
var grav:Number = 5;
var spe:Number = 0;
}

This script is for when the frame loads.It will create variables named 'grav' for gravity and 'spe' for speed.The speed variable will change,and so will the gravity one,don't worry about that.

onClipEvent (enterFrame) {
this._y += grav;
if (Key.isDown(1)) {
_rotation = -5;
this._y -= spe;
grav--;
spe++;
} else {
_rotation = 10;
spe--;
grav++;
}

Ok,so this is for when you enter the frame(or something).At first,it will make the helicopter fall down(_y- in this case)at the speed of gravity.Now,if the Key '1' is down(Keycode 1 = Left Mouse Click),then the 'grav' variable will decrease so you won't fall as fast,and the 'spe' variable will increase,along with sending it _y+ so it will go up.It also rotates the MovieClip -5,so it will appear to face up.

The 'else' is for if the Key '1' isn't pressed,so rotation will = 10(to face down),'spe' will -- (decrease by 1),and 'grav' will increase so you will fall again.

Now we will set some boundaries on your helicopter so you can't fly too high or fall too low.

if (this._y<0) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}
if (this._y>400) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}

Basically,this code will test to see if the helicopters _y is greater than(>) or less than(<) a certain point on the stage.My points will be 0(very top of stage) and 400(very bottom of stage).Height is determined by _y.Width is determined by _x,but we don't have _x boundaries because our helicopter really won't be moving left or right,just up and down.

if (this.hitTest(_root.block)) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
removeMovieClip(_root.block3);
}
if (this.hitTest(_root.block1)) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
removeMovieClip(_root.block3);
}
}

These codes are pretty much the same.They just hitTest to see whether or not if block or block1 are touching the helicopter.If either one of them is touching our beloved helicopter,then we assume the helicopter crashes,and it sends it back to frame 1.It will also remove the MovieClips that were duplicated(code not explained yet)so they won't continue to go across the screen when you die.No worries,they will reset.

Now for the last part,the blocks(yay,finally!)These just require some duplicating,which isn't too hard in some cases.I will only have to use one of the scripts because they are both the same,and I am running low on characters in this post(sorry for the extensive tutorial on something so basic :\)

onClipEvent (load) {
var spe2:Number = 10;
i = 2;
}
onClipEvent (enterFrame) {
this._x -= spe2;
if (this._x<0) {
_root.block._y = random(Stage.height);
duplicateMovieClip(_root.block, "block2"+i, i);
_root["block2"+i]._x -= spe2;
_root["block2"+i]._height = random(90);
this._x = 573.4;
}
}

So,this codes beginning is similar to the helicopter.It sets a speed variable of 10(I chose to change the speed in both of my blocks.I also changed the _x locations).So it will set a speed,but it also sets i to equal 2.'i' is going to be our depth.So,what this code will do is first send our block on its way to the left,but when it hits the _x of 0,it will be sent back to the _x of 570(the right,where it started).It's _y location will be randomized,and it will be duplicated.The first part of duplication is what is being duplicated,the second part is the new name of the duplication+i,and the last part is i(which is the depth).So now the block will once again be sent to the left and keep repeating,but there is a chance the height will be different because of the random code.It will randomize the height of the new block.

Well that is all,hope you learned something :D


wat
a compessor wil raise the volume while lowering the db - chronamut

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS:Helicopter Game 2007-03-15 00:41:48 Reply

Oh uhh there are a couple of errors that shouldn't be there...

Here is the new helicopter script.

onClipEvent (load) {
var grav:Number = 5;
var spe:Number = 0;
}
onClipEvent (enterFrame) {
this._y += grav;
if (Key.isDown(1)) {
_rotation = -5;
this._y -= spe;
grav--;
spe++;
} else {
_rotation = 10;
spe--;
grav++;
}
if (this._y<0) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}
if (this._y>400) {
_root.gotoAndStop(1);
removeMovieClip(_root.block2);
}
if (this.hitTest(_root.block)) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
if (this.hitTest(_root.block1)) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
}

Missing the damn _root :(


wat
a compessor wil raise the volume while lowering the db - chronamut

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS:Helicopter Game 2007-03-15 00:57:04 Reply

Sorry for 4 posts,but I have the final code for the helicopter.I forgot the problems with the _y boundaries...

onClipEvent (load) {
var grav:Number = 5;
var spe:Number = 0;
}
onClipEvent (enterFrame) {
this._y += grav;
if (Key.isDown(1)) {
_rotation = -5;
this._y -= spe;
grav--;
spe++;
} else {
_rotation = 10;
spe--;
grav++;
}
if (this._y<0) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
if (this._y>400) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
if (this.hitTest(_root.block)) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
if (this.hitTest(_root.block1)) {
_root.gotoAndStop(1);
_root.removeMovieClip(_root.block2);
_root.removeMovieClip(_root.block3);
}
}

I promise this is the last edit unless someone asks me...


wat
a compessor wil raise the volume while lowering the db - chronamut

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS:Helicopter Game 2007-03-15 01:11:45 Reply

the example is buggy and crappy. and you dont even have a dynamic floor and cieling. i question your ability as a developer. i dont think your ready to make tutorials

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS:Helicopter Game 2007-03-15 01:13:22 Reply

Well it is just a quick code I thought of.Not something I care about that much,so it doesn't really bother me too much...


wat
a compessor wil raise the volume while lowering the db - chronamut

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS:Helicopter Game 2007-03-15 01:32:19 Reply

At 3/15/07 01:13 AM, Thomas wrote: Well it is just a quick code I thought of.Not something I care about that much,so it doesn't really bother me too much...

... then why spam the bbs with it and make 4 huge posts that scream "this is how you make a helicopter game!"

dont try to act all hard core like you dont care because you do, you're obviously proud of this code. whichive got nothing wrong with. i just think you have a lot to learn before teaching someone actionscript

Thomas
Thomas
  • Member since: Feb. 14, 2005
  • Offline.
Forum Stats
Member
Level 13
Musician
Response to AS:Helicopter Game 2007-03-15 01:40:45 Reply

At 3/15/07 01:32 AM, ImpotentBoy2 wrote: blah

...k :(

Back to learning stuff!


wat
a compessor wil raise the volume while lowering the db - chronamut

Pauly
Pauly
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS:Helicopter Game 2007-03-15 01:44:41 Reply

Well at least you tried.Maybe your AS skills aren't the best but at least your trying to help out some people.

GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to AS:Helicopter Game 2007-03-15 01:47:52 Reply

his heart is in the right place

Pauly
Pauly
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS:Helicopter Game 2007-03-15 01:49:47 Reply

At 3/15/07 01:47 AM, ImpotentBoy2 wrote: his heart is in the right place

Yea exactly.Keep working them skills and show NG what you've learned.

johnfn
johnfn
  • Member since: Aug. 16, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS:Helicopter Game 2007-03-16 18:36:41 Reply

At 3/15/07 01:11 AM, ImpotentBoy2 wrote: the example is buggy and crappy. and you dont even have a dynamic floor and cieling. i question your ability as a developer. i dont think your ready to make tutorials

I don't understand why you're flaming someone for attempting to contribute. At least compliment him on the effort. You are a good reason why the NG community needs some major work.


[6,11,4,10,2,10,-68,5,15,-68,16,4 ,1,-68,-2,1,15,16,-67].map(function(v){ return String.fromCharCode(v + 100) }).join(""); // updated for web 2.0!

BBS Signature
FlightOfTheConchords
FlightOfTheConchords
  • Member since: Nov. 20, 2007
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS:Helicopter Game 2007-11-20 14:53:03 Reply

hey i just wanted to say thanks for posting this, its the only one i could find that was actually complete.
but i just had a question for you, for some reason when my copter gets past the first two blocks, all the blocks start changing sizes and repeating at the wrong time. also, it works fine if i hit the top, bottom, or "block", but when i hit "block1" it goes back to frame 1, but blocks are still going by in the back, any suggestions?