PDA

View Full Version : [AS3] How to make a "gun" move with mouse?


Zukky
04-19-2010, 03:47 AM
Hi,
i'm new to the whole Actionscript scene and i'm eager to get to grips with Flash CS4 and Actionscript 3.0.

I want to make a mini-game (a prototype) and I want to make a simple target shooter.

I have a gun and a crosshair. I want to move the gun and the crosshair via the mouse x and y co-ordinates.

I have been searching for hours and I can't seem to make it work, I can only make it work using keyboard inputs.

Can someone please point me in the right direction and/or give me the code needed? This would be much appreciated.

Thank you, Zukky.

svenjoypro
04-19-2010, 04:40 AM
This is a tutorial for a custom cursor, but your situation should be similar:
http://www.republicofcode.com/tutorials/flash/cursor_bc/

If you don't want to do it that way, you can look into event listeners, specifically MouseEvent.MOUSE_MOVE.

Artofwork
04-19-2010, 04:40 AM
when you make your object export it to actionscript with the name crossHair

Just changed the name of the object from heart to crosshair
Source:
http://biochimistu.blogspot.com/2008/02/animated-custom-cursor-actionscript-30.html
Mouse.hide();
var angle:Number = 0;
var heartScale:Number = 0.5;
var range:Number = 0.05;
var speed:Number = 10;
var crosshair:crossHair = new crossHair();
addChild(crosshair);

stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function enterFrameHandler(event:Event):void {
crosshair.mouseEnabled = false;
crosshair.x = mouseX;
crosshair.y = mouseY;
crosshair.scaleX = crosshair.scaleY = heartScale + Math.sin(angle) * range;
angle += speed;
}

Zukky
04-19-2010, 05:54 PM
Omg! Thank you so much Artofwork and svenjoypro! That was unbelievably helpful! Artofwork, your code helped me perfectly, now I just need to work out how I'm going to make the gun animate when I left click! Thx peeps :D!