2011-01-22 16 views
0

有我命名爲 「TestAPP」,TestAPP.as和Draggable.as我如何進入主班級? +我可以像這樣傳遞函數嗎?

TestAPP.as ActionScript項目的兩個文件:


    package { 
import flash.display.Sprite; 
import flash.display.Stage; 

public class TestAPP extends Sprite 
{ 
    var _mainStage:Stage; 
    public function TestAPP()//This is where we test the UI components. 
    { 

    var sp:Sprite = new Sprite(); 
    _mainStage = stage; 
    _mainStage.addChild(sp); 
    sp.graphics.beginFill(0x00FF00); 
    sp.graphics.drawCircle(0,0,10); 
    sp.graphics.endFill(); 
    sp.x = 50; 
    sp.y = 50; 
    var draggable1:Draggable = new draggable(sp,_mainStage,limitingfunc); 
    } 
    public function limitingfunc(x:Number,y:Number):int{ 
    return 0; 
    } 

} 
} 

而對於draggable.as:


package 
{ 
import flash.display.Sprite; 
import flash.display.Stage; 
import flash.events.MouseEvent; 

public class Draggable 
{ 
    private var _limitingFunc:Function; 
    private var _which:Sprite; 
    private var _MouseSavedX:Number; 
    private var _MouseSavedY:Number; 

    private var _stage:Stage; 
    public function Draggable(which:Sprite,stage:Stage,limitingfunc:Function) 
    { 
    _limitingFunc = limitingfunc; 
    _which = which; 
    _stage = stage; 
    _which.addEventListener(MouseEvent.MOUSE_DOWN,begin_drag); 

    } 
    //limiting func: returns 0 when the object is free to move that place. 
    //returns -1 when the user wants to block X coordinate changes (but maintain Y free) 
    //returns -2 when the user wants to block Y ... 
    //returns -3 or less when the user wants to block both X and Y from changing. 
    //returns 
    private function Return_0(x:Number = 0,y:Number = 0):int{ 
    return 0; 
    } 
    private function begin_drag(ev:MouseEvent):void{ 
    var xTo:Number = _stage.mouseX - _MouseSavedX + _which.x; 
    var yTo:Number = _stage.mouseY - _MouseSavedY + _which.y; 
    var limitingFuncReturnValue:int = _limitingFunc(xTo,yTo); 
    if(limitingFuncReturnValue == 0){//free to move. 
    _which.x = xTo; 
    _which.y = yTo; 
    } 
    else if(limitingFuncReturnValue == -1){//free to move Y 
    _which.y = yTo; 
    } 
    else if(limitingFuncReturnValue == -2){ 
    _which.y = yTo; 
    } 
    //else:do nothing. 
    } 
} 
} 

在「我的動作理論」中,當我點擊它時,我應該看到一個跟在鼠標後面的圓圈。 (可拖動不完全實現)但是該圈甚至沒有預算:(

...我一直在想弄清楚如何訪問主類的舞臺屬性。仍然沒有進展

請幫助這個無助newb !!!我真的很感謝你的幫助:) 謝謝!

回答

1

當您使用大寫命名(並且必須命名爲「Draggable」)時,您正在將第二類實現爲「可拖動」。改變它,看看是否有效。您似乎正確地通過了父類階段。

0

如果TestAPP是您的文檔類。你可以通過stage屬性訪問舞臺(就像你在你的例子中做的那樣)。 如果TestAPP不是文檔類,則應首先監聽ADDED_TO_STAGE事件,然後訪問stage屬性。 沒有必要添加_mainStage屬性,因爲您已經擁有stage屬性。 爲了移動物體,你需要使用ENTER_FRAME事件。

0

您可以像訪問舞臺上的任何DisplayObject一樣訪問主類的stage屬性:使用this.stage

因此,只要這已經足夠了:

var sp:Sprite = new Sprite(); 
stage.addChild(sp); 

然後,您可以通過精靈和主類的階段作爲參數,你做的方式 - 但我會建議創建一個子類Draggable extends Sprite和實例整個事情使用new Draggable()來代替。
只要將新對象添加到顯示列表中,該對象就會自動具有自己的引用,而且restrictFunc也可以是Draggable的成員。另外,您可以使用startDrag()stopDrag()方法,不需要實現您自己的方法:您可以指定Rectangle作爲startDrag()的參數,以限制允許的移動。

0

如果你需要它了這麼多,您可以:

private static var _instance: TestAPP; 
//... 
public function TestAPP(){ 
    _instance = this; 
    //... 
} 

public static function get instance():TestAPP{ 
    return _instance; 
} 

public static function set instance(newVal: TestAPP):void{ 
    _instance = newVal; 
} 

,你就可以從那裏您將TestAPP只是TestAPP.instance.stage進口任何類引用它的階段。
_instanceTestAPP類本身的屬性,而不是其實例

相關問題