Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Random Number Generator throwing error

Participant ,
Sep 27, 2015 Sep 27, 2015

In my code I have a function chooseTarget which parses through all children of the stage, determines if they have _enemy in their instance name, adds it to an array, and returns a random index of that array.

function chooseTarget():Object

{

  var listofenemies:Array;

  for(var n:uint = 0; n < stage.numChildren; n++){

  if(stage.getChildAt(n).name.indexOf('_enemy', 0) >= 0)

  {

  listofenemies.push(stage.getChildAt(n));

  trace(listofenemies);

  }

  }

  if(n == stage.numChildren)

  {

  var ran:Number = Math.floor(Math.random() * (listofenemies.length - 0 + 1)) + 0; //this gives an error

  var curtarg:Object = listofenemies[ran]

  }

  return curtarg;

}

When this function is called, however, I get

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Pointing to the ran variable.

What is going on?

TOPICS
ActionScript
355
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 27, 2015 Sep 27, 2015

there's no array.  to remedy:

var listofenemies:Array=[];

Translate
Community Expert ,
Sep 27, 2015 Sep 27, 2015

there's no array.  to remedy:

var listofenemies:Array=[];

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Sep 27, 2015 Sep 27, 2015

All right! It works like a charm now.

Thank you!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 27, 2015 Sep 27, 2015
LATEST

you're welcome.

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