4
votes

I need to get access to a variable from another class and I keep getting error '1119: Access of possibly undefined property enemyList through a reference with static type Class.' I can't see what I do wrong since my variable is made 'public' and 'static'.

class where the variable is made.

package classes.enemy
{   
imports ...

public class Enemy extends MovieClip
{
    public static var enemyList:Array = new Array(); **
    var speed:Number;

    public function initialize()
    {
        var stageReff:Stage = this.stage as Stage;
        addEventListener("enterFrame", enterFrame);
    }

    public function Enemy() 
    {
        enemyList.push(this); **
        this.x = 700;
        this.y = Math.random()*200 + 50;
        speed = Math.random()*5 + 5;
    }

    //code
}
}

class that needs access to the variable

package classes.ship 
{
imports ...

public class Bullet extends MovieClip
{
    var speed:Number;

    public function initialize()
    {
        var stageReff:Stage = this.stage as Stage;
        stage.addEventListener("enterFrame", enterFrame);
    }

    //code

    function enterFrame(e:Event):void
    {
        this.x += speed;

        trace(enemy.enemyList); **
    }
}   
}

Putted '**' behind the lines where the problem occurs and where the variable is made, just to make it clear.

Classes are in different folders (classes > enemy & classes > ship), don't know if that has anything to do with it.

Thanks in advance.

4

4 Answers

1
votes

If you want to access a static property, you need to use the class it is defined in: Enemy.enemyList

Also make sure the class is imported properly in your ship's class: import classes.enemy.Enemy;

2
votes

I came across the same problem and the solution is:

your class name is "enemy" and when you access it, it remains as class not object

try to make an object of your class

enemy1 = new enemy(); // in your bullet class

or if bullet is a movieclip in which enemy as its child movieclip, then change its instance name to enemy1 so you can access it as an object of its own class defination "enemy"

1
votes

I had the same problem with error 1119 on as3.

I was pulling my hair off and then I noticed that there was an output message related to TLF text.

So I went to action script settings on the FLA file and merged into code the textlayout library.

Hope this helps.

0
votes

Update 1 :

Don't forget also to import your class Enemy

I suppose that enemy in Bullet is a class instance, so you can't call a static property from an instance you need to call it with the class name where it is declared:

so in Bullet enemy should be Enemy

package classes.ship {
    //...
    import enemy.Enemy;
    //...

    function enterFrame(e:Event):void
    {
     this.x += speed;
     trace(Enemy.enemyList); **
    }