View Full Version : help for shooting at mouse pointer

12-15-2007, 07:51 PM
Hi everyone,

I'm trying to make a little game with Actionscript 2.0 where the player controls a character that walks around on a map and shoots at things along the way. View is from top to bottom.
Here's what I did so far: the map is bigger than the screen, so the character doesn't move but the map does, giving the illusion that the character is actually the one moving. (I tried making the character move, but I couldn't get the camera to follow him).
The character also rotates according to the position of the mouse pointer, so he keeps pointing at the pointer.

Now my problem is the following: I want the character to shoot at the mouse pointer but I don't know how to do that. Also, the bullets should fire from the tip of the gun, which means the position of the tip of the gun would need to be calculated too every time the character shoots.

I have found a few scripts on this forum and on other websites, but I couldn't adapt them to my game so I think I need some step by step explanation with that script. I would really appreciate it if someone would show me the proper script and explain how I can include it in my project. Thanks in advance for any help I will get.

12-16-2007, 07:55 PM
I've been doing a bit more research and I came across a tutorial that explains how to program a game of Asteroid with ActionScript 1.0
I think it must be possible to take the code in this tutorial and adapt it a little to fit my game, but I need a little help.

Here's the part of the code that I need to use:
function makeShip (){
createEmptyMovieClip("ship", 10000);
ship._x = ship._y = 200;
ship._xscale = ship._yscale = 50;
with (ship) {
lineStyle(2, 0xcccccc, 100);
beginFill(0xffffff, 100);
moveTo(0, -15);
lineTo(10, 15);
lineTo(0, 10);
lineTo(-10, 15);
lineTo(0, -15);
ship.angle = 0; // angle is the ship's rotation angle.
ship.xv = 0; // xv is the ship's x=velocity (going across)
ship.x = 0; // x is the ship's x-position.
ship.y = 0; // y is the ship's y-position.
ship.yv = 0; // yv is the ship's y-velocity (going up/down)
ship.xa = 0; // xa is the ship's x-acceleration (how it speeds up/slows down)
ship.ya = 0; // ya is the ship's y-acceleration (how it speeds up/slows down)
ship.v = 0; // v is the ship's "vector"
ship.visc = 0.02; // the ship has a "viscocity" factor to slow down some response time.
ship.drag = 0; // drag will use the viscocity factor

// Let's shorten up more Math functions.
mp = Math.PI, mpow = Math.pow, msr = Math.sqrt;

// We're going to put a timer in for the firing engine, so we don't fire all our
// lasers at the same time - spacing out the firing to make better shooting.
ship.timer = 0;

// We'll set up an array of object references to each laser.
lasers = new Array();

// Everytime the frame refreshes, all these operations are going to execute.
ship.onEnterFrame = function() {
if (this.timer>0) {
if (Key.isDown(Key.SPACE) && this.timer == 0) {
this.timer = 6;

// BEGIN firing -- lock and load :)
// We want to limit the amount of lasers on the stage at the same time
// to save the processor from bogging down, and from making the game too
// simple to win.
if (_root.lasers.length < 5) {

// We have to first physically create a laser movie clip
lasers.push(_root.createEmptyMovieClip("f"+ _root.depth, _root.depth++));
var id = lasers.length - 1;
var laser = lasers[id];
laser.id = id;

laser.lineStyle(1, 0xcccccc, 100);
laser.beginFill(0xcccccc, 100);
laser.moveTo(-2, -2);
laser.lineTo(2, -2);
laser.lineTo(2, 2);
laser.lineTo(-2, 2);
laser.lineTo(-2, -2);

laser.angle = this.angle;

// alive is a timer used to count how long the laser lasts.
laser.alive = 20;

// we put the laser's starting position at the ship's position.
laser._x = this._x;
laser._y = this._y;

// the laser has to have it's own enterFrame functions.
laser.onEnterFrame = function() {
if (this.alive>0) {
this._y += -15*Math.cos(mp/180*this.angle);
this._x += 15*Math.sin(mp/180*this.angle);
if (this._x <= _root.xmin) {
this._x += _root.xmax-10;
if (this._x >= _root.xmax) {
this._x -= _root.xmax-10;
if (this._y <= _root.ymin) {
this._y += _root.ymax-10;
if (this._y >= _root.ymax) {
this._y -= _root.ymax-10;
} else {
for ( var k in _root.lasers) {
if (_root.lasers[k] == this) {
_root.lasers.splice(k, 1);


So this part of the code is typed on a frame and creates a ship capable of firing lasers if you press the spacebar. (I have removed the part of the code that programs ship movement)
The code is pretty neat, however I need to draw the ship manually with flash and not code it like this script does. The problem is that I don't know how to tell the lasers to shoot from my ship now, and how to use the angle of the ship in order to select which way to shood.
Also, I'm coding with actionscript 2.0 and this code is actionscript 1.0, so I would need it to be "converted" to 2.0.

Can anyone please help me?

(I can post the entire code from the Asteroid game if needed)

12-17-2007, 04:47 AM
For your original problem, when creating a bullet you should:
1) Set bullets's _x and _y to the tip of the ship
2) let diffX = mouse's X position - tip of ship's X position
diffY = mouse's Y position - tip of ship's Y position
3) Set bullet's velocity x = VELOCITY_CONSTANT*(diffX^2/(diffX^2+diffY^2))
Set bullet's velocity Y = VELOCITY_CONSTANT*(diffY^2/(diffX^2+diffY^2))
Where VELOCITY_CONSTANT is the velocity (in pixels) of the bullet.

12-17-2007, 07:56 AM
Thank you very much for your help and time.
I just have one more question: I have a ship movieclip instance called "ship1". How do I tell the bullet to use the x and y coordinates of the ship1 instance?

12-17-2007, 08:20 PM
bullet._x = ship1._x;
bullet._y = ship1._y;

or, if you're using a bullet class use

this._x = _root.ship1._x;
this._y = _root.ship1._y;

12-18-2007, 01:57 AM
The second script is working great, now bullet1 is positioned on ship1 when I test the program.

How am I supposed to use diffX and diffY now? I believe they are variables, is that correct?
Here's the code I wrote and assigned to bullet1 instance:

onClipEvent (enterFrame) {
this._x = _root.ship1._x ;
this._y = _root.ship1._y ;

diffX = _xmouse - _root.ship1._x ;
diffY = _ymouse - _root.ship1._y ;

onClipEvent (enterFrame) {
if (Key.isDown(Key.SHIFT)) {
this._x = diffX ;
this._y = diffY ;


Now when I press Shift, bullet1 which is on top of ship1 disappears. It could be that since I didn't set the bullet's velocity it's actually 'teleporting' somewhere outside the map, in the same direction as the mouse, and that's why I can't see it. So that would mean everything is working fine and I just need to set the bullet's velocity now.
Or maybe my code is just wrong :confused:

In case my code is fine, could you tell me how to set the bullet's velocity? I tried a few things, but so far it's not working.

12-20-2007, 03:25 PM
So right now you're doing enterFrame. The first of tyour two clipevents should be onLoad or onInitilization or some equivalent (might As2.0 is getting rusty).

In that, after doing the diffs do this
this.velX = VELOCITY_CONSTANT*(diffX^2/(diffX^2+diffY^2))
this.velY = VELOCITY_CONSTANT*(diffY^2/(diffX^2+diffY^2))

This will create your bullet's scaled velocity. Then on the second clip event (the enter rame one)
this._x += this.velX;
this._y += this.velY;