Be a Supporter!

How do i access objects onstage AS3

  • 330 Views
  • 2 Replies
New Topic Respond to this Topic
HoundByte
HoundByte
  • Member since: Mar. 16, 2011
  • Offline.
Forum Stats
Member
Level 02
Game Developer
How do i access objects onstage AS3 Oct. 6th, 2012 @ 04:29 PM Reply

hey im tring to access an object called "block" on my stage and i put this inside a class that my player is using
PLAYER CLASS
Code:
package {

import flash.display.MovieClip;

public class bot extends MovieClip
{

public function bot()
{
var cube = stage.getChildByName("block");
if (cube.hitTestObject(this))
{

}
}
}

}
i got an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at bot()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()

how do i fix this?


i did not make with ms paint.

MSGhero
MSGhero
  • Member since: Dec. 15, 2010
  • Online!
Forum Stats
Supporter
Level 16
Game Developer
Response to How do i access objects onstage AS3 Oct. 6th, 2012 @ 04:43 PM Reply

You don't do that. The hittest should be inside the game class since the game class has direct access to the player and block classes. The player class does not have direct access to the block class (player didn't addchild the block), so nothing block-related should be inside the player class.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Online!
Forum Stats
Member
Level 26
Programmer
Response to How do i access objects onstage AS3 Oct. 6th, 2012 @ 04:58 PM Reply

At 10/6/12 04:29 PM, HoundByte wrote: how do i fix this?

There are a few issues with your code.

hey im tring to access an object called "block" on my stage

Does this mean you place a Movieclip on the stage of Adobe Flash and gave it an instance name "block"? I will assume that for now.

and i put this inside a class that my player is using

as mentioned above, the player class should not handle its own hittest, because this requires knowledge of the other object you want to test against.
Do the test in your document class, which automatically has a property (variable) "block" that references the object you placed in Flash by hand.

While it makes no sense conceptually, this line is still troublesome:

var cube = stage.getChildByName("block");

First of all, read about what "stage" is. It's really important that you understand that it is not the thing you can drag and drop your MovieClips onto in Adobe Flash, which is also often called "the stage". You should not add things to ".stage"

Secondly, the name of an MC (or DisplayObject, for that matter) which is stored in the property ".name" has nothing to do with the instance name.

These 2 things can be misunderstood easily and will screw things up if that happens. Pay attention.