点击鼠标,调用函数?

时间:2013-04-24 15:54:58

标签: actionscript-3 flash

我很难弄清楚如何使我的动画工作,以便在单击一个对象(影片剪辑)时调用一个函数,并且该函数从屏幕上移除对象并显示爆炸。代码如下(来自我的EnemyShip.as文件):

package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.MouseEvent;

public class EnemyShipMed extends MovieClip
{
    var speed:Number;

    function EnemyShipMed()
    {
        this.x = 800;
        this.y = Math.random() * 275 + 75;
        speed = Math.random()*5 + 6;
        addEventListener("enterFrame", enterFrame);
        addEventListener(MouseEvent.MOUSE_DOWN, mouseShoot);
    }

        function enterFrame(e:Event)
        {
            this.x -= speed;
            if(this.x < -100)
            {
                removeEventListener("enterFrame", enterFrame);
                stage.removeChild(this);
            }
        }

        function kill()
        {
            var explosion = new Explosion();
            stage.addChild(explosion);
            explosion.x = this.x;
            explosion.y = this.y;
            removeEventListener("enterFrame", enterFrame);
            stage.removeChild(this);
        }

        function mouseShoot(event:MouseEvent)
        {
            kill();
        }
    }
}

我用十字准线替换了鼠标指针(电影剪辑称为crosshair_mc)。这是我在Main.as文件中的内容:

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.media.Sound;
import flash.media.SoundChannel;

public class Main extends MovieClip {

public var crosshair:crosshair_mc;
var enemyShipTimer:Timer;
var enemyShipTimerMed:Timer;
var enemyShipTimerSmall:Timer;

public function Main()
{
    enemyShipTimer = new Timer(2000);
    enemyShipTimer.addEventListener("timer", sendEnemy);
    enemyShipTimer.start();

    enemyShipTimerMed = new Timer(2500);
    enemyShipTimerMed.addEventListener("timer", sendEnemyMed);
    enemyShipTimerMed.start();

    enemyShipTimerSmall = new Timer(2750);
    enemyShipTimerSmall.addEventListener("timer", sendEnemySmall);
    enemyShipTimerSmall.start();

    //This creates a new instance of the cursor movie clip and adds it onto
    //the stage using the addChild method.
    crosshair = new crosshair_mc();
    addChild(crosshair);

    //Hides the default cursor on the stage so it will not be shown.
    Mouse.hide();

    //Adds an event listener onto the stage with the enter frame event which
    //repeatedly executes the moveCursor function.
    stage.addEventListener(Event.ENTER_FRAME, moveCursor);
}

function sendEnemy(e:Event)
{
    var enemy = new EnemyShip();
    stage.addChild(enemy);
    stage.addChild(crosshair);//brings crosshair to topmost level on stage
}

function sendEnemyMed(e:Event)
{
    var enemymed = new EnemyShipMed();
    stage.addChild(enemymed);
    stage.addChild(crosshair);//brings crosshair to topmost level on stage
}

function sendEnemySmall(e:Event)
{
    var enemysmall = new EnemyShipSmall();
    stage.addChild(enemysmall);
    stage.addChild(crosshair);//brings crosshair to topmost level on stage
}
//This function set the x & y positions of the custom cursor to the x & y positions
//of the default cursor.
function moveCursor(event:Event) 
{
  crosshair.x=mouseX;
  crosshair.y=mouseY;
     }
   }
}

感谢。

1 个答案:

答案 0 :(得分:1)

我的猜测是你的十字准线会捕获所有点击事件,因为它总是在鼠标下面。设置

  crosshair_mc.mouseEnabled = crosshair_mc.mouseChildren = false;

使鼠标事件“透明”。