Create a Flash game like Snowflakes

Today I enjoyed a cute game called Snowflakes and I am about to show you how to create the main engine behing the game.

Snowflakes

The game is simple: a bunch of stars (snowflakes in the game, but I guess the author new saw the snow...) is falling from the sky, and you can affect their direction with your mouse, blowing them around.

Only two objects in this movie, the star and the mouse pointer.

This is the AS2 version, tomorrow I'll publish the AS3 one.

ACTIONSCRIPT:
  1. // mouse cursor replacement
  2. Mouse.hide();
  3. _root.attachMovie("pointer","pointer",_root.getNextHighestDepth());
  4. // max stars on stage
  5. max_stars = 20;
  6. // current stars on stage
  7. stars_on_stage = 0;
  8. // gravity
  9. gravity = 0.1;
  10. // this is the influence distance
  11. // using influence and real_influence I perform the square root only once
  12. // when the distance from the mouse and the star is less than real_influence
  13. // then the star is affected by the mouse
  14. influence = 625;
  15. real_influence = Math.sqrt(influence);
  16. // friction
  17. friction = 0.9;
  18. // divider, to make stars move slowly
  19. divider = 50;
  20. // main function
  21. _root.onEnterFrame = function() {
  22.     // should I add a star?
  23.     if (stars_on_stage<max_stars) {
  24.         //adding a star
  25.         stars_on_stage++;
  26.         starobj = _root.attachMovie("star", "star_"+x, _root.getNextHighestDepth(), {_x:Math.random()*450+25, _y:Math.random()*50-100});
  27.         // star initial speed
  28.         starobj.xspeed = 0;
  29.         starobj.yspeed = 0;
  30.         // function the star will execute at every frame
  31.         starobj.onEnterFrame = function() {
  32.             // gravity
  33.             this.yspeed += gravity;
  34.             // calculating the distance from the star and the mouse
  35.             // without square roots
  36.             dist_x = this._x-_root._xmouse;
  37.             dist_y = this._y-_root._ymouse;
  38.             distance = dist_x*dist_x+dist_y*dist_y;
  39.             // if we are in the radius of influence...
  40.             if (distance<influence) {
  41.                 // ...apply a force to the star
  42.                 // force is determined by mouse distance and speed
  43.                 xforce = mouse_speed*dist_x/divider;
  44.                 yforce = mouse_speed*dist_y/divider;
  45.                 this.xspeed += xforce;
  46.                 this.yspeed += yforce;
  47.             }
  48.             // adding friction
  49.             this.xspeed *= friction;
  50.             this.yspeed *= friction;
  51.             // updating position
  52.             this._y += this.yspeed;
  53.             this._x += this.xspeed;
  54.             // make the star rotate
  55.             this._rotation += (this.xspeed+this.yspeed);
  56.             // if the star reaches the bottom of the stage, remove it
  57.             if (this._y>300) {
  58.                 stars_on_stage--;
  59.                 this.removeMovieClip();
  60.             }
  61.         };
  62.     }
  63. };
  64. // function to be executed by the mouse pointer
  65. pointer.onEnterFrame = function() {
  66.     // calculating the distance from the last point the mouse was spotted
  67.     // and the current mouse position
  68.     dist_x = this._x-_root._xmouse;
  69.     dist_y = this._y-_root._ymouse;
  70.     mouse_speed = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
  71.     // minimum speed = minimum force applied
  72.     if (mouse_speed<0.2) {
  73.         mouse_speed = 0.2;
  74.     }
  75.     // updating pointer position
  76.     this._x = _root._xmouse;
  77.     this._y = _root._ymouse;
  78.  
  79. };

And here it is... now you can affect stars movement like in the original game.

Adjust gravity, influence, friction and divider to fine tune gameplay.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (7 votes, average: 4.86 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.
» 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 game like Snowflakes”

  1. robby on August 29th, 2008 11:50 pm

    cute little game, but it sure could use some relaxing music :D lol.

  2. Doggy on August 30th, 2008 11:01 am

    cool

  3. Nonitt on August 30th, 2008 11:50 am

    this version is more easier than the real game :S

  4. ericzoo on September 5th, 2008 4:52 pm

    Hi, this is so nice. I plan to use it as basis for a game… i will be sure to notify you if my game is finished. Can i request a tutorial for a game like the Grow series specifically the seemingly random sequence of animation. thanks. You are a god amongst flash developers.

  5. junior on September 8th, 2008 10:43 pm

    e presiso baixar que programa para mim fazer estes jogos

  6. Koi Games on September 17th, 2008 4:24 pm

    Flash CS 3.

  7. Chrisby on December 8th, 2008 5:52 am

    When you use a button to go to the next frame, the stars still come! How do you stop this?

  8. Sean on February 6th, 2009 3:31 am

    I have the same problem. What I’m trying to do is remove the movieclip at the same time that the event happens to take it to the nexty frame. Yeah, it’s not working. Apparently, I’m missing parameters.

Leave a Reply




Trackbacks

  1. Create a Flash game like Snowflakes - AS3 version : Emanuele Feronato on August 30th, 2008 11:30 pm

    [...] announced in Create a Flash game like Snowflakes, here it is the AS3 [...]

Posts