If I set the class variables as private and attempt to "set" them in the setter, function acts like they don't exist. Only way I could get it to work was to set the variables to as "public static". Defeats the purpose doesn't it? I'm assuming I'm misunderstanding something here:
Code: Select all
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class Enemy extends MovieClip
{
private var _vx:int;
private var _vy:int;
private var _enemyHalfWidth:uint;
private var _enemyHalfHeight:uint;
private var _wanderTimer:Timer;
public static var _playerposx:int; //These are the variables in question.
public static var _playerposy:int;
public function Enemy()
{
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
_enemyHalfHeight = height / 2;
_enemyHalfWidth = width / 2;
stop();
_wanderTimer = new Timer(Math.round((Math.random()*5000)));
addEventListener(Event.ENTER_FRAME, eachFrame);
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
_wanderTimer.addEventListener(TimerEvent.TIMER,changePath);
}
private function onRemovedFromStage(event:Event):void
{
removeEventListener(Event.ENTER_FRAME, eachFrame);
removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
removeEventListener(TimerEvent.TIMER,changePath);
}
private function changePath(Event:TimerEvent):void
{
_vx = Math.random() * 3 * (int(Math.random()*2) - 1 | 1);
_vy = Math.random() * 3 * (int(Math.random()*2) - 1 | 1);
}
public static function set playerPosX(playerposx:int):void //THESE TWO SETTERS ARE WHERE THE ERROR IS THROWN UNLESS THE CLASS VARIABLES ARE SET TO PUBLIC STATIC
{
_playerposx = playerposx;
}
public static function set playerPosY(playerposy:int):void
{
_playerposy = playerposy;
}
private function eachFrame(event:Event):void
{
if (Math.abs(x - _playerposx) < 15 || Math.abs(y - _playerposy) < 15)
{
enemyHunt();
}
else
{
enemyWander();
}
}
private function enemyHunt():void
{
//this function is also dependent on the variables _playerposx & _playerposy
}
private function enemyWander():void
{
//does stuff.
}
}
}