2013-10-15 141 views
0

我有兩個類,我想從另一個類的文本字段訪問類的變量或屬性,並在每一幀更新它。 這裏是這兩個類的代碼。它非常簡單,但作爲我的第一個項目,這是一個問題。 所有我要的是簡單的,如果RoadRun.as類訪問級別財產GametextField.as類AS3 - 訪問數字類型變量從另一個類

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級罰款(我有一絲chacked()語句。 這裏

http://i1363.photobucket.com/albums/r711/fakhar121/GameScreen_zps75a4dc09.png

回答

1

你應該做的是有一個面向對象的方法,在你的類意味着使用封裝。如果你不知道這意味着什麼,那麼它是可以的,例如,如果你有一個變量,你想要訪問它,那麼你應該真的使它變爲私有的,並且設置它自己的公共函數來返回變量。像這樣:

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. 

此外,您還可以使用吸功能,這使得該函數的行爲作爲一個變量,當它不是。例如:

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

}

請評論,如果你感到困惑或有任何進一步的問題。

+0

好吧,它的好,我做了這樣的事情之前,但我的實際問題是(我認爲)邏輯,我的意思是我越來越邏輯的錯誤,我想我的水平(變量)更新每一幀,但似乎像我的水平(變量)凍結1和在TextField遊戲期間它只顯示水平= 1;(水平不更新),即使我在RoadRun類中更新它。 (在第一個if語句之前)我有我的遊戲屏幕截圖,但我不知道如何在這裏發佈它。 –

+0

您可以在[PhotoBucket.com](photobucket.com)免費註冊一個帳戶,並從您的電腦上傳圖片。然後,您可以轉到該圖片,然後Photobucket允許您複製該鏈接,以便您可以將其發佈到此處(只需編輯您的問題,然後點擊圖片並閱讀如何將其插入到您的問題中)就可以了是你的照片的鏈接。 – Xiler

+0

也是,因爲我看不出你提供的兩個類有什麼問題,所以我無法幫你理清錯誤,但我能做的是強烈建議你學習(如果你還沒有)所有關於Flash Pro調試器的信息都是值得的。它會告訴你所有的事情,並可能找出這個問題。但是,如果沒有,那麼跟蹤語句也將幫助您跟蹤這一點。 – Xiler

1
private function update(event:Event):void 
{  
    scoreField.text = "Score = " + String(score) + " Level = " + String(level) 
      + " Speed = " + road.speed + " Road Width = " + road.roadWidth; 
}