Flash game creation tutorial - part 2

March 14th update: part 5.3 released.
March 3rd update: part 5.2 released.
February 9th update: part 5.1 released.
December 31st update: 5th part released.
December 23rd update: 4th part released.
December 6th update: 3rd part released.

Welcome to the second step.

Read the 1st part and you'll be ready to follow me with the game creation.

As you can see, now I can move the hero in a quite funny way.

Now, it's time to introduce some (deadly) obstacles.

The bounds

Every game area has bounds, so I created a movieclip called "bounds" and instanced as "wall".

My hero can't touch any wall, so the actionscript will be:

ACTIONSCRIPT:
  1. onClipEvent (load) {
  2.     yspeed = 0;
  3.     xspeed = 0;
  4.     wind = 0.00;
  5.     power = 0.65;
  6.     gravity = 0.1;
  7.     upconstant = 0.75;
  8.     friction = 0.99;
  9. }
  10. onClipEvent (enterFrame) {
  11.     if (Key.isDown(Key.LEFT)) {
  12.         xspeed = xspeed-power;
  13.     }
  14.     if (Key.isDown(Key.RIGHT)) {
  15.         xspeed = xspeed+power;
  16.     }
  17.     if (Key.isDown(Key.UP)) {
  18.         yspeed = yspeed-power*upconstant;
  19.     }
  20.     if (Key.isDown(Key.DOWN)) {
  21.         yspeed = yspeed+power*upconstant;
  22.     }
  23.     xspeed = (xspeed+wind)*friction;
  24.     yspeed = yspeed+gravity;
  25.     _y = _y+yspeed;
  26.     _x = _x+xspeed;
  27.     _rotation = _rotation+xspeed;
  28.     if (_root.wall.hitTest(_x, _y, true)) {
  29.         xspeed = 0;
  30.         yspeed = 0;
  31.         _x = 120;
  32.         _y = 120;
  33.     }
  34. }

What happened?
I added lines 28 to 33, let's see how do they work.

Line 28: I perform a hit test between the hero and the movieclip called "wall". The _x and _y means that I am checking the collision only on the _x and _y attributes of the hero - its center -
Why am I doing it this way? Because I think the game would be too frustrating if I should check the collision between any pixel of the hero and the game bounds. Moreover, the smallest the collision area (1 pixel in this case), the more complicate and creative the bounds you can draw to make the player move around it.
Lines 29 to 32 simply "reset" hero's position in case of death.

The coin - 1st attempt

Now I want my hero to be able to collect coins or similar things, so I created a new movieclip named and instanced as "coin".
New actionscript is:

ACTIONSCRIPT:
  1. onClipEvent (load) {
  2.     yspeed = 0;
  3.     xspeed = 0;
  4.     wind = 0.00;
  5.     power = 0.65;
  6.     gravity = 0.1;
  7.     upconstant = 0.75;
  8.     friction = 0.99;
  9. }
  10. onClipEvent (enterFrame) {
  11.     if (Key.isDown(Key.LEFT)) {
  12.         xspeed = xspeed-power;
  13.     }
  14.     if (Key.isDown(Key.RIGHT)) {
  15.         xspeed = xspeed+power;
  16.     }
  17.     if (Key.isDown(Key.UP)) {
  18.         yspeed = yspeed-power*upconstant;
  19.     }
  20.     if (Key.isDown(Key.DOWN)) {
  21.         yspeed = yspeed+power*upconstant;
  22.     }
  23.     xspeed = (xspeed+wind)*friction;
  24.     yspeed = yspeed+gravity;
  25.     _y = _y+yspeed;
  26.     _x = _x+xspeed;
  27.     _rotation = _rotation+xspeed;
  28.     if (_root.wall.hitTest(_x, _y, true)) {
  29.         xspeed = 0;
  30.         yspeed = 0;
  31.         _x = 120;
  32.         _y = 120;
  33.     }
  34.     if (_root.coin.hitTest(_x, _y, true)) {
  35.         _root.coin._x = Math.random()*400+50;
  36.     }
  37. }

Look at lines 34-36.
Line 34 performs basically the same test as I did for the bounds, line 35 simply moves the coin to another position once you collect it.
Wonderful.
Now try to collect the coin.

It looks hard and buggy, isn't it?
That's why I am performing the hit test only with the center of the hero, I mean only with one pixel.
While this method was good for the bounds, now it looks awful.

The coin - 2nd attempt

I am trying a different approach, changing line 34 with

