Actionscript 3 如何访问主类&x27;s阶段+;我可以像这样传递函数作为参数吗?

Actionscript 3 如何访问主类&x27;s阶段+;我可以像这样传递函数作为参数吗?,actionscript-3,Actionscript 3,在我的actionscript项目中有两个名为“TestAPP”的文件,TestAPP.as和Draggable.as 测试pp.as: package { import flash.display.Sprite; import flash.display.Stage; public class TestAPP extends Sprite { var _mainStage:Stage; public function TestAPP()//This is where

在我的actionscript项目中有两个名为“TestAPP”的文件,TestAPP.as和Draggable.as

测试pp.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.
  }
 }
}
在“我的动作脚本理论”中,当我点击鼠标时,我应该看到一个圆圈跟随鼠标。(拖动功能尚未完全实现)但圆甚至没有移动:(

…我一直在想如何访问主类的stage属性。我在谷歌上搜索过,但仍然没有进展

请帮助这个无助的新手!!!我会非常感谢你的帮助:)
谢谢大家!

您将第二个类实现为“draggable”,当您用大写字母将它命名为(并且必须命名为)“draggable”时。改变它,看看是否有效。不过,您似乎正确地通过了父类阶段

如果TestAPP是您的文档类。您可以通过stage属性访问stage(就像您在示例中所做的那样)。 如果TestAPP不是document类,则应首先侦听添加的_to_STAGE事件,然后访问STAGE属性。 不需要添加_mainStage属性,因为您已经拥有stage属性。
为了移动对象,您需要使用ENTER_FRAME事件。

您可以访问主类的stage属性,方法与您对stage上的任何DisplayObject所做的相同:使用
this.stage

所以这就足够了:

var sp:Sprite = new Sprite();
stage.addChild(sp);
然后,您可以像您那样将sprite和主类stage作为参数传递,但我建议您创建一个子类
Draggable扩展sprite
,并使用
new Draggable()
实例化整个过程。
一旦新对象添加到显示列表中,它将自动拥有自己对stage的引用,limitingFunc也可以是DragTable的成员


此外,您可以使用和方法,无需实现自己的:您可以指定一个矩形作为startDrag()的参数,以限制允许的移动。

如果您非常需要它,您可以:

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;
}
您可以从任何类中引用它的stage,您的
TestAPP
将通过
TestAPP.instance.stage
导入。
但是
\u instance
是类
TestAPP
本身的属性,而不是它的实例