I don't know about Flash technology, nor how the game is updated. Presuming that there can be an
Update() method like the one encountered in XNA that is called from within the game loop, would it not be possible to simply calculate:
- the coordinates of your enemy's center, that is, (x2, y2) based on its current location;
- than the coordinates of the projectile's center (if needed) or only its boundaries;
- see if these both coordinates meet sometime, and if they do, this means your enemy's dead?
Once again, I do not pretend that this is doable in Flash, I only try to help the best I can with what I know. As far as I know, Flash has a behind script capability. Now, to make sure a method would be called X times per second might be another story in Flash, I unfortunately can't say.
[...] the question is how to shoot so that the weapon hits it.
Here's my guess:
Let's take some arbitrary coordinates:
(X1, Y1) = (800, 600)
(X2, Y2) = (1000, 50)
(X3, Y3) = (X3, Y3) // Means these are to be determined.
Since this was not mentioned, I extrapolated a constant speed for both the enemy and the projectile.
Ve = d/t = 20 px/s
Vp = d/t = 10 px/s
Then, we have to determine when the projectile could reach the Y3 coordinate:
h = Y1 - Y3
h = 600 - 50
h = 550 px // This means that the projectile has to travel 550 px at 10 px/s to reach Y3
In how much time will the projectile reach Y3?
t = 10 px/s * 550 px
t = 550 px / 10 px
t = 55 s // Then, 55s is required for the projectile to reach Y3
Now, where will the enemy be in 55 seconds?
Ve = d/t = d/55s
20px/s = d/55s
d = 20px * 55s
d = 1100 px
Now, we can determine X3 with the following formula:
X3 = X2 - d
X3 = 1000 - 1100
X3 = -100 // Remember, these are arbitrary coordinates
X3 would then be outside the visible area in this example, which doesn't matter here since the Cartesian plan includes negative coordinates. What matters most is that from this (X3,Y3) coordinate, we'll be able to find the required angle.
We can now determine the size of a new right-triangle following these coordinates:
(X3, Y3) = (-100, 50)
(X1, Y1) = (800, 600)
(X3, Y1) = (-100, 600)
Which allows us to find out the distance on X that the projectile needs to travel:
d = X1 - X3
d = 800 - (-100)
d = 800 + 100
d = 900 px
And we already know the height that the projectile needs to travel on Y which is 550px. Then, we can calculate the hypotenuse according to pytaghorian theory.
sqr(a) + sqr(b) = sqr(c) // Where sqr(c) represents the sqr(hypotenuse)
sqr(c) = sqr(900) + sqr(550)
sqr(c) = 810,000 + 302,500
sqr(c) = 1,112,500
c = sqrt(1,112,500)
c = 1,054.75116 (rounded value)
Then, we can now find the cos(A), where A is the angle which we're looking for.
cos(A) = adjacent side / hypotenuse
cos(A) = 900 / 1,054.75116
cos(A) = 0.853282 (rounded value)
From cos(A) we can now determine angle A:
A = cos-1(cos(A))
A = cos-1(0.853282)
A = 31.42956561 (rounded value)
- Triangle (Wikipedia);
- Finding an Angle in a Right Angled Triangle;
- Trigonometry Calculator - Right Triangles.
I do hope this helped at least a little!