问题描述
下面是一个在街道上培养公民的班级.他们左右走动,并被分配给他们将面对的那一侧,他们以什么速度移动以及他们的y位置.我以前在构造函数中使用过函数setUpCitizens(),但由于无法删除级别和侦听器,因此我想使用ADDED_TO_STAGE和REMOVED_FROM_STAGE.
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:
我这样叫课:
var c:Citizen = new Citizen(side,speed,yPos);
这是从父类对象Level1调用的.在尝试引用所有生成的子代并将其全部删除以希望加快游戏速度的过程中,我也遇到了麻烦.
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...
感谢任何建议吗?
推荐答案
在方法内部创建变量时,它们仅存在于该方法内部.您的Citizen构造函数会创建var speed,var size和var yPos,但是一旦该方法完成,这些变量就会丢失.您需要将它们存储在类属性中(例如dx和lastTime).在类属性中添加诸如citizenSpeed:Number之类的内容,然后在您的Citizen构造函数中,将传递的参数分配给该变量,即
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);
}
...
然后,在setUpCitizens()中,仅引用那些成员变量(citizenSpeed等).
And then, in setUpCitizens(), just reference those member variables (citizenSpeed, etc.).
您得到的错误是因为setUpCitizens()包含引用不知道任何变量的代码,因为此时它们不在范围内.
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.
希望有帮助!
B.
这篇关于AS3将构造函数参数作为类中的变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!