1

I've learned one way to do that, but I want to improve my knowledge. For simplicity I'm not going to use import neither extends in the code below.

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

I've learned it reading a tutorial over internet, but I want to know where I can find more samples on how to reference objects in AS3. For future codes, I want to add hitTest and the like.

Thanks !

3 Answers 3

1

The best place is the ActionScript 3 Reference from Adobe: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/index.html

Here is the specific section on objects: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Object.html

Sign up to request clarification or add additional context in comments.

1 Comment

I'm looking for "References" among objects so that I can avoid Runtime error such as Error #1009: Cannot access a property or method of a null object reference. Sorry, but what you said I already know and it is not what I asked.
0

if you absolutely want to pass a stage reference through an argument to a constructor, you can do so about how you have it laid out (although get rid of the new Stage() call, which won't do anything).

that said, .stage is a property available to all display objects that are in the display list (meaning: the have been added via addChild or addChildAt).

you're probably getting that error trying to reference a .stage property of an object before it's been added to the display list. this is a common error, and can be handled by waiting to reference the .stage property until it has been added, usually using addEventListener(Event.ADDED_TO_STAGE...

so instead of

public class MyObject extends Sprite {
  public function MyObject():void{
    this.x = this.stage.stageWidth/2;
  }
}

you'd use something like this

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

Comments

0

In your example, you don't need do call new Stage() in your CustomObject

public var referenceStage:Stage;

is enough

A hitting function may be found here http://troygilbert.com/2007/06/pixel-perfect-collision-detection-in-actionscript3/

Possible solutions are:

  • Instead of passing the stage object, you can also pass the main object and calling functions in the main object for the custom object
  • Maintain an array in the MainObject with which you want do collisions test.
  • Implementing an Interface (extend an object) with a function which do the hit test agains the array in the MainObject (for example went the EntreFrame Event is fired)
  • Custom Events are the solution for communicating with the main object loosely

Passing a reference to an object in the constructor is a classic OOP pattern

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.