문제

I am not sure how to make this clear but : Is there a way to allow mouse event register with objects in an array? I have multiple objects being added to stage from an array and i would like to call different functions after said objects are clicked on ? I have this:

        function makeEnemies():void
    {
        //create humans

        var tempEnemy:MovieClip;
        var wolf:MovieClip;
        tempEnemy = new Enemy2();
        tempEnemy.cacheAsBitmap = true;
        tempEnemy.speed = 20;
        tempEnemy.x = Math.round(Math.random() * 800);
        tempEnemy.y = Math.round(Math.random() * 480);
        addChild(tempEnemy);
        enemies.push(tempEnemy);
    }

    function moveEnemies():void
    {
        var tempEnemy:MovieClip;
        for (var i:int =enemies.length-1; i>=0; i--)
        {
            tempEnemy = enemies[i];

            if (tempEnemy.x > stage.stageWidth)
            {
                tempEnemy.x = stage.stageWidth;
            }

            if (tempEnemy.y > stage.stageHeight)
            {
                tempEnemy.y = stage.stageHeight;
            }
            tempEnemy.x +=  Math.round(Math.random() * tempEnemy.speed);
            tempEnemy.y -=  Math.round(Math.random() * tempEnemy.speed);

                tempEnemy.addEventListener(MouseEvent.CLICK, scoreM);

        function scoreM(event:MouseEvent):void
        {
            makeBite(tempEnemy.x, tempEnemy.y);
            removeEnemy(i);
            score++;
            score_txt.text = String(score);
        }

    function removeEnemy(idx:int)
{
    removeChild(enemies[idx]);
    enemies.splice(idx,1);
}

And i get an error

TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/removeChild()
    at veinsVtest_fla::MainTimeline/removeEnemy()
    at MethodInfo-67()
도움이 되었습니까?

해결책

You won't need an Array for this job. Like Man of Snow said. Use event.currentTarget will point to the "clicked on Enemy"

tempEnemy.addEventListener(MouseEvent.CLICK, scoreM);

function scoreM(event:MouseEvent):void
{
    var clickedOnEnemy:MovieClip = event.currentTarget as MovieClip;

    //Now you have your enemy, do whatever you please with him.
    makeBite(clickedOnEnemy.x, clickedOnEnemy.y);

    //And farewell, my enemy ... time to remove him.
    removeChild(clickedOnEnemy);
    clickedOnEnemy = null;

    score++;
    score_txt.text = String(score);
}

* EDIT **

There are several ways to implement "removeAllEnemies". One way to do it is to use have another MovieClip to hold all enemies created.

So create a movieClip and add it to stage.

var enemiesWrapper : MovieClip = new MovieClip();
addChild(enemiesWrapper);

And then instead of add enemy to root

addChild(tempEnemy); //Instead of doing this

Add them to this MovieClip instead.

enemiesWrapper.addChild(tempEnemy);  //Do this instead

Note that your removeChild has to be updated accordingly

enemiesWrapper.removeChild(clickedOnEnemy);

And for "removeAllEnemies" function

function removeAllEnemies() {

    while(enemiesWrapper.numChildren > 0) {
        enemiesWrapper.removeChildAt(0);
    }
}

다른 팁

Replace removeEnemy(i); with removeEnemy(event.currentTarget);

You cannot remove an integer, because it isn't a child. However, I'm assuming you want to remove the MovieClip that was clicked, and it looks like removeEnemy calls removeChild() on the parameter. If not, do you mind showing your removeEnemy function?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top