Create a Flash game like Gold Miner

Today I received an email from a reader asking me to make a tutorial of a game like Gold Miner.

Gold Miner

It’s a Flash demo of a downloadable game, and I think its gameplay is interesting enough to deserve a tutorial.

The main actor in this game is the hook (in the original game, a claw).

The hook can have three states, that I defined this way:

rotate: that’s when the hook is rotating from left to right at the top of the screen. If the player presses the mouse the state turns to…

shoot: the hook moves in a fixed direction (the one it had when the player pressed the mouse) until it hits a gem or flies off the screen. Then the state turns again to…

rewind: the hook returns, in a straight path, to its initial position and changes the state to rotate, allowing the player to shoot again.

Rewind speed is the same as shoot speed if the hook did not take any boulder, while if the hook has a boulder attached, there will be a slowdown according to boulder size

Let’s see the commented code… remember that the hook is linked as pod while the gem as boulder

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
// number of boulders in the game
boulders = 20;
for (x=1; x<=boulders; x++) {
	// creating the boulders
	bould = _root.attachMovie("boulder", "boulder_"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
	// placing them in random positions and with random dimensions
	bould._x = Math.floor(Math.random()*400)+50;
	bould._y = Math.floor(Math.random()*200)+150;
	bould._width = Math.floor(Math.random()*30)+20;
	bould._height = bould._width;
	// 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;
// function the hook will execute at every frame
pod.onEnterFrame = function() {
	// getting pod status
	switch (pod_status) {
		case "rotate" :
			// 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);
			rod.moveTo(250,0);
			rod.lineTo(this._x,this._y);
			break;
		case "rewind" :
			// 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 = 250;
				pod_status = "rotate";
			}
			// drawing a line as seen in shoot status
			rod.clear();
			rod.lineStyle(1,0x000000);
			rod.moveTo(250,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;
		// set pod status to shoot
		pod_status = "shoot";
	}
};

And this is the result

As you can see, the basics have been reproduced.

Download the source code.

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (42 votes, average: 4.67 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.
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 32 comments

  1. Jack Hopkins

    on October 4, 2008 at 5:57 pm

    Cool! I loved this game when I first played it :D

  2. Josh of Cubicle Ninjas

    on October 4, 2008 at 7:15 pm

    Very cool game idea!

  3. Christian

    on October 4, 2008 at 11:12 pm

    hey really cool game…played it for 30mins+ and this is rare for me ;)

  4. Erv

    on October 5, 2008 at 3:22 pm

    A million thanks Emanuele!

    I’ll show you once I finish the game now.

    Erv

  5. Erv

    on October 5, 2008 at 4:01 pm

    Altho I was wondering how is it possible to add the Boulders manually and being able to move the base of the rope left n right.

  6. val

    on October 5, 2008 at 6:03 pm

    this is awesome!!

  7. MalQue

    on October 6, 2008 at 12:18 am

    This reminds me of Double Wire without the swinging. (DW)was AWESOME! but this is cool too.

  8. anonymouse

    on October 6, 2008 at 12:20 pm

    This is great!

    could you show how to also make lead boulders, like there are in the game?

    I keep trying but it stuffs up.

    thanks

  9. lolek

    on October 9, 2008 at 6:16 pm

    I made a game based on this, but i have a problem… the boulders aren’t removed on the next frame (they tay tehre?!?!?) how to remove them?

  10. Create a Flash game like Gold Miner - step 2 : Emanuele Feronato

    on October 10, 2008 at 4:02 pm

    [...] 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 [...]

  11. miff

    on October 13, 2008 at 12:14 am

    @lolek,if you doing with frames you probably use bould_n.removeMovieClip();
    Create some loop like
    for (x=1; x<=boulders; x++)
    {
    [“bould_”+x}.removeMovieClip();
    }
    to clear stage or something…
    or boulders may be in own “layer” then will be
    boulders_ly.removeMovieClip()….
    so many ways to do it, read help and this blog:)

  12. raelz

    on October 25, 2008 at 2:32 pm

    Again, thanks for this great tutorial. I’m a newbie, so don’t consider this as criticising, I am merely trying to understand the code:
    Wouldn’t it be better to determine the hotspot somewhere outside the switch, so it doesn’t have to be determined over and over?

  13. vahidshahi

    on November 12, 2009 at 10:54 pm

    hello.
    can you send c# source code of this game to me?
    fine thanks.

  14. vahidshahi

    on November 13, 2009 at 9:53 am

    hello.
    can you send c# code for this game to me?
    fine thanks.

  15. Triqui’s Picks #5 : Emanuele Feronato

    on November 16, 2009 at 1:43 pm

    [...] difficulty: see something similar here. [...]

  16. Ononmin Flash prototype : Emanuele Feronato - italian geek and PROgrammer

    on December 22, 2009 at 6:20 pm

    [...] To change the game a bit, in this version you can move the “mushroom” along the x axis with the mouse, and the launchable ball will move from left to right just like in Create a Flash game like Gold Miner. [...]

  17. Vikas

    on January 9, 2010 at 7:30 am

    gr8 indeed……….

  18. Rufino Salgado

    on January 17, 2010 at 2:28 am

    I wanted to ask what I needed to get and where I could get it in order to make games like these. Should I know about any specific programming languages in order to do this? Thanks for the help.

  19. vice

    on February 2, 2010 at 1:12 pm

    what about the stones? how you define them and make the hook go slower?

  20. TKK Praneeth

    on April 25, 2010 at 11:43 am

    This is the actionscript 3 code of goldminer. Did this on my own as a mini project in my sem(at present).im a newbie to Flash.Here i get an error saying::
    ArgumentError: Error #1063: Argument count mismatch on gold_edit_fla::MainTimeline/func3(). Expected 0, got 1.

    can any1 fix this error…i have to submit this by 2moro morning….plz….

    code is this:
    var boulders:Number = 20, a:Number=1;
    var pod_status:String = “rotate”;
    var hot_spot_x:Number,hot_spot_y:Number;
    var dir:Number;
    var slowdown:Number=0,rotation_dir:Number = 2;;

    for (a=1; a400 || hot_spot_x500)
    {
    pod_status = “rewind”;
    }
    rod.graphics.clear();
    rod.graphics.lineStyle(1,0×000000);
    rod.graphics.moveTo(250,0);
    rod.graphics.lineTo(this.x,this.y);
    break;

    case “rewind” :
    hot_spot_x = pod1.x+40*Math.cos(dir);
    hot_spot_y = pod1.y+40*Math.sin(dir);
    pod1.x -= (10-slowdown)*Math.cos(dir);
    pod1.y -= (10-slowdown)*Math.sin(dir);
    if (pod1.y<0) {
    pod1.y = 0;
    pod1.x = 250;
    pod_status = "rotate";
    }
    rod.graphics.clear();
    rod.graphics.lineStyle(1,0×000000);
    rod.graphics.moveTo(250,0);
    rod.graphics.lineTo(this.x,this.y);
    break;

    }
    }

    stage.addEventListener(MouseEvent.CLICK,func3);
    function func3()
    {
    if (pod_status == "rotate")
    {
    //var z=pod1.rotation+90;
    //dir = z*0.0174532925;
    //dir = (pod1.rotation+90)*0.0174532925;
    pod_status = "shoot";
    }
    }

  21. TKK Praneeth

    on April 25, 2010 at 11:51 am

    in func3() i commented dir=(pod1.rotation+90)*0.0174532925;

    it is also present…check the code using it…

  22. TKK Praneeth

    on April 30, 2010 at 4:53 am

    Edited the above code a lot…n now im getin d goldminer game…but im having nly dis error.

    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild()
    at gold_edit_name_fla::MainTimeline/func1()

    here pod instance name is pod1 n for boulder is bould.

    var boulders:Number = 20, a:Number=1;
    var pod_status:String = “rotate”;
    var hot_spot_x:Number,hot_spot_y:Number;
    var dir:Number;
    var slowdown:Number=0,rotation_dir:Number = 2;;

    for (a=1; a400 || hot_spot_x500)
    {
    pod_status = “rewind”;
    }
    rod.graphics.clear();
    rod.graphics.lineStyle(1,0×000000);
    rod.graphics.moveTo(250,0);
    rod.graphics.lineTo(pod1.x,pod1.y);
    break;

    case “rewind” :
    hot_spot_x = pod1.x+40*Math.cos(dir);
    hot_spot_y = pod1.y+40*Math.sin(dir);
    pod1.x -= (10-slowdown)*Math.cos(dir);
    pod1.y -= (10-slowdown)*Math.sin(dir);
    if (pod1.y<0) {
    pod1.y = 0;
    pod1.x = 250;
    pod_status = "rotate";
    }
    rod.graphics.clear();
    rod.graphics.lineStyle(1,0×000000);
    rod.graphics.moveTo(250,0);
    rod.graphics.lineTo(pod1.x,pod1.y);
    break;

    }
    }

    stage.addEventListener(MouseEvent.CLICK,func3);
    function func3(evt:MouseEvent):void
    {
    if (pod_status == "rotate")
    {
    dir = (pod1.rotation+90)*0.0174532925;
    pod_status = "shoot";
    }
    }

  23. jeiboy

    on October 13, 2010 at 11:08 pm

    haha very cool tutorial. I want to make my own flash game but the problem is I do not know how to use a flash software.funny.

  24. Victor Michael

    on November 11, 2010 at 5:41 pm

    Hello emmanuele i just want to say it’s indeed a very wonderful moment for me to be here, because indeed i really think i can learn alot from people like you who are ready to teach others ……….. just keep it up and i want you to send me just send me the step’s used in creating the Gold Miners game to mail add talk2every1@yahoo.com

  25. Jon Ace Aparicio

    on January 17, 2011 at 11:14 am

    hello,good day…i really appreciate this game (gold miner) and that’s why during our proposal in our software engineering class, i proposed a game related to the concept of this one. I just have a problem. I still don’t know how to use flash cs4 and and our deadline of submission is fast approaching. I know that it would be hard for me to find someone who would willingly teach me,but im hoping that i would find him/her here…thanks…Godbless

  26. Spellen

    on January 21, 2011 at 9:23 am

    Definitily a good tutorial. Going to try to make a cool game for my gaming website. Thanks for sharing :-)

  27. Akash

    on January 21, 2011 at 11:36 am

    Nice Tutorials ..

  28. Abby

    on February 28, 2011 at 5:16 am

    I’m having a bit of problem with this tutorial, how do you remove the hook/pod and boulders in the next frame? [please give me the exact as2 code/instruction]
    – PLs. & Thanks [pls. reply back]

  29. Stef

    on March 23, 2011 at 8:46 pm

    Thanks, that’s great, really!

  30. lilipop

    on June 5, 2011 at 2:08 am

    how can i move boulders and picked then with the hook

  31. marlon

    on July 4, 2011 at 6:33 am

    what application do u use for building such games? Citrus Engine ??

  32. Heyam

    on September 20, 2011 at 12:56 pm

    waw thats really cool! very good lesson thank you