Prototype of a Flash game like GearTaker
Posted by Emanuele Feronato on 05/6/08 in Actionscript 2, Flash, Game design
Do you know Tony Pa's GearTaker game?
You have a buddy jumping around rotating gears. As most Tony Pa's games, it's easier to play than to explain, and it's hard to link... from this page select "GearTaker" and play.
I made a Flash prototype using two objects, the cog and the hero

The prototype has the following features:
1) Commented code
2) (virtually) unlimited fully customizable gears
3) Player starting gear and offset
Do you want more?
The only feature I am not showing you at the moment is how to determine the angle offset when the hero jumps from a cog to another. In my prototype, the hero always places himself at the top of the gear.
Also, if the hero files out of the stage, you'll have to reload the page.
Can you improve the prototype?
-
// constant to convert degrees to radians
-
degrees_to_radians = 0.0174532925;
-
// constant to convert radians to degrees
-
radians_to_degrees = 57.2957795;
-
// arrays storing cogs information: x position, y position and diameter
-
cog_x_position = Array(100, 400);
-
cog_y_position = Array(200, 200);
-
cog_diameter = Array(100, 100);
-
// diameter of the hero
-
hero_diameter = 24;
-
// speed of the hero, when he'll fly
-
hero_speed = 5;
-
// flag to determine if the hero has to rotate attached to the cog (true) or not (false)
-
rotate = true;
-
// cog where the hero will rotate. The starting one
-
cog_to_rotate = 0;
-
// offset to align the hero to the top of the inner circle of the cog
-
angle_offset = -Math.PI/2;
-
// cycle scanning through all cogs
-
for (x=0; x<cog_x_position.length; x++) {
-
// attaching the cog
-
wheel = _root.attachMovie("cog", "cog_"+x, x, {_x:cog_x_position[x], _y:cog_y_position[x], _width:cog_diameter[x], _height:cog_diameter[x]});
-
// setting a random rotation speed for the cogs
-
wheel.speed = Math.random()*20-10;
-
wheel.onEnterFrame = function() {
-
// update cog rotation according to its speed
-
this._rotation += this.speed;
-
};
-
}
-
// placing the hero on the screen
-
_root.attachMovie("hero", "hero", _root.getNextHighestDepth());
-
// function the hero will execute every frame
-
hero.onEnterFrame = function() {
-
// if the hero is supposed to rotate sticked to the cog...
-
if (rotate) {
-
// determining hero's angle
-
angle = (_root["cog_"+cog_to_rotate]._rotation)*degrees_to_radians+angle_offset;
-
x = _root["cog_"+cog_to_rotate]._x;
-
y = _root["cog_"+cog_to_rotate]._y;
-
// no!!! this wont work
-
//radius = (_root["cog_0"]._width+hero_diameter)/2;
-
// yes !!
-
radius = (cog_diameter[0]+hero_diameter)/2;
-
// updating hero position
-
this._x = x+Math.cos(angle)*radius;
-
this._y = y+Math.sin(angle)*radius;
-
// if the hero is flying
-
} else {
-
// updating hero's x and y position
-
this._x += hero_speed*Math.cos(shooting_angle);
-
this._y += hero_speed*Math.sin(shooting_angle);
-
// scanning all cogs
-
for (x=0; x<cog_x_position.length; x++) {
-
// if it's not the cog the hero just left...
-
if (x != cog_to_rotate) {
-
// checking the distance bewteen the hero and the cog
-
dist_x = _root["cog_"+x]._x-this._x;
-
dist_y = _root["cog_"+x]._y-this._y;
-
distance = Math.sqrt(dist_x*dist_x+dist_y*dist_y);
-
// if the hero hits a cog
-
if (distance<(cog_diameter[x]+hero_diameter)/2) {
-
// time to calculate a new angle offset if you want
-
// stick the hero to the cog
-
cog_to_rotate = x;
-
// make the hero rotate around the cog
-
rotate = true;
-
}
-
}
-
}
-
}
-
};
-
// if the player presses the mouse
-
onMouseDown = function () {
-
// checking hero's angle
-
dist_x = hero._x-_root["cog_"+cog_to_rotate]._x;
-
dist_y = hero._y-_root["cog_"+cog_to_rotate]._y;
-
shooting_angle = Math.atan2(dist_y, dist_x);
-
// dont' make the hero rotate sticked to the cog anymore
-
rotate = false;
-
};
Mouse button to play. Happy jumping. Yes, this is a one-button game, and I have a next-to-be-published incredible game based upon this prototype.
Prototypes are the most interesting ways to make a game, remember what I was able to make from this to this...
And this is the source code, just in case you need it...
If you liked this post buy me a beer (or two) » 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.

tag this
JDog | May 6, 2008 | Reply
Its good, can’t wait to see what you are going to do with this !
Jack Hopkisn | May 6, 2008 | Reply
I forsee an awesome game coming out of this :D
Galaxian | May 6, 2008 | Reply
I’ve seen an awesome game like this. Miniclip’s Wheels of Salvation.
Andre | May 6, 2008 | Reply
In between lines 62-63 add…
*************************************************
angle = Math.atan2(dist_y, dist_x);
angle_offset = angle - _root["cog_"+x]._rotation;
*************************************************
This will make the hero stick to the place where it hit the cog.
Emanuele Feronato | May 7, 2008 | Reply
Wrong!
you’re mixing degrees with radians…
JDog | May 7, 2008 | Reply
Don’t you just correct it using radians ? the code could stil have some use….I imagine ?
Xavi | May 7, 2008 | Reply
ya, I played wheels of salvatin too.
What’s different though is that the guy doesn’t automatically go to a predefined point.
it just continues from the point where it hit the wheel/gear.
Emanuele Feronato | May 7, 2008 | Reply
No, it would attach at the opposite side of the wheel
the correct angle_offset is determined by
*************************
angle_offset = angle-_root["cog_"+x]._rotation*degrees_to_radians+Math.PI;
*************************