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

variable have different values

New Here ,
Sep 24, 2013 Sep 24, 2013

I have a public var called enemyNumber in my Enemy class.

And in my document class, I added an instance of the Enemy class, and I want to give enemyNumber a number. Let's say 1.

Enemy:

//ENEMY HOLDER

                    public var addMonster:MovieClip = new MovieClip();

  public var enemyNumber:int;

//LIST OF ENEMIES

                    private var croak:Croak = new Croak();

  private var worm:Worm = new Worm();

if (enemyNumber == 1) { addMonster = croak; }

if (enemyNumber == 2) { addMonster = worm; }

addChild(monster);

Main:

public var enemy:Enemy = new Enemy;

addChild(enemy);

enemy.enemyNumber = 1;

Basically I added movie clips of enemies that will equal addMonster depending on the number. I want to declare the number in the main document class because I added an instance of the Enemy class in my main. Problem is, when I try this out, nothing is added. The enemyNumber is 0 when I trace it in Enemy class, but 1 when I trace it in Document class. Is there anyway to relay the value of what I have for enemyNumber in my Document class to the Enemy class?

TOPICS
ActionScript
780
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

LEGEND , Sep 24, 2013 Sep 24, 2013

When you first create the instance is the only time those two if statements will execute, if at all.  At that point in time it will not have been assigned a value.

You will need something in the main file that causes execution of the conditional(s).  You could place them inside a function in the Enemy class and then have the main file call upon that function as needed.

Translate
LEGEND ,
Sep 24, 2013 Sep 24, 2013

The only reference you have to the enemy instance is the instance you create in the main file.  In what way are you trying to access its enemyNumber without going thru the instance that you created in the main file?

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
New Here ,
Sep 24, 2013 Sep 24, 2013

Right. I'm trying to access enemy's enemynumber variable from main file and declare it in main file. But I'm thinking that, since I have an if statement checking to see what enemyNumber equals in my Enemy class, it's not registering the main file declaration.

Perhaps I might need to add those enemies like croak and worm in my main file?

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
LEGEND ,
Sep 24, 2013 Sep 24, 2013

When you first create the instance is the only time those two if statements will execute, if at all.  At that point in time it will not have been assigned a value.

You will need something in the main file that causes execution of the conditional(s).  You could place them inside a function in the Enemy class and then have the main file call upon that function as needed.

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
New Here ,
Sep 24, 2013 Sep 24, 2013

Worked like a charm! THANKS!!!

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
LEGEND ,
Sep 24, 2013 Sep 24, 2013
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