Create a Flash game like Gold Miner - step 2

I received a lot of emails asking me to add left-right movement to my Gold Miner clone and requiring more information about manually placing the boulders.

In order to add left-right movement, you need to check for left and right arrow keys and move the hook in the proper direction.

Just remember you don't have to let the hook move out of the stage and to update the starting/ending points of the rope.

When you want to manually place boulders, you need an array.

In the one I created, every boulder is an array made this way

[x position, y position, diameter]

Then I created an array containing n boulder arrays where n is the number of boulder I wanted to be in the stage.

Here it is the commented code:

ACTIONSCRIPT:
  1. // boulders are stored in an array
  2. // [0] is x pos
  3. // [1] is y pos
  4. // [2] is the width
  5. // don't make boulders too large (<50) or the rewind won't work
  6. // until you change slowdown formula
  7. boulders = [[100, 100, 30], [200, 100, 40], [300, 100, 40], [400, 100, 30], [100, 200, 30], [200, 200, 40], [300, 200, 40], [400, 200, 30], [100, 300, 30], [200, 300, 40], [300, 300, 40], [400, 300, 30]];
  8. for (x=0; x<boulders.length; x++) {
  9.     // creating the boulders
  10.     bould = _root.attachMovie("boulder", "boulder_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
  11.     // placing them in random positions and with random dimensions
  12.     bould._x = boulders[x][0];
  13.     bould._y = boulders[x][1];
  14.     bould._width = boulders[x][2];
  15.     bould._height = boulders[x][2];
  16.     // setting picked attribute as false
  17.     // picked = false => the boulder has not been picked by the hook
  18.     // picked = true => the boulder has been picked
  19.     bould.picked = false;
  20.     // function to be executed at every frame for the boulder
  21.     bould.onEnterFrame = function() {
  22.         // if it's not been picked...
  23.         if (!this.picked) {
  24.             // check if the hook is in shoot mode and touched the boulder
  25.             // you'll see later what do hot_spot_x and hot_spot_y mean
  26.             if (pod_status == "shoot" and this.hitTest(hot_spot_x, hot_spot_y, true)) {
  27.                 // set the hook on rewind mode
  28.                 pod_status = "rewind";
  29.                 // mark this boulder as picked
  30.                 this.picked = true;
  31.                 // determining the slowdown according to boulder size
  32.                 slowdown = Math.floor(this._width/5);
  33.             }
  34.         } else {
  35.             // the boulder has been picked, so move it as the hook moves
  36.             this._x = hot_spot_x;
  37.             this._y = hot_spot_y;
  38.             // if the hook status changed to rotate
  39.             // (this means: if the hook took a boulder and pulled it out to surface...
  40.             if (pod_status == "rotate") {
  41.                 // remove the boulder
  42.                 this.removeMovieClip();
  43.             }
  44.         }
  45.     };
  46. }
  47. // placing the hook on stage
  48. _root.attachMovie("pod", "pod", _root.getNextHighestDepth(), {_x:250});
  49. // creating an empty movie clip to draw the rope
  50. _root.createEmptyMovieClip("rod", _root.getNextHighestDepth());
  51. // this is the rotation direction and speed
  52. rotation_dir = 2;
  53. // hook initial status
  54. pod_status = "rotate";
  55. // slowdown malus
  56. slowdown = 0;
  57. // hook movement speed
  58. pod_speed = 4;
  59. // function the hook will execute at every frame
  60. pod.onEnterFrame = function() {
  61.     // getting pod status
  62.     switch (pod_status) {
  63.     case "rotate" :
  64.         // moving the hook to the left if player presses left
  65.         if (Key.isDown(Key.LEFT)) {
  66.             this._x -= pod_speed;
  67.             if (this._x<20) {
  68.                 this._x = 20;
  69.             }
  70.         }
  71.         // moving the hook to the right if player presses left   
  72.         if (Key.isDown(Key.RIGHT)) {
  73.             this._x += pod_speed;
  74.             if (this._x>480) {
  75.                 this._x = 480;
  76.             }
  77.         }
  78.         // if the status is rotate, just rotate the hook according to rotation_dir     
  79.         this._rotation += rotation_dir;
  80.         if (this._rotation == 80 or this._rotation == -80) {
  81.             // invert rotation_dir if the hook reaches its minimum (or maximum) rotation allowed
  82.             rotation_dir *= -1;
  83.         }
  84.         break;
  85.     case "shoot" :
  86.         // the hook has ben shoot
  87.         // (re)set slowdown malus to zero
  88.         slowdown = 0;
  89.         // moving the hook using trigonometry
  90.         this._x += 10*Math.cos(dir);
  91.         this._y += 10*Math.sin(dir);
  92.         // determining the hot spot of the hook
  93.         // the hot spot is the lowest corner of the hook (that acts like an harpoon in this case)
  94.         hot_spot_x = this._x+40*Math.cos(dir);
  95.         hot_spot_y = this._y+40*Math.sin(dir);
  96.         // if the hot spot goes off the stage
  97.         if (hot_spot_y>400 or hot_spot_x<0 or hot_spot_x>500) {
  98.             // then rewind the hook
  99.             pod_status = "rewind";
  100.         }
  101.         // draw a line from the hook starting position to its actual position       
  102.         // this will simulate the rope
  103.         rod.clear();
  104.         rod.lineStyle(1, 0x000000);
  105.         // once it started from (250,0), now that hook can move I am starting from (start_rope_x, 0)
  106.         rod.moveTo(start_rope_x, 0);
  107.         rod.lineTo(this._x, this._y);
  108.         break;
  109.     case "rewind" :
  110.         // clearing the rope
  111.         rod.clear();
  112.         // rewinding the hook...
  113.         // it may seem a nonsense determining the hot spot now, but I need id
  114.         // to move the boulder (if I have any boulder attached to the hook)
  115.         hot_spot_x = this._x+40*Math.cos(dir);
  116.         hot_spot_y = this._y+40*Math.sin(dir);
  117.         // moving the hook with slowdown malus (if any)
  118.         this._x -= (10-slowdown)*Math.cos(dir);
  119.         this._y -= (10-slowdown)*Math.sin(dir);
  120.         // if the hook returns in its initial position...
  121.         if (this._y<0) {
  122.             // then reset its position and set its status to rotate
  123.             this._y = 0;
  124.             this._x = start_rope_x;
  125.             pod_status = "rotate";
  126.         } else {
  127.             // drawing a line as seen in shoot status, but only if the hook is still in the air   
  128.             rod.lineStyle(1, 0x000000);
  129.             rod.moveTo(start_rope_x, 0);
  130.             rod.lineTo(this._x, this._y);
  131.         }
  132.         break;
  133.     }
  134. };
  135. // when the mouse is clicked...
  136. _root.onMouseDown = function() {
  137.     // if the status is rotate...
  138.     if (pod_status == "rotate") {
  139.         // save hook heading and convert it to radians
  140.         dir = (pod._rotation+90)*0.0174532925;
  141.         // setting rope start position... the last hook position
  142.         start_rope_x = pod._x;
  143.         // set pod status to shoot
  144.         pod_status = "shoot";
  145.     }
  146. };

And here it is the result...

Left/right arrows to move, mousebutton to fire.

Download the source code and enjoy

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 (10 votes, average: 5 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 game like Gold Miner - step 2”

  1. Erv on October 10th, 2008 4:36 pm

    thank you!

  2. LanoG on October 10th, 2008 8:44 pm

    Can’t wait to see AS3.0 version of Gold Miner example code.

  3. Erv on October 10th, 2008 9:42 pm

    BTW How can we make it so the spike change on picking up and penetrates the boulder, also changing the boulder, with sprites maybe?

    I’m trying but no luck.

  4. Kevin on October 12th, 2008 12:29 am

    Erv, one way you could do this would be to simply create another frame for the pod. Then where the pod “picks” the boulder just add a frame change for the pod.

    I have my pod changing frames when the user clicks the mouse so it opens, then closes when it touches a boulder. Or it closes when it reaches the outside of the level.

  5. Amit on October 12th, 2008 8:58 pm

    Hi, thanks… im going to develop one soon.

  6. Erv on October 13th, 2008 2:30 pm

    Kevin, yes but if you play the real Gold Miner game you will see that when you hit a Bag, the pod penetrates it also differs depending what object you’re picking up.

    Maybe that could be on hit, remove object movie and set pod frame on the frame where u pick up the kind of boulder?

  7. Scarybug on October 13th, 2008 5:06 pm

    I think something is wrong with your RSS Feed. The latest article I have from you is “tips for learning a new language”. It seems to only update the feed sporadically.

  8. Yagura on October 22nd, 2008 1:10 am

    IM GONNA MAKE MY FIRST FLASH GAME!

  9. oliver_l1 on October 29th, 2008 4:19 pm

    is it possible to get an as3 version of this prototype ? thanks.

Leave a Reply