Reputation:
Below is a class to generate citizens in a street. They walk both left and right and are assigned what side they will be facing, what speed they move and their y position. I previously used the function setUpCitizens() inside the constructor but I wanted to use ADDED_TO_STAGE and REMOVED_FROM_STAGE because I am having trouble removing the level and listeners.
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
import flash.utils.Timer;
public class Citizen extends MovieClip
{
private var dx:Number;// speed and direction
private var lastTime:int;// animation time
public function Citizen(side:String, _speed:Number, yPos:Number)
{
var side = side;
var speed = _speed;
var yPos = yPos;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(event:Event):void
{
setUpCitizens();
addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
}
private function onRemovedFromStage(event:Event):void
{
removeAllCitizens();
this.removeEventListener(Event.ENTER_FRAME,moveCitizen);
}
private function setUpCitizens():void
{
if (side == "left")
{
this.x = -40;// start to the left
dx = _speed;// walk left to right
this.scaleX = 1;// reverse
}
else if (side == "right")
{
this.x = 5720;// start to the right
dx = - _speed;// walk right to left
this.scaleX = -1;
}// not reverse
this.y = yPos;// vertical position
this.gotoAndStop(Math.floor(Math.random()*9+1));
// set up animation;
this.addEventListener(Event.ENTER_FRAME,moveCitizen);
lastTime = getTimer();
}
private function removeAllCitizens():void
{
removeEventListener(Event.ENTER_FRAME,moveCitizen);
parent.removeChild(this);
}
I am getting this error:
I call the class like this:
var c:Citizen = new Citizen(side,speed,yPos);
This is called from the parent class object Level1. I am also having trouble trying to reference all the generated children and remove them all to hopefully speed up the game...
Thanks for any suggestions?
Upvotes: 0
Views: 2460
Reputation: 47
When you create variables inside a method, they only exist inside that method. Your Citizen constructor creates var speed, var size, and var yPos, but as soon as that method finishes, those variables are lost. You need to store them in class properties (like your dx and your lastTime). Add something like citizenSpeed:Number to the class properties, and then in your Citizen constructor, assign the passed parameter to that variable, i.e.
public class Citizen extends MovieClip
{
private var dx:Number;// speed and direction
private var lastTime:int;// animation time
//added lines
private var citizenSpeed:Number;
private var citizenSide:String;
private var citizenYPos:Number;
public function Citizen(_side:String, _speed:Number, _yPos:Number)
{
citizenSide = _side;
citizenSpeed = _speed;
citizenYPos = _yPos;
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
...
And then, in setUpCitizens(), just reference those member variables (citizenSpeed, etc.).
The errors you're getting are because setUpCitizens() contains code that is referencing variables it doesn't know anything about, because they're out of scope at that point.
Hope that helps!
B.
Upvotes: 2