Game Design Blog
 
Catapult Pull

Catapult Pull

 

We have some code that detects the down arrow Key being pressed. If the catapult is loaded and the boy is nearby this pulls back the catapult.
The following code is used:

if (mcx.rocks_mc.catapult_mc.arm_mc._rotation>-90){
extrapull = mcx.rocks_mc.catapult_mc.sprite_obj.mass/200;
mcx.rocks_mc.catapult_mc.arm_mc._rotation-=gamevars.pullbackamount+extrapull;
}

The arm of the catapult starts at angle -45 degrees (pointing up and left) and can be pulled back all the way to -90 degrees (pointing left).
The first line of code says to only keep pulling back the arm if its angle is greater than -90 degrees.

Next a value called extrapull is calculated by taking the mass of the rock being fired and dividing it by 200.
This means that heavy rocks pull back the arm faster, this is useful as heavy rocks need the arm to be pulled back further to reach the dam.
Finally we add our normal pullback amount to the extrapull value and subtract this from the arm angle.
This will be shown on the screen.


Catapult Fire

The fun bit! When the down arrow key is released, the rock is launched.
The following code is used:

power = Math.abs(cat_mc.arm_mc._rotation+45);
acceleration = (power*100)/cat_mc.sprite_obj.mass;

rads = Math.PI/4; // 45 degrees
projectile_mc.xspeed = acceleration*Math.sin(rads);
projectile_mc.yspeed = acceleration*Math.cos(rads);

"power" creates a number from 0 to 45 from the current angle of the catapult arm.
A maximum angle of -90 will become a power value of 45. [-90+45=-45; Math.abs() converts this to a positive number]
"acceleration" divides the power by the mass of the rock. Heavier rocks will need more power to accelerate to the same speed.
"rads" is the angle the rock is fired. This is fixed at 45 degrees, the actionscript Math functions works in Radians so this is calculated by dividing Pi by 4 (Pi = 180 degrees).

We separate out the forces acting on our rock into horizontal and vertical motion. This is done so we can easily change the X and Y position of the rock as it moves and also because of gravity which only acts vertically.
We know the angle and the total acceleration so we can use trigonometry to get our X and Y speeds. (imagine acceleration is the hypontenuse of a triangle; y is the opposite side and x is the adjacent)


Rock Flight
Everytime we update the screen the rock needs to change position.
The following code is used on the rock:

this._x+=this.xspeed;
this._y-=this.yspeed;
this.yspeed-=gamevars.gravity;

We incriment the X and Y co-ordinates of our rock by the speed values.
Then we update yspeed by subtracting the force of gravity, this means that yspeed will go down to zero and into negative values. When yspeed is negative the rock will be falling back down.
It is the effect of gravity that creates the arced flightpath of the rock.

 
 
Latest Stuff
TomSka Behind Your Sofa

TomSka Behind Your Sofa

What on earth is that boy up to?

Shetland Pony

Shetland Pony

Who likes tiny horses? I do.

Which Fruit

Which Fruit

Cat Face answers another of your questions.

 
 
Latest Peepl's Stuff
Investiture of the edible

Investiture of the edible

Once upon a perfect peril...

Universithing: Pilot

Universithing: Pilot

So here it is! I'm starting Uni this September, and as...

Drugs, drugs, drugs!

Drugs, drugs, drugs!

A young boy faces the horrors of crack use through the smooth...

 
 
Weebl's Tweets
 
@TheWeebl

Nice to see @tunecore is making it so I can't upload my own work to the jellybug channel.

2014-07-30
@TheWeebl

Note to self. Don't amend scripts while drunk.

2014-07-30
@TheWeebl

RT @AFAblog: If you have little ones in your family the new channel @Jellybug_toons features kid-friendly stuff from @TheWeebl http://t.co

2014-07-30
 
 
 

Roll up, see what awesome stuff we have on offer!

Weebl's Stuff Retro Flight Bags!!

Weebl's Stuff Retro Flight Bags!! - £14.99