简体   繁体   English

AS3 MouseEvent.CLICK不同索引上的交互

[英]AS3 MouseEvent.CLICK Interaction on Different Indexes

I'm currently working through a AS3 game tutorial on Lynda.com and am coming across a problem with the MouseEvent.CLICK and child indexes. 我目前正在使用Lynda.com上的AS3游戏教程,我遇到了MouseEvent.CLICK和子索引的问题。 The game is a simple point and shoot, where the player must shoot all of the approaching enemies before they get too close. 游戏是一个简单的指向和射击,玩家必须在他们离得太近之前射击所有接近的敌人。 It works initially, however the custom cursor I added displays behind the enemies. 它最初工作,但我添加的自定义光标显示在敌人后面。 However when I try and adjust the index (I've used the addChildAt function and moving the addChild(cursor) line of code below the enemy container initializer) the on click interaction, which is supposed to remove the enemy when clicked on, doesn't work. 但是,当我尝试调整索引时(我使用了addChildAt函数并将addChild(游标)代码行移动到敌人容器初始化程序下方)点击交互,这应该是在点击时移除敌人,并不是工作。

My document class: 我的文档类:

package {
import flash.display.*;
import flash.utils.*;
import flash.events.*;
import flash.ui.*;

public class Game extends MovieClip {
    public var cursor:Cursor;
    public var enemy:Enemy;
    public var numberOfEnemies:uint;
    public var enemyContainer:MovieClip;
    public var enemyTimer:Timer;

    public function Game() {
        addEventListener(Event.ADDED_TO_STAGE, init);
        Mouse.hide();
    }

    public function init(event:Event):void {
        cursor = new Cursor;
        addChild(cursor);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, dragCursor);

        numberOfEnemies = 10;
        enemyTimer = new Timer(1000, numberOfEnemies);
        enemyContainer = new MovieClip();
        addChild(enemyContainer);

        enemyTimer.addEventListener(TimerEvent.TIMER, createEnemies);
        enemyTimer.start();
    }

    public function dragCursor(event:MouseEvent) {
        cursor.x = this.mouseX;
        cursor.y = this.mouseY;
    }

    public function createEnemies(event:TimerEvent):void {
        enemy = new Enemy();
        enemy.x = 25 + Math.random() * (stage.stageWidth - 75);
        enemy.y = 25 + Math.random() * (stage.stageHeight - 75);
        enemyContainer.addChild(enemy);
        enemy.timerStart();
    }
}
}

My enemy class: 我的敌人班:

package {
import flash.display.MovieClip;
import flash.utils.Timer;
import flash.ui.Mouse;
import flash.events.*;

public class Enemy extends MovieClip {
    public var scaleObj:Number = 0.50;
    public var growTimer:Timer;

    public function Enemy() {
        scaleX = scaleObj;
        scaleY = scaleObj;
        addEventListener(MouseEvent.CLICK, shootEnemy);
    }

    public function timerStart() {
        growTimer = new Timer(50);
        growTimer.addEventListener(TimerEvent.TIMER, objectGrow);
        growTimer.start();
    }

    public function objectGrow(event:TimerEvent):void {
        if(scaleObj <= 1.0) {
            scaleObj += 0.01;
            scaleX = scaleObj;
            scaleY = scaleObj;
        }
        else {
            killEnemy();
        }
    }

    public function killEnemy():void {
        this.parent.removeChild(this);
        growTimer.stop();
    }

    public function shootEnemy(event:MouseEvent):void {
        killEnemy();
    }
}
}

There also is a cursor class, however there is no code beyond the package and class definers. 还有一个游标类,但是包和类定义器之外没有代码。 Please let me know of any questions or comments you might have, thanks. 如果您有任何问题或意见,请告诉我们,谢谢。

Set your Cursor instance to not receive mouse events itself as it would block the click events from getting to the objects behind it. 将Cursor实例设置为不接收鼠标事件本身,因为它会阻止单击事件到达其后面的对象。 Code would be something like 代码就像是

cursor = new Cursor;
cursor.mouseEnabled = false;
cursor.mouseChildren = false;

Most likely the Cursor object is intercepting the mouse click since it is above the Enemy object. 很可能Cursor对象正在拦截鼠标单击,因为它位于Enemy对象之上。

You can stop the Cursor from intercepting mouse events by setting in the cursor class: 您可以通过在游标类中设置来阻止Cursor拦截鼠标事件:

this.mouseEnabled = false;
this.mouseChildren = false;

Also, you should ideally be using a native mouse cursor instead of manually creating your own. 此外,理想情况下,您应该使用本机鼠标光标而不是手动创建自己的光标。 Check out this Adobe tutorial for an example. 查看此Adobe教程以获取示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM