I would do it this way:
First, create a new actor, call it "crosshair" !
Use a little image like this:
Go to Actor Control and choose Events: Add: Create Actor: Add Action: Follow Mouse to make it stick to your mouse.
Now create a new actor, the player. I used a simple pre-rotated tank (sorry for crappy graphics):
Download BMP tank image strip here: http://www.prodyon-virtual-gear.com/tutorial/mytank.bmp
It contains 36 Images, one image all 10 degrees. Remember: It has to face to east on start and must be rotated counter-clockwise(!)
Now create again a new actor, call it "player" or something. Load up the tank-animation.
Now, we need some (simple) script.
Choose the player-actor, select Events: Add: Draw Actor: Script Editor and type in the following:
- Code: Select all
double mouseangle = direction(xscreen,yscreen, xmouse, ymouse);
animpos = (mouseangle/360.0)*(nframes);
This little code makes sure the Tank will always rotate and face the mouse (e.g. a crosshair).
Some explanations: double mouseangle declares a new variable called "mouseangle" of type double precision (32 bit).
The function "direction" returns the direction between points (x1, y1) and (x2, y2) (in degrees, 0 to 360, from positive x axis, counterclockwise, in degrees). (This function returns double precision values).
x/yscreen and xy/mouse return xy coordinates of screen and mouse position. (AFAIK, x/yscreen return the "on screen" position of an actor).
then we finally use some maths to calculate the image to be shown at that very moment. (nframes returns the number of frames the actor has (animation images)).
Okay back to work!
If you would start the game now, the tank should always face the mouse / the crosshair.
Ok, now lets add the bullet:Create a new actor, call it "bullet" or whatever you like.
Download BMP bullet image strip here: http://www.prodyon-virtual-gear.com/tutorial/bullet.bmp(I´ve made it red here that you can see it better in the game).
Place this bullet also somewhere on your "scene".
Again go into Actor Control. First, choose "Create at startup: No" (upper right corner). Now go into Events: Add: Draw Actor: Script Editor:
- Code: Select all
animpos = (angle/360.0)*(nframes);
This will ensure that it will always face the direction it is traveling.
Next thing: Events: Add: Create Actor: Script Editor:
- Code: Select all
angle = direction(x, y, crosshair.x, crosshair.y);
directional_velocity = 8;
Here we set the bullets angle so that i will face to the mouse(crosshair). Then we apply velocity to it so that it will fly in that direction.
All this is done ONCE. And only when it will be created.
Final step -> Go into Actor Control of your "player". Choose Events: Add: Key Down: (Use e.g. Space Key) Select Repeat: Disable (!). Then choose "Create Actor" as the action from the list.
Choose Actor: bullet and leave the rest as it is.
Done! Now you should be able to shoot bullets in the direction of the mouse (as much bullets as you want).
If i have made any mistakes here, hopefully someone can correct me please
Oh and by the way: You should also check if the bullets are "out of view" and then delete them to save resources / CPU. I leave that to you