I would like to make a two player game where tanks shoot eachother, and which is turn based. I need the turret to follow the mouse, the turret to charge up when the mouse button is held down, and the turret to fire when the mouse button is released. I also need some equations for wind and gravity for the bullets.

Chris

to charge something when the mouse is down, add some script like this to the movieclip:

``````onClipEvent(mouseDown) {
//how fast you would like to it increase the charge:
numberOfFrame = 5; //charge will increase by one every 5 frames.
//start Charging...
if (j/numberOfFrames == Math.round(j/numberOfFrames)) {
charge++;
trace(charge);
}
}
onClipEvent(mouseUp) {
//perform action after charging and then reset the charge value:
*//actions*
//reset the charge
charge = 0;
}
``````

that should be it, but i didn’t do that in flash. :-o sorry if it’s not

to make things aim, have a look at this link:

hope this helps.

also: here’s a little note on gravity.
the vertical speed of an object (how fast it is travelling towards the ground - it’s a negative speed if it’s going away from the ground) increases by a constant amount every set amount of time. actual gravitational field strength on earth is 9.81kg/N (how swatty am i )

also, horizontal speed is constant when there is no wind. i’ll come back on later with some more help but i’m at my dad’s house atm and he doesn’t have flash

hope this helps more.

thanks

Here’s the current version. I’m still not sure how to do the gravity equations though.

OK, this took me a while to put together, so i hope it helps. If i get the time I’ll maybe make it into a tutorial for kirupa.com - any questions feel free to ask. # The Finished Article:

Gravity:
the following ignores air resistance

(s = seconds)

Gravity is a force surrounding VERY big objects - like planets. The bigger the object, the stronger the gravity it pulls with.
The gravitational force caused by an object is called that object’s gravitational field. How strong this field is, is called it’s gravitational field strength.
Gravitational Field Strength is measured in kg/N (kilograms per Newton).

An object’s weight is measured in kilograms. The mass of an object is measured in newtons.
On Earth, there is 9.81Kg per newton. No matter what planet an object is on, it’s mass remains the same - however, it’s weight does not, as this is dependant on that planet’s gravitational field strength.

There is a formula that we can use for this.

``````W = m * g
``````

W is the weight of the object in kilograms (Kg).
m is the mass of the object in newtons (N).
g is the gravitational field strength acting on the object in kilograms per Newton (Kg/N).

So, for example if we had an object of 2N on Earth, we would work out it’s weight like this:
m = 2 N //mass is equal to two newtons
g = 9.81 Kg/N //gravitational field strength acting on the object is 9.81 kilograms per newton
W = m * g
W = 2 * 9.81
W = 19.62 Kg

OK, so now we have some background information on gravity you may be wondering how this applies to your game!
Well, before we get onto that, lets learn a little bit more about acceleration and speed with consideration to gravity.

Acceleration:
The acceleration of an object towards a planet is equal to the planet’s gravitational field strength.

(m/s/s = how many meter’s per second the speed will increase by, per second)
(m/s = metres travelled per second)

In the case of Earth, an object accelerates towards the ground at 9.81m/s/s
this means that the object’s speed towards the ground increases by 9.81 m/s every second.
If you hold an object at a height, it’s speed towards the ground is 0m/s - it is not moving yet.
If you let go of the object, it begins to fall. After one second, that object’s speed towards the ground is 9.81m/s (9.81Kg/N * 1s).
After another second of falling, the object’s speed is 19.62m/s (9.81Kg/N * 2s)
After another second of falling, the object’s speed is 29.43m/s (9.81Kg/N * 3s)

An objects speed towards the ground = gravitational field strength * the amount of time it’s been falling.

Coding it!:
If you have an object falling, you might have code like this:

``````onClipEvent(enterFrame) {
this._y += 10; //fall at 10 pixels per frame
}
``````

This code does not look very realistic however because we all know that an object speeds up as it falls towards the ground.
So more accurate code for an object falling towards the ground would be something like this:

``````onClipEvent(enterFrame) {
vSpeed += 9.81; //set the vertical speed that the object is falling at.
this._y += vSpeed; //move the object.
}
``````

Now, you will notice that the object accelerates towards the ground.

