我有两个类,我想从另一个类的文本字段访问一个类的变量或属性,并在每一帧上更新它。 这是这两个类的代码。它非常简单,但作为我的第一个项目,它给出了问题。 我想要的只是Simply Access级别属性,如果来自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) ;
}
}
}
一切正常,但是,Level在GameTextField类中没有更新。 虽然它在RoadRun Class中很好(我已经跟踪了trace()语句。
!http://i1363.photobucket.com/albums/r711/fakhar121/GameScreen_zps75a4dc09.png
答案 0 :(得分:1)
private function update(event:Event):void
{
scoreField.text = "Score = " + String(score) + " Level = " + String(level)
+ " Speed = " + road.speed + " Road Width = " + road.roadWidth;
}
答案 1 :(得分:1)
你应该做的是采用OOP方法,这意味着在你的类中使用封装。如果你不知道这意味着什么,那就没关系。例如,如果你有一个你想要访问的变量,那么你应该把它变成私有的,并设置它自己的返回变量的公共函数。像这样:
package {
public class SomeClass {
private var someVar:Number = 12; // A private variable, which means only this class can
// use the reference someVar, and only other outiside classes can use the function getSomeVar.
... // skip some regular class stuff
public function getSomeVar():Number {
return this.someVar; //returns the variable someVar from this class to whoever is accessing it.
//This function is public which means that anyone can call it and get the variable someVar.
}
}
}
要访问该变量,只需引用一个类实例:
var someClass:SomeClass = new SomeClass(); // create the instance using the variable someClass
var myVar:Number = someClass.getSomeVar(); // ACCESSES the variable that you want from the class,
//by first using the class instance reference, and then calling its public function that returns the value you want.
此外,您可以使用getter函数,这使得函数充当变量,而不是。例如:
package {
public class SomeClass {
private var myVar:Number = 12;
...
public function get someVar():Number { // The only difference here, is that
//this class is using a getter function (notice the get before the function name)
//to allow other sources to exclude the parentheses when calling this function, this
//way makes it so you can't pass any parameters,
//but that's ok because you don't need to when your only looking to access some data
//form the class.
return this.myVar;
}
}
}
您可以再次使用对类实例的引用来引用类SomeClass中的变量myVar,但这一次,您要排除括号,因为获取函数被视为来自类外部的变量。
var someClass:SomeClass = new SomeClass();
var myVar:Number = someClass.someVar;
这是处理类之间数据交换的常用方法,因为它是最有效的。下面是你的RoadRun类应该用这种技术看的东西(我只改变它,以便我有一个你希望被另一个类访问的变量的getter函数,并且m_level被改变,因为你不能让那个变量具有相同的name作为getter函数,现在是level。)
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; // this is the variable i want to access.
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
{
level++;
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();
private 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
{
scoreField.text = "Score = " + String(score) + " Level = " + String(road.level); // here, it is referencing it
}
}
}
如果您感到困惑或有任何其他问题,请发表评论。