ACTIONSCRIPT:
  1. if (_root.coin.hitTest(this)) {

I am extendid the hit test to the entire shape.
Try to collect the coin now...

Now it's too easy!!
You can collect coins just staying close to them. Why?
Because Flash performs that hit test basing on a rectangular shape built around the hero's shape.
Just imagine a bounding box. Everything you hit with the bounding box returns a true value.
While the 1st attempt looked hard and buggy, this one looks easy and buggy. Keyword: buggy.

The coin - 3rd attempt

Are you about to give up? No?
Well, I have the solution.
Just create a new movieclip called "hero_hit" and insert into the hero's movieclip, at its center. That's the small blue square you notice on the movie.
Now perform the hit test on the hero_hit movieclip instead of the hero itself.
You can obvioulsy change the hero_hit size according to the feeling you want to give to your game.
The smaller the size, the hardes to collect coins.
Final actionscript is:

ACTIONSCRIPT:
  1. onClipEvent (load) {
  2.     yspeed = 0;
  3.     xspeed = 0;
  4.     wind = 0.00;
  5.     power = 0.65;
  6.     gravity = 0.1;
  7.     upconstant = 0.75;
  8.     friction = 0.99;
  9. }
  10. onClipEvent (enterFrame) {
  11.     if (Key.isDown(Key.LEFT)) {
  12.         xspeed = xspeed-power;
  13.     }
  14.     if (Key.isDown(Key.RIGHT)) {
  15.         xspeed = xspeed+power;
  16.     }
  17.     if (Key.isDown(Key.UP)) {
  18.         yspeed = yspeed-power*upconstant;
  19.     }
  20.     if (Key.isDown(Key.DOWN)) {
  21.         yspeed = yspeed+power*upconstant;
  22.     }
  23.     xspeed = (xspeed+wind)*friction;
  24.     yspeed = yspeed+gravity;
  25.     _y = _y+yspeed;
  26.     _x = _x+xspeed;
  27.     _rotation = _rotation+xspeed;
  28.     if (_root.wall.hitTest(_x, _y, true)) {
  29.         xspeed = 0;
  30.         yspeed = 0;
  31.         _x = 120;
  32.         _y = 120;
  33.     }
  34.     if (_root.coin.hitTest(this.hero_hit)) {
  35.         _root.coin._x = Math.random()*400+50;
  36.     }
  37. }

As you can see, the hit test at line 34 checks collision between the coin movieclip and the hero_hit movieclip

And here ends the second part..

Download the source code for all examples and experiment.

Of course, leave me feedback.

Continue with the 3rd step

Related Item: casino game

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.88 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

135 Responses to “Flash game creation tutorial - part 2”

  1. Rahul Mhatre on November 20th, 2006 5:18 pm

    HI
    this is a very cool tutorial really.I liked it and i will make it.I am a beginner in flash so it is quite difficult for me.
    But the tutorial is really cool.

  2. Conor on November 23rd, 2006 8:29 pm

    another problem:

    i pasted the code properly so that the ball can move, but instead of the ball simply going left and right while rotating the shape, the ball rotates round a point thats about 200 pixels away from it! i hope you understand that - it still moves left and right but swirls around and goes all out of control. Plus the gravity and speed don’t seem to be right because basically it’s very slow. by the way i am a beginner…

  3. fhbdnv on November 27th, 2006 5:42 am

    pleas just make it so

    is key is down up he goes tn the direction he is faceing like a tank and make no gravity

  4. Ben on November 28th, 2006 10:02 pm

    really cool
    But how would i edit the code so when it hits an object i want it to it goes to the next level (where i will put the next level)

  5. Emanuele Feronato on November 29th, 2006 12:19 am

    We’ll cover it in the next tutorial, coming soon.

  6. Anonymous WebDesigner-In-Training on November 29th, 2006 11:26 pm

    Wow. I read Part 1 and 2, and I really like this. I knew MOST of it, but for example, the square used to make the pathing better was a great idea that I would have never thought of. I never knew such a nice game like Ball Revamped could be made with such easy coding. Of course, this isn’t Ball Revamped yet. ;)
    Also, I’m surprised that there are still people in the world that are so caring as you are. I’m glad there are people around that out of their free will, decide to help others that they don’t even need to help. If you were to not write these tutorials, it wouldn’t affect you in anyway, but instead it effects people you don’t even know.

  7. Anonymous WebDesigner-In-Training on November 30th, 2006 4:44 am

    But I have an issue. For some reason, even after I do all the things tutorials tell me to do, I can never get hitTests to work. Is there any way you can help me with it?

    I have a movieclip titled hero, and the instance of hero, and this is the actionscript on it.

    onClipEvent (load) {
    power = 3;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
    _x -= power;
    }
    if (Key.isDown(Key.RIGHT)) {
    _x = power;
    }
    if (Key.isDown(Key.UP)) {
    _y -= power;
    }
    if (Key.isDown(Key.DOWN)) {
    _y = power;
    }
    if (ifthis.hitTest(_root.point)) {
    _root.score = 1;;
    }
    }

    Basically, as you can see, I want my dynamic text box with the instance of score to go up one point when my hero hits the point movie clip with the instance of point.
    The movieclips touch, but NOTHING HAPPENS. Every time I try to to a hitTest, I get the same problem.

  8. Me on November 30th, 2006 5:28 pm

    Your code:
    if (ifthis.hitTest(_root.point)) {
    _root.score = 1;;
    }

    Correct code:
    if (this.hitTest(_root.point)) {
    _root.score = 1;
    }

    I think

  9. Anonymous WebDesigner-In-Training on November 30th, 2006 5:43 pm

    Ill try that.
    Also, the two ;’s was a typo.

  10. Emanuele Feronato on November 30th, 2006 5:43 pm

    I think
    if (this.hitTest(_root.point)) {
    _root.score = 1;
    }
    Anyway scoring system and whatsoever is going to be explained soon.

  11. Anonymous WebDesigner-In-Training on November 30th, 2006 5:50 pm

    Alright, it worked. Thanks a bunch.

  12. TuShae on December 2nd, 2006 6:55 pm

    how can i get the ball to bounce off the bounds instead of reset when it hits them?

  13. Emanuele Feronato on December 2nd, 2006 8:52 pm

    Bouncing requires a close-to-real physics engine, that won’t be covered in this game creation.

    Anyway, I am preparing some tuts on flash physics so come back soon.

  14. Jonathan on December 5th, 2006 3:34 am

    By far one of the best tutorials in terms of making things clear to people and well written out. Can’t wait for 3 and beyond?

  15. Anonymous WebDesigner-In-Training on December 5th, 2006 11:47 pm

    I have a question. I have a movie clip following my mouse, okay? And I have my actionscript set to where, when it hits another movie clip, my score goes up constantly. I’ve done a hittest that makes my score go up constantly before, but its not working this time. :( I always have problems with hittest actionscript. Do you think you could show me an example in coding that when you get a chance?

  16. James on December 6th, 2006 1:33 pm

    Awesome
    your’e tutorial helped me allot

  17. No problemo, (yeah right!) on December 10th, 2006 12:20 am

    I have some problems. Even though I type the script the right way, the wall thingy won’t work. I have even tried to just copy it, and it still doesn’t work.

    Can someone please help me??

  18. Ammar J. on December 17th, 2006 1:42 am

    hey man, ur tutorialz r greatt. i’m only 13 and i understand the script perfectly. ur a good teacher :P keep up the good work.

  19. Ammar J. on December 17th, 2006 1:47 am

    yeh, this is for No problemo, (yeah right!). same happened to meh, but its easier if u make a htin line ur boundy. just make a line fomrt he line tool…. select it, press F8, give it a name and then kepp the instance wall(or somehitng…this just an example). then on ur hero, type in this code:

    if(_root.wall.hitTest(this)){
    xspeed = 0;
    yspeed = 0;
    _y = 120;
    _x = 120;
    }

    and voila, theres ur boundery. notice how i changes the:

    (_x, _y, true)){

    to :

    (this)){

    ?
    typing, (this)){ will make the entire line or shape the bondery instead of just one pixel of it.

    p.s. thanks to this tutorial for giving me this info :P

  20. killa 7 on December 18th, 2006 4:36 am

    I do it all right but when i move my guy the walls move with it wtf!?!

  21. gamerz paradise on December 18th, 2006 9:26 pm

    Personally, I never use more than one life on this if I understand this correclty. I just wonder why so many do not understand how this is. I guess that is the beauty of it all. Good post though!!

  22. Anonymous Reader on December 20th, 2006 10:13 am

    This is a great tutorial up to the point where you are supposed to create a wall. When I make my wall, I named it boundry. Ok, how do i use an instance or what ever you call it. When I drag the movie clip onto the screen, and play the game, it will not work… Why is this, care to share more details about the boundries? Thanks!

  23. Anonymous Reader on December 20th, 2006 6:43 pm

    Ahh, I figured it out, you go to property’s and type in a name in . Now, though, I seem to be haveing a problem getting the blue square to work. How do you insert one movie clip into another? When I do it, the blue box remains still whereas the Hero does like normal. Why do they not move together? Thanks for any help.

  24. Emanuele Feronato on December 20th, 2006 10:25 pm

    They have to be in the same movieclip.

    Download the source examples and you will figure out how.

  25. Diamonddrake on December 24th, 2006 10:46 am

    This tutorial is awsome, the look and feel of the movement you get out of most tutorials is sad, but with your step by step explaination, it shows you just how each element affects your character, so you can choose the effects you want. you have the makings for greatness. but comment questions that are being left to you, offend me. as a learning programmer, I take pride in the work that I do. and I spend hours carfully reading books, and online tutorials. When people constantly send you questions that are obvious typos and simple careless mistakes like forgetting to assign instance names, it wastes time of people with real questions. Not to mention the time of the people reading the tutorial thinking they can learn more from the comments, when in fact they are reading usless bable… not unlike this… of course, I am making a point. not being an idiot. you rock. thanks for this tutorial

    Rickey
    web master, diamonddrake web design

  26. dunno on December 24th, 2006 4:22 pm

    i cant get the hero to pick up coins i instanced itand name it coin but it still doesnt please can someone help me

  27. yura on January 4th, 2007 11:01 pm

    hey i cant make my walls work i added another layer and named it boundry and it still dosent work lol

  28. yura on January 5th, 2007 5:34 pm

    Hey i have another question. When you make like wall boundries and coins do you paste you autoscript on to the coin for coin and wall for wall? or you continue doing the for the hero???

  29. yura on January 5th, 2007 5:35 pm

    another thing how do you intense it?

  30. Purple_Chikcen on January 6th, 2007 12:17 am

    hey nice tutorial ive done part one and 2 it works perfectly but is there a way to make multiple movie clips with the same instance name do the same thing? like instead of putting this:

    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }

    i put that 4 times like this:

    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall2.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall3.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall4.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }

    is there a way to make the copies of a movie clip do the same thing without giving it a different instance name? cause i was doing my first ever game on flash and i made 150 enemies and only one worked so i had to frikin change the instance name of every one of them! lol can u help

  31. yoda on January 8th, 2007 9:12 pm

    omg my wall is still moving with the ball. How do i change that. plz tell me lol

  32. Dan on January 9th, 2007 8:05 pm

    Yoda check the actionscript is not on the wall as well. It doesnt need to be on the wall instance.

  33. yoda on January 9th, 2007 9:26 pm

    hey thanks Dan now its working all right

  34. yoda on January 9th, 2007 9:30 pm

    why is my ball goes right under the coin???

  35. yoda on January 9th, 2007 9:38 pm

    Do i have to make a new layer for the movie clip??

  36. xDeadxEmox on January 11th, 2007 1:24 am

    i am new to flash gmes and stuff but i dont get how to do the coin attempet 3 please help me

  37. Matt on January 11th, 2007 9:48 pm

    Hey the hero is working fine but the HitTest on the wall is not happening. My heroMC just goes under it! Also, when I download the source code it says invalid file format. Im using Flash MX 2004 - is that a compatability problem? Thanks, MAt

  38. ivis on January 12th, 2007 8:17 am

    Hey, in coin attemp 3, I dont ubderstand the “insert hero_mc into the middle of the hero”. Can you please tell me in full details. And when I pasted your script in coin attempt three,the hero just move seperatly with the hero_hit.

  39. xDeadxEmox on January 13th, 2007 3:23 am

    i love you tutorial but is there a way so that you can put the coinb in a fixed place in stead of random?

  40. Owen on January 15th, 2007 7:17 am

    When I tried the code, I had a slow frame rate and I noticed that the ball doesn’t rotate at the right speed, you can’t simply say
    _rotation = xspeed; . This rotation glitch seems to be Conor’s problem as well. The ball is rotating at δ pixels/frame. The distance travelled is the length of the arc, which is r*α^c, where α is the angle subtended and ^c means its in radians. Since flash works in °, you must convert it to that. So, the distance travelled in 1 frame (δ) = 2πr*(α°/360), .: α° = 180*δ/πr, so, the rotation should be set to be equal to (180/πr)*xspeed. r is equal to half of the height of the hero, so, the final code for the rotation is:
    _rotation = (180/3.14159*(0.5*_height))*xspeed; This fixed the glitch.

  41. Emanuele Feronato on January 15th, 2007 11:28 pm

    That’s amazing, I just wanted to make it rotate to simulate the speed, but you improved the whole thing a lot.

    Thank you.

  42. Nabeel on January 20th, 2007 7:27 am

    hello, umm.. i have a problem with the wall thing. every time i try it always goes under and passes through it like a ghost. please can you help?

  43. Paul w on January 21st, 2007 4:23 pm

    Absolutely Bloody Brilliant!

  44. asif on January 22nd, 2007 12:29 pm

    The tutorial is really very cool.It is helping me to devlop different type of games too.

  45. Mick on January 30th, 2007 6:42 am

    hey, so far this tutorial has been really good, but when i move my character at the wall really fast he goes straight through it, but when he goes slow it works.
    Any one got any ideas?

  46. Mick on January 30th, 2007 6:45 am

    oh yeah, and what if i want to make itn when my hero hits the wall it goes to a game over screen?

  47. Mick on January 30th, 2007 7:03 am

    ok, i worked out wat was going on, if i’m holding down an arrow key while i’m heading at a wall my hero goes through it, but if i head at a wall and let go of the key b4 i hit the wall, my hero goes back to the starting point. But i still dont nkow y its doing it.

  48. James on February 3rd, 2007 8:30 am

    Great! I’ve never seen a tutorial like this. This tutorial is cool.

  49. Dan M on February 6th, 2007 10:41 pm

    Thanks, Emanuele Feronato for publishing such thorough actionscript tutorials.

    Can anybody explain why my code is broken? It is entered into the action window for PLAYER_MC. PLAYER_MC and GROUND_MC are both on layer1 frame 1. PLAYER_MC has some simple movement controls. When PLAYER_MC crosses GROUND_MC, this hitTest doesn’t work.

    onClipEvent (enterFrame) {
    if (_root.GROUND_MC.hitTest(this)) {
    trace(”hitdetected”);
    }
    }

    I have also tried writing it as
    if (this.hitTest(_root.GROUND_MC)){

    And I’ve tried it without “_root” No collisions are ever detected. Adding the following:

    trace (_root.GROUND_MC.hitTest(this))

    Returns nothing but “undefined”.

    Thanks for any help, I’m learning flash and starting to understand and enjoy it, but this is really stumping me.

  50. Anonymous on February 10th, 2007 6:29 pm

    I really like your tutorial but I don’t like how you explain.
    Example:
    “Just create a new movieclip called “hero_hit” and insert into the hero’s movieclip,”
    How do I insert it into the hero’s movieclip??

  51. Dan M on February 11th, 2007 11:18 pm

    NM, figured it out. I didn’t give names to all of my instances.

  52. glucozade on February 13th, 2007 1:32 pm

    sweet tutorial! im replacing the coin with rings, similar to a Sonic game, and its working. YAY! Hooray for tutorials

  53. glucozade on February 13th, 2007 11:54 pm

    hey i’ve done up till the end of this tut. and my script is as below (the ‘hero’ is Sonic and the ‘coin’ is a ring and thw ‘wall’ are spikes, i’ve also taken rotation out.)

    onClipEvent (load) {
    yspeed = 0;
    xspeed = 0;
    wind = 0.00;
    power = 0.65;
    gravity = 0.1;
    upconstant = 0.75;
    friction = 0.99;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
    xspeed = xspeed-power;
    }
    if (Key.isDown(Key.RIGHT)) {
    xspeed = xspeed power;
    }
    if (Key.isDown(Key.UP)) {
    yspeed = yspeed-power*upconstant;
    }
    if (Key.isDown(Key.DOWN)) {
    yspeed = yspeed power*upconstant;
    }
    xspeed = (xspeed wind)*friction;
    yspeed = yspeed gravity;
    _y = _y yspeed;
    _x = _x xspeed;
    if (_root.spikes.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.ring.hitTest(this.sonic_hit)) {
    _root.ring._x = Math.random()*400 50;
    }
    }

    it works ok when i try it,but is there anything ive missed?

  54. glucozade on February 13th, 2007 11:57 pm

    PS

    No Problemo hve you given your movieclips an instance name each? and are they the same in the script?

    ie, in my script above, i’ve given my ‘coin’ an instance name as ‘ring’ and replace the ‘coin’ in the script with ‘ring’

  55. Ryke on February 20th, 2007 4:26 am

    This tutorial is the best of the bestbut i was wandering why Whenever i add the rotation it always goes up before going sideways. why is that

  56. Mike on February 24th, 2007 6:21 am

    Ryke,

    double-click on the movieclip of your ball and you see that it brings you to an edit screen for that clip? Make sure the little crosshair is at the center of your ball. Once you do that you can double click anywhere on the frame and it should bring you back to your main screen. Test your movie out and let me know if that helps any.

    Mike

  57. Mike on February 24th, 2007 6:22 am

    Purple Chicken made a post in January that wasn’t answered by anyone. If anyone knows how please tell cus I have the same

  58. Mike on February 24th, 2007 6:24 am

    Purple Chicken made a post in January that wasn’t answered by anyone. If anyone knows how please tell cus I have the same question. He said:

    “hey nice tutorial ive done part one and 2 it works perfectly but is there a way to make multiple movie clips with the same instance name do the same thing? like instead of putting this:

    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }

    i put that 4 times like this:

    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall2.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall3.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    if (_root.wall4.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }

    is there a way to make the copies of a movie clip do the same thing without giving it a different instance name? cause i was doing my first ever game on flash and i made 150 enemies and only one worked so i had to frikin change the instance name of every one of them! lol can u help”

    //any ideas anyone? It would be appreciated =)
    //Thanks,
    //Mike

  59. Daniel on February 25th, 2007 12:54 pm

    Hi i need help!
    I am making my spacship game but i need it to rotate on side to side movment and up down thrusts in direction its facing can anyone help?

    Please e-mail me if you can
    mcdonnell.13@gmail.com

    cheers

  60. Mike on February 26th, 2007 6:21 am

    Daniel,

    Try doing this to rotate your ship.

    if (Key.isDown(Key.LEFT)) {
    _rotation -= 5;
    }
    if (Key.isDown(Key.RIGHT)) {
    _rotation = 5;
    }

    I find 5 to be a nice speed to rotate at as it isn’t too fast or too slow. The up and down thrust movement you are asking about is mentioned in every one of these tutorials so I figure you know how to do that already.

  61. ronald on March 1st, 2007 5:15 pm

    stil a nice tutorial,
    i got stuck with the collision because i rreally have no idee what movieclips and everything is but i now i have it almost perfect,
    only my sprite inst perfectly round so it moves weird,
    think i start on the next now

  62. Zeozen on March 10th, 2007 4:34 pm

    Zomg, damn good tutorial! Only one question… I created a movie clip called hero_hit, and instanced it as the same. But how do I get the hero_hit movie clip to move with the hero instead of just staying at one place and looking retarded.. please answer me anyone:D

  63. Ryke on March 14th, 2007 7:15 am

    I cant seem to get the wall to work. Well acually i dont get the wall at all. but i must say so far this is the best tutorial ive seen.

  64. Ryke on March 14th, 2007 7:20 am

    Oh never mind I got it now I forgot to read the part about making the movie clip and instance name. although i still cant get the ball to spin straight it always goes up and then down if i add the rotation,but only when i turn left or right, once rotation is off it stops

  65. Trevor on March 14th, 2007 7:07 pm

    I don’t get the wall bit, I have added a movie clip surrounding the ball and named it wall but it still goes straight through! any ideas of what it might be?
    P.S. great guide thx a lot!

  66. Trevor on March 14th, 2007 7:10 pm

    Oh never mind, I just made the lines thicker and now it works!

  67. i need help on March 19th, 2007 5:19 pm

    guy i’m beginner i dont know how to do the bond plz help me …

  68. i need help on March 19th, 2007 5:24 pm

    help guy my e-mail is quockh2000@gmail.com send me how to do the wallplz

  69. Jeff Nemeth on March 23rd, 2007 4:33 pm

    Hey! My hero dude is going way faster than yours in this tutorial. How can I slow it down?

  70. CDC7 on March 24th, 2007 5:06 am

    I am creting a flash game and i was wondering if you could help me with the code for it. It is a maze where the cursor has to stay inside a line, when it reaches the end there is a button to press and it moves to the next scene. This tutorial was so helpful I thought I would ask about this.

  71. King Twili on April 3rd, 2007 7:09 pm

    When i enter the code, for the walls, the walls move around. this has happened twice now. weeblhater@yahoo.co.uk if you can help. thank you!!

  72. newbie... on April 10th, 2007 2:49 am

    i just started practicing flash yesterday i can make the ball move and turn (rotate) but it cant make it so the walls make it disappear (or die) i tried it even by starting over but… my ball just goes through the walls… anyone here to fix my mistake? this is the action code i put in

    onClipEvent (load) {
    yspeed = 0;
    xspeed = 0;
    wind = 0.00;
    power = 0.65;
    gravity = 0.1;
    upconstant = 0.75;
    friction = 0.99;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
    xspeed = xspeed-power;
    }
    if (Key.isDown(Key.RIGHT)) {
    xspeed = xspeed power;
    }
    if (Key.isDown(Key.UP)) {
    yspeed = yspeed-power*upconstant;
    }
    if (Key.isDown(Key.DOWN)) {
    yspeed = yspeed power*upconstant;
    }
    xspeed = (xspeed wind)*friction;
    yspeed = yspeed gravity;
    _y = _y yspeed;
    _x = _x xspeed;
    _rotation = _rotation xspeed;
    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    }

    ……………………………………………………
    an heres how the things were placed

    key:
    boundoury= …
    ball= *

    ……………………………………..
    . .
    . .
    . .
    . * .
    . *** .
    . * .
    . .
    . .
    . .
    . .
    ……………………………………..

    help would be greatly appreciated

  73. newbie... on April 10th, 2007 2:50 am

    sorry the map got cut off my mistake…

  74. Flash beginer on April 12th, 2007 6:20 am

    I was wondering how you put the hero_hit and the hero into 1 movieclip?
    I couldnt open the source code files either. So Could someone PLEASE help me?

  75. joey on April 17th, 2007 2:21 pm

    really good tutorial, helped me loads !! i have a crap understanding of actionscript but i still found this easy to follow.
    cheers.

  76. bluemilk on April 17th, 2007 7:53 pm

    People asking about the hitArea part. Doubleclick on your ball movie clip and it will go into the movie clip where your ball is just a shape not a movie clip. In the movieclip create a new layer underneath or (above if you want to see it) your ball. In this layer create your hitArea square and convert it to a movie clip. Give it an instance name of hero_hit. Great tutorial.

  77. Guy-Who-Really-Needs*HELP* on May 21st, 2007 10:52 am

    i didnt get the bounds part, do i create a new symbol called bounds?
    i did that and put it in different layer and pasted the code nd when i check it out the entire thing moves so if i press left the bounds(which i drew) move left with the ball inside?weird huh.

  78. Helpless on May 30th, 2007 3:15 am

    Uh ok, I really need help with my wall! I can get it to work sometimes, but its almost like if Im going fast enough it will just tress pass through it.

  79. abhilash on May 30th, 2007 12:39 pm

    in reply to helpless:
    this is happening because your bounds will be thin make them a very thick

  80. Piggy on June 20th, 2007 4:32 am

    AWSOME TUTORIAL!!!!!!!!!!!!!!!
    and the comments were really helpful with the hero hit thingy. couldn’t have finished without it
    THANKS!!

  81. Phobia on June 20th, 2007 9:50 pm

    Great tutorial, love it! I had one problem with the walls, but thanks to abhilash’s comment, I was able to fix it.

  82. flaming on June 24th, 2007 11:59 pm

    I ahve a problem with the bounds. My hero just goes through the wall. Can you explain how to fix my problem?

  83. flaming on June 25th, 2007 12:01 am

    Can u explain how to instance the bounds as “wall”?

  84. tom on June 26th, 2007 11:09 pm

    i need help with hit checking. i want to get the wall to reset the ball as soon as it hits the edge not justhe center pixel but i can’t get it to work. i have tried using the thing done on the coin with the hero_hit but i can’t get that same method to work with the boundries.

  85. Michael?! on July 11th, 2007 3:17 am

    hey guys, im new to flash games so this is a great tutorial, thanks!

    Also, my wall moves with my hero, how do i stop that?

  86. Michael?! on July 11th, 2007 4:02 am

    email me if you can help please!!!
    aim_your_nukes@yahoo.com

  87. flaming on July 17th, 2007 12:25 am

    how can it so that the hero_hit also works with the wall?

  88. Dan on July 20th, 2007 11:36 pm

    Really good tutorial. Thank you so much for taking the time to write it.

  89. newbies 4 evr on August 17th, 2007 5:16 pm

    i need help so that when the ball hits the wall is will “gotoAndPlay” a movie clip

  90. David Azar on October 31st, 2007 10:45 pm

    hi nice tutorial

    i have some little problems

    when i copy/paste the srcipt op gettingf the coin, it doesent work.

    when i play the game, the hero juust pass under the coin.
    it doesent dissapear but its imposible to me to get the coin please help me.

    ps. i dont have a web page, just needed to write a flase one to write this

  91. In need of help on November 10th, 2007 8:35 am

    When i was trying to create the walls mine ended up very glitchy so i downloaded your source file. When i looked at your wall movie clip i noticed that the blue box that surrounds movie clips was in the center of the wall. How did you do this?

  92. Moomoo on November 13th, 2007 7:34 am

    My friend told me about your tutorials and i love them. I was wondering if i could use some of your script to create an original game of mine. I’ll say I used your tutorials to help me make it.

  93. Emanuele Feronato on November 13th, 2007 10:11 am

    Of course you can!

  94. Moomoo on November 21st, 2007 6:47 am

    thanks

  95. BO on November 30th, 2007 2:33 am

    YOU LEGEND….!!!

  96. Lemony on February 3rd, 2008 11:40 am

    Hi, I am making a retro game, and i dont want my delta to move in the way your hero did, so I skipped out the steps that made it all swurly, then I added the coins in without the code that makes it go swurly, but my charictar is not collecting the coins, just passing strait through them.

  97. Rhys on February 3rd, 2008 9:04 pm

    Hi
    Excellent Tutorial.
    I understand all of it aprt from the coin thing. In try to do it and when i do it says:
    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 1: Clip events are permitted only for movie clip instances
    onClipEvent (load) {

    Scene=Scene 1, Layer=Layer 1, Frame=1: Line 10: Clip events are permitted only for movie clip instances
    onClipEvent (enterFrame) {

    Please help
    and also wat program do u use?

  98. Rhys on February 20th, 2008 9:01 pm

    To lemony that is what happened to mine.
    Try this code in your main charcter:
    if(_root.coin.hitTest(_root.player)){
    { _root.coin._x = Math.random()*400+10; { _root.coin._y= Math.random()*400 + 10 } }
    }
    }

    also make sure that the coin is an MC and hasnt got any coding in it!!

  99. Rhys on February 20th, 2008 9:03 pm

    To David Azar i think u had the same problem as Lemony and posted the code that works with me above ^^^^
    Thanks
    Rhys

  100. Rhys on February 20th, 2008 9:05 pm

    To flaming
    to fix your wall problem what i did
    was create 4 walls around my
    lines (or wateva u call them!) and then put this code in each of them: Left = onClipEvent (enterFrame) {
    if (this.hitTest(_root.player)) {
    _root.player._x +=15;
    }
    }

    Right = onClipEvent (enterFrame) {
    if (this.hitTest(_root.player)) {
    _root.player._x -=15;
    }
    }

    Up = onClipEvent (enterFrame) {
    if (this.hitTest(_root.player)) {
    _root.player._y +=15;
    }
    }

    Bottom = onClipEvent (enterFrame) {
    if (this.hitTest(_root.player)) {
    _root.player._x -=15;
    }
    }

    View the tutorials or http://www.rasclerhys.com/Tutorials.php
    Thanks!

  101. Chris Valle on March 1st, 2008 6:12 pm

    again, easy to follow and really helped

    keep up the good work ^_^

  102. anthony on March 16th, 2008 5:03 pm

    so i went to do this and wrote out all of the code and it comes with an error saying it needs to be a movie clip how do i make it one. could u put a link to show me this? im a bit new to flash

  103. jan on March 17th, 2008 6:52 pm

    hi,
    my hero walks always trough the walls, what kinda code i need that he should stand before???

  104. The_erik on April 8th, 2008 12:56 pm

    so.. i too have this problem with the hitTest thing.
    My character just passes through the wall no matter what i do :S
    i named my character “hero”.
    i named the wall “wall”.
    i’ve tryed doing Rhys’s trick aswell, help :<

  105. The_erik on April 10th, 2008 8:33 am

    allright, i’ve figured it out (needed to instance the boundary >.< )

    but my Flash is still awfully laggy, while the flash i see here works perfectly.

    ? :(

  106. Tommy on April 17th, 2008 2:54 am

    Okay,

    “The bounds

    Every game area has bounds, so I created a movieclip called “bounds” and instanced as “wall”.

    My hero can’t touch any wall, so the actionscript will be:

    PLAIN TEXT
    ACTIONSCRIPT:
    onClipEvent (load) {
    yspeed = 0;
    xspeed = 0;
    wind = 0.00;
    power = 0.65;
    gravity = 0.1;
    upconstant = 0.75;
    friction = 0.99;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.LEFT)) {
    xspeed = xspeed-power;
    }
    if (Key.isDown(Key.RIGHT)) {
    xspeed = xspeed+power;
    }
    if (Key.isDown(Key.UP)) {
    yspeed = yspeed-power*upconstant;
    }
    if (Key.isDown(Key.DOWN)) {
    yspeed = yspeed+power*upconstant;
    }
    xspeed = (xspeed+wind)*friction;
    yspeed = yspeed+gravity;
    _y = _y+yspeed;
    _x = _x+xspeed;
    _rotation = _rotation+xspeed;
    if (_root.wall.hitTest(_x, _y, true)) {
    xspeed = 0;
    yspeed = 0;
    _x = 120;
    _y = 120;
    }
    }

    That doesn’t work for me.. My player just goes through the walls.

  107. Important Question on May 2nd, 2008 6:49 pm

    What do I do if I don’t want to kill the character, but just stop them from leaving a defined area?

  108. Important Question on May 2nd, 2008 6:56 pm

    Try:
    “if (_root.movieclip.hitTest(this)){”…
    with “movieclip” as the name of the object that the movieclip you add this hitTest to will react to.

  109. Richard on May 19th, 2008 9:37 am

    Hit test trange behaviour?

    I created the walls as a movie clip and called the instance of the movie clip on my stage walls.

    Now, the first if statement works the second doesn’t. The hero (red ball) doesn’t even more. Any idea why?

    1) if (_root.walls.hitTest(this._x, this._y, true)) { ….

    2) if (_root.walls.hitTest(_root.hero)) { ….

  110. Richard on May 19th, 2008 9:39 am

    Hi Tommy

    Silly question but have you named the instance of the wall on your stage in the property window?

  111. Roger on May 22nd, 2008 6:20 pm

    HI.

    First I want to say that the tutorial is very good, and that I have learned a lot of it. But it was really hard to understand some changes you made on the code from the first to the second part. Things that are the same but make confusing for some of us that are starting on this. like: On the first you used something like _y += yspeed; and then U change it to _y = _y + yspeed; now I know it is the same.

  112. john smith on May 31st, 2008 10:36 pm

    you would put the next level on a different level and you would have to make the object a button then make the action script:
    on (rollover){
    gotoAndPlay (2)}

  113. Samuel Benson on June 6th, 2008 11:54 am

    EMPHASIS ON THE INSTANCE NAMING!!
    (i forgot and wasted a lot of time trying to solve the problem DOH!!

  114. joe on June 15th, 2008 5:36 am

    hey this tutorial is so f-ing awesome omg.i really want to try it but… i dont know where to download flash. i want to make a cool game like this mmo called dofus it’s made with flash and it awesome but everything is for mem’s i want to make a game like that but no mem crap where it’s all like pay pay pay so tell me where to buy or download the flash game creation thing

  115. lol on July 7th, 2008 1:18 am

    Works fine until wall collisions they just wont work for me…

  116. Stetlo on July 7th, 2008 2:07 pm

    You just put a stop actionscript on the frame your at an then make a new frame and stop it. theres 2 levels

  117. Thanks! on July 19th, 2008 7:39 am

    thanks really heplful!

  118. help on July 21st, 2008 2:21 am

    can some one help i don’t know how to connect the hero movie clip with the hero_hit one.

  119. SumYungGai on July 23rd, 2008 2:23 am

    This is sort of irritating me. =/

    When I make a symbol and give it an instance name “wall” -I’m not confusing this with the symbols’ name- my program -Adobe Flash CS3- only considers the first “wall” symbol I made. All other symbols with the instance name “wall” are not accounted for and don’t carry on the appropriate actions regaurding the actionscript.

    E.G. A black square -symbol name “thing”- and-instance name “wall”- at the bottom of my screen kills my guy appropriatley. But when I try to put more symbol “thing”s into the field and give them instance name of “wall” my guy passes through them. The bottom wall still works but the others dont.

    Know what I’m doing wrong?

  120. SumYungGai on July 23rd, 2008 2:28 am

    I worked around it by giving the walls action names of “wall1″, wall2″, wall3″, and “wall4″ and it works, but I have to write actionscript for all 4 walls. Not to mention is clogs my script box and makes it more difficult to find bugs, certain portions of action script that need rewritten, ect…

  121. Connor on July 27th, 2008 6:04 am

    I have a problem. I added hero_hit to the hero movie clip, and I added the actionscript to the new-and-improved hero movie clip after I put hero_hit in (I went back to the “scene 1″ area, not inside the hero movie clip), and when I move over the coin movie in the test movie, the hero won’t “collect” the coin. I don’t really know what it is I should do.

  122. Nathan on July 28th, 2008 8:15 am

    reply to Connor:

    make sure that hero_hit is an instance name.

  123. cc on July 29th, 2008 3:49 am

    wohoo thanks you are a hero!

  124. Korezz on July 30th, 2008 3:51 am

    How do i make him stand on the wall and not die

  125. Adrian on August 13th, 2008 7:32 pm

    ok, i took the exact same code from your hitTest for the wall, and mine doesn’t work…….
    I am trying to make it will not fall (which i made it do) but do not want to make a separate hitTest for each piece of wall.

    onClipEvent (load) {
    xspeed = 0;
    yspeed = 0;
    uppower = 7;
    downpower = 5
    friction = .75;
    gravity = 5;
    upconstant = 5;
    }
    onClipEvent (enterFrame) {
    if (Key.isDown(Key.UP)) {
    yspeed -= uppower+upconstant;
    }
    if (Key.isDown(Key.DOWN)) {
    yspeed += downpower;
    }
    if (Key.isDown(Key.LEFT)) {
    xspeed -= uppower;
    }
    if (Key.isDown(Key.RIGHT)) {
    xspeed += uppower;
    }
    _x += xspeed;
    _y += yspeed;
    xspeed *= friction;
    yspeed *= friction;
    yspeed += gravity;
    if (_root.wall.hitTest(_x, _y, true)) {
    yspeed = 0;
    downpower = 0;
    } else {
    downpower = 5;
    gravity = 5;
    }
    }

    this is the part that doesn’t work

    if (_root.wall.hitTest(_x, _y, true)) {
    }

    ????????????????

  126. JM on August 30th, 2008 3:11 pm

    I have used the exact same code as you did for the coin test, yet the hero does not collect it. Again, it is exactly the same apart from the graphics and yet it makes no difference. Some help on this matter would be appreciated? I tried the solution Rhys posted as well.

  127. Malboro Jones on September 18th, 2008 3:27 pm

    Hi JM if your still interested I may have been having the same problem, it wasnt going through the pot or left but was through the right and bottom?

    It’s a lot more simple to make 4 lines, one one top,bottom, left and right, call them wall1/2/3/4 and in the
    _root.wall1.hitTest(_x + _height/2, _y, true) {
    }
    that would be for the bottom wall if instanced as wall1.
    Hope this is helpful for anyone having the same problem.

  128. D on September 24th, 2008 5:27 am

    onClipEvent (load) {

    doesn’t work for me it says there’s and error
    any help? :)

Leave a Reply




Trackbacks

  1. Flash game creation tutorial - part 1 at Emanuele Feronato on November 18th, 2006 10:34 pm

    [...] November 18th update: 2nd part released. [...]

  2. Flash game creation tutorial - part 3 at Emanuele Feronato on December 6th, 2006 1:57 pm

    [...] Remember to read 1st and 2nd part if you are new to this tutorial, and let’s go. [...]

  3. Flash game creation tutorial - part 4 at Emanuele Feronato on December 23rd, 2006 6:27 pm

    [...] Read steps 1, 2 and 3 if you’re new to this tutorial, then follow this one. [...]

  4. Flash game creation tutorial - part 5 at Emanuele Feronato on December 31st, 2006 11:25 pm

    [...] Read steps 1,2,3 and 4 and you’re ready. [...]

  5. A strange way to move the player with Flash at Emanuele Feronato on May 30th, 2007 8:16 pm

    [...] Lines 23-25: if the upper or lower part of the ball collides with the wall, I invert ball’s y speed because I am sure I touched the “ceiling” or the “floor”. For more information about hitTest method refer to this tutorial [...]

  6. Rodrigo Flausino » Dicas de tutoriais e sites para criação de jogos em Flash on August 30th, 2007 5:27 am

    [...] Flash game creation tutorial - part 2 [...]

  7. A Gem of Flash Game Tutorials | Newbie Game Programmers on December 17th, 2007 4:22 pm

    [...] game creation tutorial - Part 1 :: Part 2 :: Part 3 :: Part 4 :: Part 5 :: Part 5.1 :: Part 5.2 :: Part [...]