我的actionscript项目中有两个名为“TestAPP”的文件,TestAPP.as和Draggable.as
TestAPP.as:
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.
}
}
}
在“我的动作脚本理论”中,当我点击它时,我应该会看到一个跟在鼠标后面的圆圈。 (可拖动的没有完全实现)但是圆圈甚至没有让步:(
...我一直在试图弄清楚如何访问主类的舞台属性。我已经google了,但仍然没有进展。
请帮助这个无奈的新手!!!我真的很感谢你的帮助:) 谢谢!
答案 0 :(得分:1)
当你将第二个类命名为“draggable”时,你将它命名为(并且必须命名)“Draggable”,大写。改变它,看看是否有效。您似乎正确地传递了父类阶段。
答案 1 :(得分:0)
如果TestAPP是您的文档类。您可以通过舞台属性访问舞台(就像您在示例中所做的那样)。 如果TestAPP不是文档类,则应首先侦听ADDED_TO_STAGE事件,然后访问stage属性。 无需添加_mainStage属性,因为您已经拥有了stage属性。 为了移动对象,您需要使用ENTER_FRAME事件。
答案 2 :(得分:0)
您可以像对舞台上的任何DisplayObject一样访问主类的舞台属性:使用this.stage
。
所以这应该足够了:
var sp:Sprite = new Sprite();
stage.addChild(sp);
然后你可以将精灵和主类的阶段作为参数传递,就像你做的那样 - 但我建议创建一个子类Draggable extends Sprite
并使用new Draggable()
来实例化整个事物。登记/>
一旦将新对象添加到显示列表中,它就会自动拥有对stage的引用,而restrictFunc也可以是Draggable的成员。
此外,您可以使用startDrag()和stopDrag()方法,无需实现自己的方法:您可以将Rectangle指定为startDrag()的参数,以限制允许的移动。
答案 3 :(得分: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
导入的任何课程中引用其舞台。
但_instance
是类TestAPP
本身的属性,而不是其实例