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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | // boulders are stored in an array
// [0] is x pos
// [1] is y pos
// [2] is the width
// don't make boulders too large (<50) or the rewind won't work
// until you change slowdown formula
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]];
for (x=0; x<boulders.length; x++) {
// creating the boulders
bould = _root.attachMovie("boulder", "boulder_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
// placing them in random positions and with random dimensions
bould._x = boulders[x][0];
bould._y = boulders[x][1];
bould._width = boulders[x][2];
bould._height = boulders[x][2];
// setting picked attribute as false
// picked = false => the boulder has not been picked by the hook
// picked = true => the boulder has been picked
bould.picked = false;
// function to be executed at every frame for the boulder
bould.onEnterFrame = function() {
// if it's not been picked...
if (!this.picked) {
// check if the hook is in shoot mode and touched the boulder
// you'll see later what do hot_spot_x and hot_spot_y mean
if (pod_status == "shoot" and this.hitTest(hot_spot_x, hot_spot_y, true)) {
// set the hook on rewind mode
pod_status = "rewind";
// mark this boulder as picked
this.picked = true;
// determining the slowdown according to boulder size
slowdown = Math.floor(this._width/5);
}
} else {
// the boulder has been picked, so move it as the hook moves
this._x = hot_spot_x;
this._y = hot_spot_y;
// if the hook status changed to rotate
// (this means: if the hook took a boulder and pulled it out to surface...
if (pod_status == "rotate") {
// remove the boulder
this.removeMovieClip();
}
}
};
}
// placing the hook on stage
_root.attachMovie("pod", "pod", _root.getNextHighestDepth(), {_x:250});
// creating an empty movie clip to draw the rope
_root.createEmptyMovieClip("rod", _root.getNextHighestDepth());
// this is the rotation direction and speed
rotation_dir = 2;
// hook initial status
pod_status = "rotate";
// slowdown malus
slowdown = 0;
// hook movement speed
pod_speed = 4;
// function the hook will execute at every frame
pod.onEnterFrame = function() {
// getting pod status
switch (pod_status) {
case "rotate" :
// moving the hook to the left if player presses left
if (Key.isDown(Key.LEFT)) {
this._x -= pod_speed;
if (this._x<20) {
this._x = 20;
}
}
// moving the hook to the right if player presses left
if (Key.isDown(Key.RIGHT)) {
this._x += pod_speed;
if (this._x>480) {
this._x = 480;
}
}
// if the status is rotate, just rotate the hook according to rotation_dir
this._rotation += rotation_dir;
if (this._rotation == 80 or this._rotation == -80) {
// invert rotation_dir if the hook reaches its minimum (or maximum) rotation allowed
rotation_dir *= -1;
}
break;
case "shoot" :
// the hook has ben shoot
// (re)set slowdown malus to zero
slowdown = 0;
// moving the hook using trigonometry
this._x += 10*Math.cos(dir);
this._y += 10*Math.sin(dir);
// determining the hot spot of the hook
// the hot spot is the lowest corner of the hook (that acts like an harpoon in this case)
hot_spot_x = this._x+40*Math.cos(dir);
hot_spot_y = this._y+40*Math.sin(dir);
// if the hot spot goes off the stage
if (hot_spot_y>400 or hot_spot_x<0 or hot_spot_x>500) {
// then rewind the hook
pod_status = "rewind";
}
// draw a line from the hook starting position to its actual position
// this will simulate the rope
rod.clear();
rod.lineStyle(1, 0x000000);
// once it started from (250,0), now that hook can move I am starting from (start_rope_x, 0)
rod.moveTo(start_rope_x, 0);
rod.lineTo(this._x, this._y);
break;
case "rewind" :
// clearing the rope
rod.clear();
// rewinding the hook...
// it may seem a nonsense determining the hot spot now, but I need id
// to move the boulder (if I have any boulder attached to the hook)
hot_spot_x = this._x+40*Math.cos(dir);
hot_spot_y = this._y+40*Math.sin(dir);
// moving the hook with slowdown malus (if any)
this._x -= (10-slowdown)*Math.cos(dir);
this._y -= (10-slowdown)*Math.sin(dir);
// if the hook returns in its initial position...
if (this._y<0) {
// then reset its position and set its status to rotate
this._y = 0;
this._x = start_rope_x;
pod_status = "rotate";
} else {
// drawing a line as seen in shoot status, but only if the hook is still in the air
rod.lineStyle(1, 0x000000);
rod.moveTo(start_rope_x, 0);
rod.lineTo(this._x, this._y);
}
break;
}
};
// when the mouse is clicked...
_root.onMouseDown = function() {
// if the status is rotate...
if (pod_status == "rotate") {
// save hook heading and convert it to radians
dir = (pod._rotation+90)*0.0174532925;
// setting rope start position... the last hook position
start_rope_x = pod._x;
// set pod status to shoot
pod_status = "shoot";
}
}; |
And here it is the result…
Left/right arrows to move, mousebutton to fire.
Download the source code and enjoy
They can be easily customized to meet the unique requirements of your project.
12 Responses to “Create a Flash game like Gold Miner – step 2”
Leave a Reply
Trackbacks
-
Creating multiple objects for… on
December 19th, 2008 10:59 am
[...] requires much less AS? This is associated with the famous gold miner step 2 tutorial located here: http://www.emanueleferonato.com/2008…-miner-step-2/ Code: // boulders are stored in an array // [0] is x pos // [1] is y pos // [2] is the width // [...]
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)

(12 votes, average: 4.92 out of 5)





thank you!
Can’t wait to see AS3.0 version of Gold Miner example code.
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.
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.
Hi, thanks… im going to develop one soon.
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?
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.
IM GONNA MAKE MY FIRST FLASH GAME!
is it possible to get an as3 version of this prototype ? thanks.
Hey,
I’m wondering how to set the acceleration and friction to the movieClip. I was trying to add to the above code something like this:
power = 0.1;
yspeed = 0;
xspeed = 0;
friction = 0.95;
zwolnienie = 4;
if (Key.isDown(Key.LEFT)) {
xspeed -= power*zwolnienie;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power*zwolnienie;
}
if (Key.isDown(Key.PGUP)) {
yspeed -= power;
}
if (Key.isDown(Key.PGDN)) {
yspeed += power;
}
xspeed *= friction;
yspeed *= friction;
pod._y += yspeed;
pod._x += xspeed;
But it doesn’t work:(
how do i reverse the direction of the pod.
how can i bring the position of the pod to the bottom of the stage