Create a flash draw game like Line Rider or others - A different approach

April 21st update: part 2 released
June 15th update: part 3 released

Some days ago I received a very very interesting email from Kevin W. (for Kevin: I can give you more credits if you send me some information about you).

It refers to the Line Rider game creation, with a different approach.

Read carefully:

------

I was reviewing parts three and four of your line rider tutorials today because I had never tried out your collision and reaction techniques for myself, when I found some things I didn't like.

Mostly, the practice of combining and extracting angles constantly using trig, and therefore the need to compensate for the differences between degrees and radians. I didn't want to do this because I've had a lot of bad luck with my own experiments.

I was reminded of a book on Flash MX game design I saw a few months ago. I remembered the author talking about using vectors exactly for this purpose which eliminates the need for taxing trigonometry functions.

The idea is to keep the x and y speeds separate, and use normalization and scalars to determine the resultant speed.

I think the result is somewhat more efficient and basically keeps track of the ball's momentum which would lead well into the concepts of sliding required for a sled-based game.

I haven't yet incorporated your fixes for the ball sticking in the terrain and losing energy, but I would like to know what you think of my approach to this problem.

I hope you take this as gratitude on my part for your tutorials. I started in January of this year using just your blog.

By the way, I am Izy who commented on your posts, the one who made the rocket game with the missile.

-----

Kevin sent a fully commented actionscript too, here it is:

ACTIONSCRIPT:
  1. //lineRider_ideas//
  2. //////////////////////////
  3. //Initialize all variables to be used//
  4. //and attach all movieclips.////////////////
  5. //M is the ball, T is the terrain, F is a textfield.
  6. //////////////////////////////////////////////////////////
  7. var m:MovieClip = _root.attachMovie("ball", "ball", 1, {_x:125});
  8. var t:MovieClip = _root.attachMovie("terrain", "terrain", 2, {_x:0});
  9. var f:TextField = _root.createTextField("textfield", 20, 100, 25, 300, 25);
  10. var precision:Number = 1;
  11. var radius:Number = m._height/2;
  12. var sum_x:Number = 0;
  13. var sum_y:Number = 0;
  14. var collisions:Number = 0;
  15. var normal_x:Number = 0;
  16. var normal_y:Number = 0;
  17. var vector_length = 0;
  18. var go:Boolean = true;
  19. //////////////////////////////////////////////////////////
  20. //Create an object to hold speed information and gravity//
  21. //////////////////////////////////////////////////////////
  22. var speed:Object = {x:0, y:0, g:0.25};
  23. /////////////////////////////
  24. //Position certain elements//
  25. /////////////////////////////
  26. m._y = m._height;
  27. t._y = 400;
  28. f.text = "Click and drag to reposition the ball.";
  29. ///////////////////////////////
  30. //Allow the ball to be positioned//
  31. ///////////////////////////////
  32. _root.onMouseDown = function() {
  33.     go = false;
  34.     speed.x = 0;
  35.     speed.y = 0;
  36.     m._x = _root._xmouse;
  37.     m._y = _root._ymouse;
  38.     m.startDrag();
  39. };
  40. _root.onMouseUp = function() {
  41.     m.stopDrag();
  42.     go = true;
  43. };
  44. ////////////////////////////////
  45. //The main part of the code.////
  46. ////////////////////////////////
  47. _root.onEnterFrame = function() {
  48.     //Reset variables each frame.
  49.     sum_x = 0;
  50.     sum_y = 0;
  51.     collisions = 0;
  52.     if (go) {
  53.     //Check for collisions by generating points in a slightly different//
  54.     //manner than in your tutorials./////////////////////////////////////
  55.         for (var i:Number = 1; i<360; i += precision) {
  56.             var angle = (i*precision)*(Math.PI/180);
  57.             var spot_x = m._x+radius*(Math.sin(angle));
  58.             var spot_y = m._y-radius*(Math.cos(angle));
  59.             if (t.hitTest(spot_x, spot_y, true)) {
  60.                 collisions++;
  61.                 sum_x += spot_x;
  62.                 sum_y += spot_y;
  63.             }
  64.         }
  65.         if (collisions>0) {
  66.             //Here's the real key to my approach.//////////////
  67.             //First, I find the magnitude of the speed vector//
  68.             //to use as a scalar.//////////////////////////////
  69.             var total_speed = Math.sqrt(Math.pow(speed.x, 2)+Math.pow(speed.y, 2));
  70.             spot_x = (sum_x/collisions)-m._x;
  71.             spot_y = (sum_y/collisions)-m._y;
  72.             //Next, I find the length of a the vector that is the line//
  73.             //between the point of collision and the center of the object.//
  74.             //This is equal to the radius of the circle and so could be replaced
  75.             //by a constant instead.//////////////////////////////////////////////
  76.             vector_length = Math.sqrt(spot_x*spot_x+spot_y*spot_y);
  77.             //Here I normalize the component of the vector by dividing their magnitude
  78.             //by the total length of the two vectors, thereby creating another vector
  79.             //with a magnitude of exactly one.///////////////////////////////////////
  80.             normal_x = spot_x/vector_length;
  81.             normal_y = spot_y/vector_length;
  82.             //Finally, I multiply the normalized vector by the scalar to proportionally
  83.             //achieve the final speed of the ball./////////////////////////////////////
  84.             speed.x += -(normal_x*total_speed);
  85.             speed.y += -(normal_y*total_speed);
  86.         }
  87.         //Add gravity to the speed object and then add the speed to the ball's position.//
  88.         speed.y += speed.g;
  89.         m._x += speed.x;
  90.         m._y += speed.y;
  91.     }
  92. };

