mercredi 23 septembre 2015

AS3 - Shooting Game - hitTestObject

i'm about to finish my project for University. But I'm stuck with the hittestobject.

var Player: gun = new gun();
Player.x = mouseX;
Player.y = mouseY;
addChild(Player);

stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot);
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot);

function mousemove(e: MouseEvent): void
{
        Player.x = mouseX + 200;
        Player.y = mouseY + 35;
}

function shoot(event: Event): void
{
        var Bullet: bullet = new bullet();
        /*var explosion:explo1 = new explo1(); */
        Bullet.x = Player.x;
        Bullet.y = Player.y;
        /*      explosion.x = Player.x;
                explosion.y = Player.y;*/
        Player.rotationX = 5;
        Player.rotationY = 5;
        addChild(Bullet);
        /*      addChild(explosion);*/
        Bullet.addEventListener(Event.ENTER_FRAME, moveBullet);
}

function release_shoot(event: Event): void
{
        var explosion: explo1 = new explo1();
        Player.rotationX = -5;
        Player.rotationY = -5;
}

function moveBullet(e: Event): void
{
        e.target.y -= 12;
        e.target.x -= 96;
        if (e.target.y <= -200 || e.target.x <= -200)
        {
                e.target.removeEventListener(Event.ENTER_FRAME, moveBullet);
                removeChild(MovieClip(e.target));
        }
}

function goesside_1(event: Event): void
{
        mc_target.x -= 2;
        if (mc_target.x < -20)
                mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);

function targeting(event: Event): void
{
        var bullet: MovieClip = MovieClip(event.target);
        if (bullet.hitTestObject(mc_target))
        {
                mc_burst.x = mc_target.x;
                mc_burst.y = mc_target.y;
                mc_burst.gotoAndPlay(2);
                mc_target.x = 200;
                mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
                mc_target.x = 200;
                trace("targerting");
        }
        else if (mc_target.x > 550)
                bullet.removeEventListener(Event.ENTER_FRAME, targeting);
        else
                bullet.y -= 12;
        bullet.x -= 96;
}

The bullet is going in the Target without any doubt, I see it haha... But won't replace mc_target with mc_burst.

EDIT

This is the working code I used for anyone who's interested:

var Player:gun = new gun(); 
Player.x = mouseX; 
Player.y = mouseY; 
addChild(Player); 

stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove); 
stage.addEventListener(MouseEvent.MOUSE_DOWN, shoot); 
stage.addEventListener(MouseEvent.MOUSE_UP, release_shoot); 
function mousemove(e:MouseEvent):void{ 
Player.x = mouseX + 200; 
Player.y = mouseY + 35; 
} 

function shoot(event:Event):void{ 
var bullet1:bullet = new bullet();
/*var explosion:explo1 = new explo1(); */
    bullet1.x = Player.x;
    bullet1.y = Player.y;
/*  explosion.x = Player.x;
    explosion.y = Player.y;*/
    Player.rotationX = 5;
    Player.rotationY = 5;
    addChild(bullet1); 
/*  addChild(explosion);*/
    bullet1.addEventListener(Event.ENTER_FRAME, targeting); 
} 

function release_shoot(event:Event):void{ 
var explosion:explo1 = new explo1(); 
    Player.rotationX =- 5;
    Player.rotationY =- 5;

}

function movebullet(e:Event):void{ 
e.target.y -= 12;
e.target.x -=96;/*When the function is called the targets Y position will be subract by 40 pixels every frame, this makes the movieclip move up. The target is the Bullet movieclip.*/
if(e.target.y <= -200 && e.target.x <= -200 ){ 
e.target.removeEventListener(Event.ENTER_FRAME, movebullet);
removeChild(MovieClip(e.target));
}
}
function goesside_1(event:Event):void {
    mc_target.x -= 2;
        if (mc_target.x < -20)
        mc_target.x = 550;
}
mc_target.addEventListener(Event.ENTER_FRAME, goesside_1);

function targeting(event:Event):void {
    var bullet1:MovieClip = MovieClip(event.target);
    if (bullet1.hitTestObject(mc_target)){
        mc_burst.x = mc_target.y;
        mc_burst.y = mc_target.x;
        mc_burst.gotoAndPlay(2);
        mc_target.x = 200;
        mc_target.removeEventListener(Event.ENTER_FRAME, targeting);
        mc_target.x = 200;
        trace("targerting");
    } 
    else if (mc_target.x > 550){
        bullet1.removeEventListener(Event.ENTER_FRAME, targeting);
    }
    else{
    bullet1.y -= 12;
    bullet1.x -= 96;}

}
// REPLACING CURSOR BY A SIGHT //

import flash.ui.Mouse;

Mouse.hide();

var myCursor:sight = new sight();

myCursor.visible = false;

function init()
{
    addChild(myCursor);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
    stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
    stage.addEventListener(MouseEvent.ROLL_OVER, mouseLeaveHandler);
    stage.addEventListener(MouseEvent.ROLL_OUT, mouseMoveHandler);

}

function mouseMoveHandler(evt:MouseEvent):void
{
    myCursor.visible = true;
    myCursor.x = evt.stageX + 10;
    myCursor.y = evt.stageY + 10;
}

function mouseLeaveHandler(evt:Event):void
{
    myCursor.visible = false;
}
init();

Aucun commentaire:

Enregistrer un commentaire