actionscript-3flashclassflash-cs5point

Global Position troubles in actionscript


I'm working on some enemy AI and I want them to turn towards the player every once in a while depending on a timer. All the code is fleshed out, however I'm using multiple separate classes and I'm having a lot of trouble making the player a global point that can be accessed by the enemy AI.

I wrote:

public var playerGlobalPos:Point = localToGlobal(new Point(PlayerTank.x, PlayerTank.y))

But this keeps turning up this error message:

1119: Access of possibly undefined property y through a reference with static type Class.

and I'm not sure if I could just reference the variable playerGlobalPos in the enemy class just like that and have the code jsut recognize it, something tells me it's just not going to work. I'm new to multiple class programming so having variables transfer through to other classes is giving me some trouble.


Solution

  • Apparently your PlayerTank is a class name, not an object (instance) name. Imagine your player controls two tanks, which one's X and Y should be used? So, your enemy should be able to reach the player's tank instance somehow. While there's only one player and one tank, the best way for learning will be using a static var for your AI class.

    public static var player:PlayerTank;
    

    Assign it once with the player instance as you create your battlefield, since it's the place where you place all, AI, player, obstacles etc, and then reference it within AI routines. Such a playerGlobalPoint var can also be static, but note that its X and Y should be updated regularly without calling new Point() if possible. Calling localToGlobal() will still create one Point object per call, but you can avoid creating two.