Hey, can someone please make a post to this tutorial, about how to make the same of this, how to shot at mouse click, but without any child adds, everything with symbols. Well I will begin...
Draw a triangle,convert it to mc, give instant name "triangle". Draw a bullet, do the same thing as with the triangle just give an other instant name "shot". Put this actionscript at the first frame.
var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int = 2;
var shotSpeed:int = 10;
function mcFunction(event:Event):void {
xd = triangle.x-stage.mouseX;
yd = triangle.y-stage.mouseY;
radAngle = Math.atan2(yd, xd);
triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
}
stage.addEventListener(Event.ENTER_FRAME , mcFunction);
function shootShot(event:MouseEvent):void {
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
beginFill(0x1E1E1E, 0.5);
moveTo(-size/5,-size);
lineTo(size/5,-size);
lineTo(size/5,size);
lineTo(-size/5,size);
lineTo(-size/5,-size);
}
addChild(shot);
shot.x=triangle.x;
shot.y=triangle.y;
shot.rotation = triangle.rotation;
shot.addEventListener(Event.ENTER_FRAME, moveShot);
}
function moveShot(event:Event) {
with (event.target) {
x+= shotSpeed*Math.sin(rotation*(Math.PI/180 ));
y-= shotSpeed*Math.cos(rotation*(Math.PI/180 ));
if (x>=550||x<=0||y>=400||y<=0) {
event.target.removeEventListener(Event.E NTER_FRAME, moveShot);
this.removeChild(DisplayObject(event.tar get))
;
}
}
}
stage.addEventListener(MouseEvent.MOUSE_
DOWN, shootShot);
When you test it, you see that it uses a bullet which is an actionscripted sprite, not your mc..
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
beginFill(0x1E1E1E, 0.5);
moveTo(-size/5,-size);
lineTo(size/5,-size);
lineTo(size/5,size);
lineTo(-size/5,size);
lineTo(-size/5,-size);
}
addChild(shot);
But if you'll delete that, there will be a bug on clicking. Try to click a few times on .swf file, you'll see that your shoting object restarts at the same point... Figure out what is wrong with it.