0

I'm building a simple space shooter game

using Tweener class I wrote a code that fires a bullet from the hero spaceship current position to the mouse current position

var fire = new Bullet();
        addChild(fire);
        fire.x = ship.x + (ship.width * 0.5);
        fire.y = ship.y
        Tweener.addTween(fire, {x:_me.currentTarget.mouseX, y:_me.currentTarget.mouseY, time: 5} );

the problem is this code makes the bullet stop at the last mouse position and I want the bullet to continue moving in the same direction until it's outside of the stage.

theoretically, the most simple way would be to input x.y position of the mouse as if it was at the same angle but outside of the stage but how can i get those x,y coordinates??

  • sorry, I read again your question and my answer is out of topic, I've deleted it. What you're looking for are the coordinates of the off stage point, this is a trigonometric task – Sr.Richie Jun 18 '13 at 11:24

2 Answers2

1

determine the angle of the bullet. using the angle, consider the origin of the bullet the center of a circle and the first point as a coordinate at the edge of the circle.

for the bullet to follow the same path, its just a larger circle, so the radius will increase.

the angle will be the same and so will the origin.

Discipol
  • 3,137
  • 4
  • 22
  • 41
  • agreed! problem is my math is weak, I'm not sure how to calculate this? – user2496394 Jun 18 '13 at 14:53
  • http://snipplr.com/view/47060/ for angle and http://stackoverflow.com/questions/2887513/find-the-point-with-radius-and-angle for the second point. just increase the radius. – Discipol Jun 18 '13 at 18:42
  • o.k..i think i got it..for example to get my new x and make sure it's outside of the stage i need to create the following equation: NEW.x = fire.x + ((fire.x - _me.currentTarget.mouseX) + stage.width) * cos(angle) just about two thing i'm still not yet sure: 1. how can i calculate the angle? 2. obviously there will times when fire.x is smaller then _me.currentTarget.mouseX, wouldn't that cause this equation to malfunction? – user2496394 Jun 19 '13 at 05:52
  • @user2496394 I gave you two links. The first has the function for the angle. Due to mathmagic, negative angles and negative radius won't make malfunctions. You seem to get the idea. Use stage.stageWidth by the way :P Please accept the answer if it pleases you. – Discipol Jun 19 '13 at 07:16
  • i'm not near my computer right now (on the road with my iphone) but as soon as i get back home I'll test it to accept the answer, thank u so much for your help! – user2496394 Jun 19 '13 at 10:16
  • but what about the radius? i think that (fire.x - _me.currentTarget.mouseX) doesn't represent the radius, does it? shouldn't i find the radius by calculating the distance between the two points using Math.sqrt? – user2496394 Jun 19 '13 at 21:26
  • exactly. using the angle and that radius will give you mouseX/Y. increasing the radius increases the path of the bullet to the next point, which you tween to. – Discipol Jun 19 '13 at 21:49
  • @user2496394 please accept an answer to your question, hopefully mine. – Discipol Jun 21 '13 at 11:36
  • I want to but I can't get this to work, I'm not sure what I'm doing worng and I don't want to waste any more of your time – user2496394 Jun 21 '13 at 14:51
0

I think what you you want is to find a point one the same straight line with (fire.x, fire.y) , (_me.currentTarget.mouseX, _me.currentTarget.mouseY). And the point's x would be stage width.

So assume target point is A(stage.width, targetY)

We get

(targetY - mouseY)/(targetX - mouseX) = (mouseY - fire.y)/(mouseX - fire.x)

So

targetY = (mouseY - fire.y)/(mouseX - fire.x)*(targetX - mouseX) + mouseY;

Hero mouseX is _me.currentTarget.mouseX, mouseY is _me.currentTarget.mouseY

You can set targetX = stage.width + 10, then targetY

So you can get targetY, and do another tween

Tweener.addTween(fire, {x:targetX, y:targetY , time: 5} );

Pan
  • 2,101
  • 3
  • 13
  • 17
  • almost! there is a virtual straight line between (fire.x, fire.y) and (_me.currentTarget.mouseX, _me.currentTarget.mouseY) and I want to find a point on it but that point also need to be bigger stage.width and stage.height so target point can't be simply (stage.width, targetY) because it will fall off from that virtual line ...unless I didn't understand you correctly? – user2496394 Jun 18 '13 at 18:56
  • You can change A's x to whatever you want, then to find to the target Y in above formule. – Pan Jun 19 '13 at 00:58
  • so.. if i understand you correctly the code i'm looking for should look like this? Tweener.addTween(fire, {x:(stage.width+10), y:((targetY - mouseY)/(stage.width - mouseX) = (mouseY - fire.y)/(mouseX - fire.x)), time: 5} ); – user2496394 Jun 19 '13 at 05:54
  • I'm not sure it will work.. probably Flash wont understand what targetY stands for and i'll get an error stating "Cannot assign to a non-reference value" – user2496394 Jun 19 '13 at 06:00
  • (targetX, targetY) is a point, you must get targetY value by the formule, I have edit the answer. – Pan Jun 19 '13 at 06:19
  • i'm not near my computer right now (on the road with my iphone) but as soon as i get back home I'll test it to accept the answer, thank u so much for your help – user2496394 Jun 19 '13 at 10:16
  • so theoretically the following code should do the trick. (but it doesn't.. the bullet goes off in a wrong angle): Tweener.addTween(fire, {x:((_me.currentTarget.mouseY - fire.y)/(_me.currentTarget.mouseX - fire.x)*((stage.stageWidth + 10) - _me.currentTarget.mouseX) + _me.currentTarget.mouseY), y:stage.stageWidth + 10, time: 5 } ); – user2496394 Jun 19 '13 at 20:23
  • O.K! looks like it's working! swapping x with y solved it ..not sure why did i swapped them in the beginning only problem now is this good for only one direction: Left to right, if the mouseX is smaller then the ship current position, the ship will still fire to the right, guess I need to create some kind of 'if' statement that checks if the movement is from left to right or right to left and then changes the formula accordingly (e.g, '-10' instead of 'stage.stageWidth+10') – user2496394 Jun 23 '13 at 09:21