0

我有两个类,我想从另一个类的文本字段访问一个类的变量或属性,并在每一帧上更新它。这是这两个类的代码。它非常简单,但作为我的第一个项目,它给了问题。如果来自 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

4

2 回答 2

1
private function update(event:Event):void
{       
    scoreField.text = "Score = " + String(score) + "  Level = " + String(level)
            + "  Speed = " + road.speed + "  Road Width = " + road.roadWidth;
}
于 2013-10-15T19:23:13.380 回答
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 已更改,因为您不能让该变量具有相同的名称为现在级别的 getter 函数。)

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          
    }
}

}

如果您感到困惑或有任何其他问题,请发表评论。

于 2013-10-15T20:58:02.007 回答