我有两个类,我想从另一个类的文本字段访问一个类的变量或属性,并在每一帧上更新它。这是这两个类的代码。它非常简单,但作为我的第一个项目,它给了问题。如果来自 GametextField.as 类的 RoadRun.as 类,我想要的只是简单访问级别属性
package
{
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class RoadRun extends MovieClip
{
public var carHit:Boolean = false;
public var roadWidth:Number;
public var roadHeight:Number;
public var speed:Number = .5;
private var m_level:Number = 1;
private var levelSpeed:Number= speed * level ;
public function RoadRun()
{
this.addEventListener(Event.ADDED_TO_STAGE, added);
}
private function added(event:Event):void
{
roadHeight = this.height;
roadWidth = this.width;
addEventListener(Event.ENTER_FRAME, move);
}
private function move(event:Event):void
{
//trace(m_level);
m_level = m_level + 0.1;
if(carHit == false)
{
this.y += speed;
speed++;
if(speed >= 10)
{
speed = 10;
}
if(this.y >= 400)
{
this.y = this.height - this.height - this.height; // this.y = 400;
}
}
else if(carHit == true)
{
this.y += speed;
this.speed -= .1;
if(this.y >= 400)
{
this.y = -400;
}
this.y = this.y;
if(this.speed <= 0.0)
{
speed = 0;
}
}
}
public function get level():Number
{
return this.m_level ;
}
}
}
然后在这里我希望它被访问。
package
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.text.TextField;
public class GameTextField extends MovieClip
{
public var score:Number = 0;
private var road:RoadRun = new RoadRun();
public var level:Number;
private var scoreField:TextField = new TextField();
public function GameTextField()
{
addEventListener(Event.ADDED_TO_STAGE, added);
}
private function added(event:Event):void
{
scoreField.background = true;
scoreField.height = 20;
scoreField.width = stage.stageWidth;
scoreField.backgroundColor = 0x000000 ;
scoreField.textColor = 0xFFFFFF;
scoreField.y = 0;
scoreField.x = 0;
addChild(scoreField);
addEventListener(Event.ENTER_FRAME, update);
}
private function update(event:Event):void
{
trace(road.level)
scoreField.text = "Score = " + String(score) + " Level = " + String(level) ;
}
}
}
好吧,一切都在完美地工作,但是,GameTextField 类中的级别没有更新。虽然它在 RoadRun 类中很好(我已经被 trace() 语句破解了。这里
!http://i1363.photobucket.com/albums/r711/fakhar121/GameScreen_zps75a4dc09.png