ok i need help i have say an instance named enemy on the stage, and i want it so when the shot hits the enemy i want the enmy to go to x =900 however i have yet to figure this out i have tried
if (this.hitTestObject(enemy)) {
enemy.x +=900;
near the bottom but, all this does is when the mouse clicks the enemy goes away
please help!!!
// imports the events ie- ENTER_FRAME ect * means all of them
var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int = 1;
// size of the shot
var shotSpeed:int = 20;
// defines some variables
function mcFunction(event:Event):void {
// you will notice that there is no longer _xmouse, it is now mouseX
// _x also changed to x (same with y)
// Void is now a lowercase void
xd = test.x-stage.mouseX;
yd = test.y-stage.mouseY;
// finds the x and y difference of the sprite and the mouse
radAngle = Math.atan2(yd, xd);
// finds the angle in radians (flash works in radians not degrees)
// uses PI because radians work with PI (not the tasty kind either)
}
stage.addEventListener(Event.ENTER_FRAME , mcFunction);
function shootShot(event:MouseEvent):void {
var shot:Sprite = new Sprite();
with (shot.graphics) {
lineStyle(1, 0x000000, 1);
moveTo(-size/2,-size);
lineTo(size/2,-size);
lineTo(size/2,size);
lineTo(-size/2,size);
lineTo(-size/2,-size);
}
addChild(shot);
// draws and attaches
shot.x=test.x;
shot.y=test.y;
shot.rotation = int(radAngle*360/(Math.PI*2)-90);
// sets the shot x,y, and rotation to the triangles
shot.addEventListener(Event.ENTER_FRAME, moveShot);
// adds and enterFrame to the shot, to move it
}
function moveShot(event:Event) {
with (event.target) {
// with the object that called the event
x+= shotSpeed*Math.sin(rotation*(Math.PI/180 ));
y-= shotSpeed*Math.cos(rotation*(Math.PI/180 ));
/*seeee here no worky:(
if (this.hitTestObject(enemy)) {
enemy.x +=900;
}
*/
// moves the shot depending on its rotation, uses build in math sin and cos functions
// remember that flash works in radians
if (x>=550||x<=0||y>=400||y<=0) {
// if the shot is out of the screen
event.target.removeEventListener(Event.E NTER_FRAME, moveShot);
this.removeChild(DisplayObject(event.tar get));
// removes the shot sprite and the enterFrame on it
}
}
}
stage.addEventListener(MouseEvent.MOUSE_
DOWN, shootShot);
// adds a mouseListener, when the mouse is pressed