Highlighted

Trouble Creating and Randomizing Location of MovieClip

Community Beginner ,
Apr 23, 2019

Copy link to clipboard

Copied

I am trying to make a game and I want to spawn 5 enemies when the game opens and place them in random places on the screen, but I can't figure it out.

The movieclip I am using is named Enemy and the linkage is named the exact same - Enemy

Here is what I have so far on the first frame::

var enemiesarray:Array;

enemiesarray = [];

function addEnemy():void

{

    if(enemiesarray.length < 5)

    {

      

        var e:Enemy = new Enemy();

        var startX:int = Math.random()*500-10; stage.stageWidth ;

        var startY:int = Math.random()*500-10; stage.stageHeight ;

   

        stage.addChild(e);

      

        enemiesarray.push(e);

    }

}

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

Besides of what Clay said, I want to point out that each of these two lines has an extra semicolon:

var startX:int = Math.random()*500-10; stage.stageWidth;

var startY:int = Math.random()*500-10; stage.stageHeight;

And your function will only generate one enemy if you don't call it more than once.

One suggestion to achieve what you want is like this:

import flash.display.DisplayObjectContainer;

var enemies:Vector.<Enemy> = new Vector.<Enemy>();

// alternatively, you can write:

// var enemies:Array = [];

function addEnemies(container:DisplayObjectContainer, total:uint, minX:int, maxX:int, minY:int, maxY:int):void

{

     for (var i:uint = 0; i < total; i++)

     {

          enemies = new Enemy();

          enemies.x = Math.random() * (maxX - minX) + minX;

          enemies.y = Math.random() * (maxY - minY) + minY;

          container.addChild(enemies);

     }

}

addEnemies(stage, 5, 0, stage.stageWidth, 0, stage.stageHeight);

I hope this helps.

Regards,

JC

Views

250

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Trouble Creating and Randomizing Location of MovieClip

Community Beginner ,
Apr 23, 2019

Copy link to clipboard

Copied

I am trying to make a game and I want to spawn 5 enemies when the game opens and place them in random places on the screen, but I can't figure it out.

The movieclip I am using is named Enemy and the linkage is named the exact same - Enemy

Here is what I have so far on the first frame::

var enemiesarray:Array;

enemiesarray = [];

function addEnemy():void

{

    if(enemiesarray.length < 5)

    {

      

        var e:Enemy = new Enemy();

        var startX:int = Math.random()*500-10; stage.stageWidth ;

        var startY:int = Math.random()*500-10; stage.stageHeight ;

   

        stage.addChild(e);

      

        enemiesarray.push(e);

    }

}

Adobe Community Professional
Correct answer by JoãoCésar | Adobe Community Professional

Besides of what Clay said, I want to point out that each of these two lines has an extra semicolon:

var startX:int = Math.random()*500-10; stage.stageWidth;

var startY:int = Math.random()*500-10; stage.stageHeight;

And your function will only generate one enemy if you don't call it more than once.

One suggestion to achieve what you want is like this:

import flash.display.DisplayObjectContainer;

var enemies:Vector.<Enemy> = new Vector.<Enemy>();

// alternatively, you can write:

// var enemies:Array = [];

function addEnemies(container:DisplayObjectContainer, total:uint, minX:int, maxX:int, minY:int, maxY:int):void

{

     for (var i:uint = 0; i < total; i++)

     {

          enemies = new Enemy();

          enemies.x = Math.random() * (maxX - minX) + minX;

          enemies.y = Math.random() * (maxY - minY) + minY;

          container.addChild(enemies);

     }

}

addEnemies(stage, 5, 0, stage.stageWidth, 0, stage.stageHeight);

I hope this helps.

Regards,

JC

Views

251

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 23, 2019 0
Adobe Community Professional ,
Apr 23, 2019

Copy link to clipboard

Copied

So you're instantiating a new instance of Enemy.

Then you're adding that instance to the stage.

Then you're also pushing a reference to that instance onto enemiesarray.

So why aren't you ever assigning to the .x or .y coordinates of your Enemy instance? If you're intending to write a game you surely already understand how to programmatically position clips on the stage.

Adobe Flash Platform * Changing position

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2019 1
Adobe Community Professional ,
Apr 23, 2019

Copy link to clipboard

Copied

Besides of what Clay said, I want to point out that each of these two lines has an extra semicolon:

var startX:int = Math.random()*500-10; stage.stageWidth;

var startY:int = Math.random()*500-10; stage.stageHeight;

And your function will only generate one enemy if you don't call it more than once.

One suggestion to achieve what you want is like this:

import flash.display.DisplayObjectContainer;

var enemies:Vector.<Enemy> = new Vector.<Enemy>();

// alternatively, you can write:

// var enemies:Array = [];

function addEnemies(container:DisplayObjectContainer, total:uint, minX:int, maxX:int, minY:int, maxY:int):void

{

     for (var i:uint = 0; i < total; i++)

     {

          enemies = new Enemy();

          enemies.x = Math.random() * (maxX - minX) + minX;

          enemies.y = Math.random() * (maxY - minY) + minY;

          container.addChild(enemies);

     }

}

addEnemies(stage, 5, 0, stage.stageWidth, 0, stage.stageHeight);

I hope this helps.

Regards,

JC

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2019 1
Adobe Community Professional ,
Apr 23, 2019

Copy link to clipboard

Copied

You forgot to use the container parameter that's passed into that function.

Also, using enemies so many times instead of a local variable isn't great practice.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2019 0
Adobe Community Professional ,
Apr 23, 2019

Copy link to clipboard

Copied

Fixed the container part. That was a last minute addition.

__________________________________________
HTML5, JSFL, and AS3 samples: http://bit.ly/2mJgDoG

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2019 0