Projectiles:
One of the things about objects, is that they do not only move towards and away from the planet - up and down. Sometimes they move from side to side.
Here’s something you’re going to like:
Gravity does not affect horizontal speed at all. Once an object has started moving, it goes the same distance horizontally every second for every second that it travels.
So, if we were to code an object falling, and moving horizontally too we would do something like this:

``````onClipEvent(enterFrame) {
this._x += 10; //the object is moving pixels along every second.
vSpeed += 9.81; //set the vertical speed that the object is falling at.
this._y += vSpeed; //move the object.
}
``````

If you test this code, you’ll find a nice projectile movement.

OK, so you may have some other questions:
"What if i want my object to go up before coming down?"
This is a very simple effect to accomplish if we think about it.
When the object is moving away from the Earth, it is falling towards the Earth at a negative speed.
So to get an effect of something moving away, we would code something along the lines of this:

``````onClipEvent(load) {
vSpeed = -50; //set the vertical speed, towards the ground, as a negative to make the object move away from the ground.

onClipEvent(enterFrame) {
vSpeed += 9.81; //set the vertical speed that the object is falling at.
this._y += vSpeed; //move the object.
}
``````

And there you have it.

Another thing that you may like to incorporate into your game would be wind. Personally, i LOVED Worms2 and wind always played a big part of it.
Earlier, i stated that gravity does not effect the horizontal speed of an object. Wind, however, does. We simply change the horizontal speed of an object according to how strong the wind is.
In this case, a wind blowing left has a negative value and wind blowing right has a positive value.
We might use some code like this to complete the effect.

``````onClipEvent(load) {
windSpeed = 10;
vSpeed = 10;
}
onClipEvent(enterFrame) {
hSpeed += windSpeed;
vSpeed += 9.81;
this._x += hSpeed;
this._y += vSpeed;
}
``````

Remember though, just because Earth’s gravitational field strength is 9.81, does not mean that you have to use 9.81. It is best to play around with these numbers until you find what looks best for your game, as Earth and your game are probably not to the same scale.

hope this helps when I use the one to make it arc, all my bullet dows is go at a diagonal going down and right. could you help?

what’s the exact code that you’re using?

uhh how many projects are you doing please don’t end up like i diod doing like 4 at once

I’m using this:

``````onClipEvent(load) {
windSpeed = 10;
vSpeed = 10;
}
onClipEvent(enterFrame) {
hSpeed += windSpeed;
vSpeed += 9.81;
this._x += hSpeed;
this._y += vSpeed;
}
``````

that codes works perfectly for me. where have you put it and what is the problem, exactly?

I put it on the projectile

hm… it works fine for me. upload the fla?

I changed it around a little, so it’s probably not the same code, but with the code that you gave me, the projectile was going diagonally left and right

ah. yes, i can see exactly why it is doing that, but what i don’t understand is why you’ve coded it like that. that’s not what i showed you in the tutorial. vSpeed is not related to hSpeed at all.

``````vSpeed = -Math.exp(hSpeed);
``````

makes no sense at all.

what you want is something like:

``````vSpeed += x;
``````

if you do it like that, it will work. just like i said…

and also, you’ve copied this bit wrongly: it should not be

``````hSpeed = windSpeed;
``````

but it should be:

``````hSpeed += windSpeed;
``````

and it should just be fine

from

``````this._y += vSpeed;
``````

to

``````this._y -= vSpeed;
``````

something more like this, like i said the first time:

``````onClipEvent(load) {
windSpeed = 4;
vSpeed = -50;
}
onClipEvent(enterFrame) {
hSpeed += windSpeed;
vSpeed += 10;
this._x += hSpeed;
this._y += vSpeed;
}
``````

Okay, you have fixed it for me. Now, I would like to factor in power and angle into that. The angle of the turret is in strange values like negatives and positives, and the power is from zero to 100. So I want it so that IF there is no wind, and the turret is pointing straight up, the bullet will go up some height, according to the power, and come back down, hitting the tank. Could you please help me do that?

Thanks,
Chris

yeah… i’ll have a look at it in a second. what you’d need to do is determine the hSpeed from the angle, and vSpeed according to the angle and power.

it’s pretty simple to do. i’ll do the code for it in a second.