0

I have a problem, I don't know how to pick a random array .

var enemies:Array;

public function Main() {
enTimer = new Timer(1000);
enTimer.addEventListener("timer", createEnemy);
enTimer.start();
}

private function Enemy1():void {
var enemy1 = new Enemy1();
enemies.push(enemy1);
}

private function Enemy2():void {
var enemy2 = new Enemy2();
enemies.push(enemy2);
}

public function createEnemy():void {
//here is the problem how to pick up a random enemy ??
var EN = enemies[math.round(Math.random() * 2)];

stage.addChild(EN)
}

after this im geting an error:

Implicit coercion of a value of type Array to an unrelated type flash.display:DisplayObject.

1 Answer 1

4

You've got a few problems with your code.

For starters, you're defining two functions, Enemy1 and Enemy2 but you also have what looks like two classes named Enemy1 and Enemy2. I would refrain from calling functions and classes the same name as it can get confusing.

In addition, the reason you are getting the error:

Implicit coercion of a value of type Array to an unrelated type flash.display:DisplayObject.

Is because your Enemy1 or Enemy2 classes do not extend a DisplayObject class and only objects that are of the type DisplayObject can be added to the stage. What object does your Enemy1 and Enemy2 classes inherit from?

Your enemy object class should look something like this as far as inheritance goes to ensure they become a DisplayObject type:

package {
    import flash.display.MovieClip;

    public class Enemy1 extends MovieClip {

        public function Enemy1() {

        }
    }
}

or

package {
    import flash.display.Sprite;

    public class Enemy1 extends Sprite {

        public function Enemy1() {

        }
    }
}

MovieClip and Sprite are two examples of objects that are part of the DisplayObject family of classes.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.