我已经学会了一种方法,但我希望提高自己的知识水平。为简单起见,我不打算在下面的代码中使用import。
1
public class Main
{
public function Main()
{
new MyCustomObject(stage);
}
}
2
public class MyCustomObject
{
public var referenceStage:Stage = new Stage();
public function MyCustomObject(xxx:Stage)
{
this.referenceStage = xxx;
referenceStage.addChild(this);
}
}
我已经学会了通过互联网阅读教程,但我想知道在哪里可以找到更多关于如何在AS3中引用对象的示例。对于将来的代码,我想添加hitTest等。
谢谢!
答案 0 :(得分:1)
最佳位置是Adobe的ActionScript 3参考:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html
以下是有关对象的具体部分:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html
答案 1 :(得分:0)
如果你绝对想通过一个参数将一个阶段引用传递给一个构造函数,你可以这样做(尽管你没有做任何事情)去掉新的Stage()调用。
表示,.stage是显示列表中所有显示对象可用的属性(意思是:已通过addChild或addChildAt添加)。
在将对象添加到显示列表之前,您可能会尝试引用对象的.stage属性。这是一个常见错误,可以通过等待引用.stage属性直到它被添加来处理,通常使用addEventListener(Event.ADDED_TO_STAGE ...
所以而不是
public class MyObject extends Sprite {
public function MyObject():void{
this.x = this.stage.stageWidth/2;
}
}
你会用这样的东西
public class MyObject extends Sprite {
public function MyObject():void{
this.addEventListener(Event.ADDED_TO_STAGE, this.addedHandler, false, 0, true);
}
private function addedHandler(e:Event):void{
this.x = this.stage.stageWidth/2;
}
}
HTH
答案 2 :(得分:0)
在您的示例中,您不需要在CustomObject
中调用新的Stage()public var referenceStage:Stage;
就够了
点击功能可以在http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/
找到可能的解决方案是:
在构造函数中传递对象的引用是一种经典的OOP模式