Then, Kevin fixed another problem... read:

-----

I noticed that if you change the code

speed.x += -(normal_x*total_speed);
speed.y += -(normal_y*total_speed);

to

speed.x = -(normal_x*total_speed)*friction;
speed.y = -(normal_y*total_speed)*friction;

it will behave almost identically to your part four code without the sticking bug.

-----

What to say? First, I want to thank Kevin for this precious feedback... then, I am going to study this code and eventually use it in future updates of this tutorial.

Here it is his working final movie

And this is the source code.

Any comment?

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 (1 votes, average: 3 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.

9 Responses to “Create a flash draw game like Line Rider or others - A different approach”

  1. hansa on April 9th, 2007 4:17 pm

    first reply =D

  2. voidSkipper on April 10th, 2007 7:12 am

    It seems to behave a lot more messily than your code. When and if the ball comes to a stop, it sinks twenty pixels through the floor, then shoots off the top of the stage.

  3. abhilash on April 10th, 2007 11:06 am

    Good

  4. AAR on April 10th, 2007 10:42 pm

    Itts nice!

  5. Peter on April 14th, 2007 5:53 pm

    Sorry my bad english ^^
    Your AS seems to be more clear, if you make a game with this AS I’m sure it will be a lot of glitches….
    Keep up with your nice tutorials!! :)

  6. Jesse on April 19th, 2007 11:08 pm

    This is minor, but I ran some tests and found

    Math.sqrt(Math.pow(speed.x, 2) Math.pow(speed.y, 2));

    is slower than

    Math.sqrt(((speed.x)*(speed.x)) ((speed.y)*(speed.y)));

    Math.pow just adds weight.

  7. Daniel on October 12th, 2007 7:30 am

    probably because its alignment is to the center of the ball.

Leave a Reply




Trackbacks

  1. Create a flash draw game like Line Rider or others - A different approach - Part 2 at Emanuele Feronato on April 21st, 2007 5:46 pm

    [...] I am glad to publish a very interesting upgrade to Create a flash draw game like Line Rider or others - A different approach by Kevin Ward. [...]

  2. Throw a ball with a sling physics Flash tutorial: Part 2 by Kevin Ward at Emanuele Feronato on August 16th, 2007 10:41 am

    [...] You may know Kevin for his excellent post about a different approach to create a game like Line Rider, and now he come with the second part of the sling tutorial. [...]