As3 Adding MC from Libary and accesing content inside loaded MC - actionscript-3

I have a movieClip I am loading from the Libary and I have properly LINKED it to export with a name of myMC. This movieclip contains another movieClip and some properties. Lets call the movieClip inside: insideMC.
Here is my code:
function loadScreen()
{
var newMC:MovieClip = new myMC();
addChild(newMC);
loadButtons();
}
function loadButtons()
{
newMC.insideMC.addEventListener(MouseEvent.CLICK, homeButtons);
}
loadScreen();
HOWEVER, when I call the function loadButtons() within the loadScreen() function then I get this error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at iRosary_fla::MainTimeline/loadButtons()[iRosary_fla.MainTimeline::frame1:83]
at iRosary_fla::MainTimeline/loadScreen()[iRosary_fla.MainTimeline::frame1:110]
at iRosary_fla::MainTimeline/frame1()[iRosary_fla.MainTimeline::frame1:103]
It is not seeing the insideMC. Perhaps because it's calling to fast or not loaded yet. It is calling and loading the newMC tho. Just the function loadButtons() is not working because it is not seeing the insideMC movieClip. I am sure this is an easy fix but I can't find it anywhere. Thanks

newMC is a local variable in your loadScreen() method, therefore it has no scope in your loadButtons() method.
Declare newMC as a class member variable and it will have scope in loadButtons()
for example :
// in class declarations
public var newMC:MovieClip;
function loadScreen()
{
newMC = new myMC();
addChild(newMC);
loadButtons();
}
It's important to understand that :
var newMC:MovieClip = new myMC();
Creates a local variable. From your comments, it sounds like you did have newMC as a class variable. So you assumed that the above line was assigning the new instance to your class member newMC, and not the local variable you created.

Not completely sure this is your problem. But to access a movie clip within a movie clip you have to give that "insideMC" an instance name within the first movie clip. Otherwise you'll reference an object that you haven't added to the stage - a null object.
Tutorial on instance names here

Related

This, stage & parent are null

Im trying to access the stage from an external class this is what I have:
Player.as (Main Class)
import flash.display.MovieClip;
public class Player extends MovieClip
{
private var _controls:Controls;
public function Player()
{
// constructor code
_controls = new Controls();
}
}
Controls.as
import flash.display.MovieClip;
public class Controls extends MovieClip
{
private var _playbtn:MovieClip;
public function Controls()
{
trace(this.getChildByName("playbtn"));
}
}
but this line trace(this.getChildByName("playbtn")); always errors I have even tried:
trace(stage.getChildByName("playbtn"));
trace(parent.getChildByName("playbtn"));
But I get the same error:
Null for this and
Cannot access a property or method of a null object reference. for
stage & parent
Is there something I am doing wrong?
I think you are mistaken as to what getChildByName actually does. It returns reference to an object with its name property set to the supplied value. It does not return a reference to an object set to a variable with the supplied name.
For a reference to be returned by getChildByName, you must instantiate an object, set its name to something, and then call addChild on a DisplayObjectContainer with that object. Then you can call getChildByName on the DisplayObjectContainer.
For future reference, stage will be null until the object is added to the stage. parent will be null until the object is used in an addChild call
Display objects get access to their 'stage' property when they are added to the stage. You haven't added the _controls to the stage ( eg addChild(_controls) ) when you try to access the stage property.
Add an event listener (Event.ADDED_TO_STAGE) to the constructor of your Controls class that points to a handler that then checks the stage property.
If you are adding everything to the stage manually, there is no need to create a new instance of Controls in the constructor of Player, you should just retrieve the reference:
_controls = this.controls; //where 'controls' is the instance name you assigned in Flash IDE.
Also, where is the 'playbtn' instace you are trying to retrieve? Is it on the stage or is inside of the Controls instance? As you are tracing this.getChildByName("playbtn"); it HAS TO be inside of Controls with the instance name playbtn assigned in the IDE (or via .name property).
You are getting indeed a null for parent and stage because you have not added the instance to the stage. What you are doing right now:
You instantiate Player, the player then instantiates the Controls (it does not retrieve the reference you have on stage, because you are calling it with the new keyword!)
The new controls have never been added to anything, therefore they have no parent and neither stage.

AS3 Add event listener to a movieclip within a movieclip

I currently have two movieclips one called mcInvFrame, and one called btnCloseInv (It is a movieclip, and I know the naming convention is wrong). btnCloseInv is located inside mcInvFrame. I have two files Inventory.as and my main document class. I can load the mcInvFrame just fine to the stage and everything works as expected. However when I try to access the btnCloseInv movieclip i get errors. Here is the code for Inventory.as I have commented out my most recent failed attempt
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Inventory extends MovieClip
{
public var inv:MovieClip = new mcInvFrame;
public function Inventory()
{
addChild(inv);
/*var invClose:MovieClip = inv.btnCloseInv;
invClose.addEventListener(MouseEvent.CLICK, CloseInventory);
function CloseInventory($e:MouseEvent):void
{
this.parent.removeChild(inv);
}*/
}
}
}
What I need to know is can/should i create a variable within inventory.as for the button that I can access from the main document? If so how?
P.S. I have been searching the forums and trying various solutions but I either didn't understand the implementation or they were not suitable for this situation. The most common error I receive is "Error #1009: Cannot access a property or method of a null object reference." Occasionally I will receive an error stating that an object has no properties.
you cant register event on stage.movieclip.movieclip2 , i have tried to do the same thing before, but it wont work, try to create btnCloseInv outside, then use this code
btnCloseInv.x = mcInvFrame.x + numberHere;
btnCloseInv.y = mcInvFrame.y + numberHere2;
if you doesn't want to use this code, AS3 - Button inside MovieClip triggers MC's event
EDIT: if you set mcInvFrame.buttonMode = true it will not work

AS3 addEventListener to movieclip inside another movieclip

So, I have a movieclip named "LoginScreen" with a instance inside called "confirmbutton".
I want to add the LoginScreen to stage and set a event listener to the button inside it, but I keep getting an error.
This is my code:
var LoginScreen:loginscreen = new loginscreen;
LoginScreen.x = stage.stageWidth / 2;
LoginScreen.y = stage.stageHeight / 2;
addChild(LoginScreen);
LoginScreen.confirmbutton.addEventListener(MouseEvent.CLICK, test);
function test(e:MouseEvent):void{
trace("Sup?");
}
I get the error:
Symbol 'LoginScreen' 1046: Type was not found or was not a compile-time constant: confirmbutton.
I'm pretty sure it exists and its named properly (has that exact name without caps), so I'm guessing its probably a scope problem.
Try using a helper gettter function for getting the inner MovieClip. So where you defined the LoginScreen class put a simple getter function like this one:
public function get ConfirmButton():MovieClip { return this.getChildByName("confirmButton") as MovieClip; }
Then you can access that MovieClip from the LoginScreen object like this:
LoginScreen.ConfirmButton.addEventListener(MouseEvent.CLICK, test);
Figured it out. Renamed the button to "confirmButton" (capital "B") and started working. I guess the problem was that the "confirmbutton" was also the AS Linkage of the movieclip.

movie clip class parameters ane null

I have a movie clip with an external class attached.
here is the MC code (I've shorten it only for the relevant part...)
package {
//all the imports here...
public class mc_masterChapter extends MovieClip {
public function mc_masterChapter() {
trace (picFile,strChapTitle);
}
//Properties
public var picFile:String;
public var strChapTitle:String;
}
}
In the main class file I'm adding this object to stage using addChild:
var masterChapter:mc_masterChapter = new mc_masterChapter;
masterChapter.picFile = "pic_Chap1.jpg";
masterChapter.strChapTitle = "ABCD:
addChildAt(masterChapter,1);
now, the trace in the MC class code gives nulls to both parametes but if i put a trace inside the MC timeline (instead of the attached class code), it gives the right value!
how can I access the values from the MC class itself without getting nuls?
Thank you.
It works! Let me explain:
var masterChapter:mc_masterChapter = new mc_masterChapter; // Calls class constuctor
// so calls trace() too!
// You will get null null
masterChapter.picFile = "pic_Chap1.jpg"; // Assign the variables
masterChapter.strChapTitle = "ABCD"; // so they can be read
trace(masterChapter.picFile, masterChapter.strChapTitle); // Should trace pic_Chap1.jpg ABCD
If you add the following method to your class:
public function test():void {
trace(picFile, strChapTitle);
}
Then call masterChapter.test() it will successfully trace those two properties. So yes, the class can read its properties.
Make the var you use in your main class public static vars.
OK!
I solved the mystery.
I put two traces. one in the main MC class saying "hey, I'm inside the MC - the picFile="
and one in the put Function saying "I'm putting this file into picFile:"
well this is what I've got:
hey, I'm inside the MC - the picFile=null
I'm putting this file into picFile:image.jpg
got it!?! at the moment I asked him to give birth to an instance of the MC (even before putting it on stage - just defining the object (with this line:)
var masterChapter:mc_masterChapter = new mc_masterChapter;
it allready run the class, so of course that in this stage the parameters were not defined allready and were null.
the definition code came right after that line (in the main.as)
masterChapter.pic="pic_Chap1.jpg";
so what I did, was to move all the code from the main class of the MC object to a public function inside the same package called init(). Then I called this function manually from the parent main class.
By that I can decide when to call it (after I declare all the parameters of course).
That's it.
god is hiding in the small details : )
tnx for all the helpers.
Possibly a better solution would be to use a getter/setter pair, so you can know at the exact moment the properties are set:
protected var _picFile:String:
public function get picFile():String {
return _picFile;
}
public function set picFile(value:String):void {
if (value != _picFile) {
_picFile=value;
trace('picFile set to', _picFile);
}
}

Stage and classes

I am new to AS3 and am trying to lean its OOP ways. What I am having problems with is understanding how to access the stage with separate classes.
Here is an example of what I am trying to do:
package game{
import flash.display.*;
public class Main extends MovieClip{
function Main(){
var player = new Player();
var playerBullets = new playerBullet();
addChild(player.players);
}
}
package game{
import flash.display.*;
public class Bullet extends Main // also tried with MovieClip and Sprite{
function Bullet(){
// empty
}
function blah(){
var someSprite = new someSprite();
Main.addChild(someSprite);
stage.addChild(someSprite);
root.addChild(someSprite);
}
}
}
I have Omitted another class which calls the blah method as I feel it is not relevant.
Basically what I want to know is how to add things to the stage in classes as it lookes like I am missing something crucial.
*EDIT TO INCLUDE ERROR*
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at game::Bullet/blah()
at game::Player/fire()
You shouldn't necessarily be extending main to create something like a bullet class, this can be it's own class that extends Sprite or MovieClip. The stage object is considered a global object, as it is a singleton (except in the case of Adobe AIR where you can have one stage per NativeWindow that you spawn). So any object that extends DisplayObject or has DisplayObject in it's inheritance chain will by default have a reference to the stage via a getter, which is populated automatically when a displayObject is added to the display list. This can happen by either adding a clip directly to the root stage object or by adding a clip as a child of another clip, that eventually connects to the stage. For example:
var clip1:MovieClip = new MovieClip();
stage.addChild(clip1); //Clip 1 can now access the stage reference internally.
ver clip2:MovieClip = new MovieClip(); //Right now, clip2 cannot access the stage reference interally.
clip1.addChild(clip2); //Now clip2 can access the internal stage reference because it has been connected to the display list through clip1.
The other mistake people make is accessing stage within a DisplayObject typed class (such as your Main class) without first ensuring that the object itself has been added to the stage. You do this by listening for the Event.ADDED_TO_STAGE event within the constructor of the class, like so:
public class Main extends MovieClip{
function Main(){
if(stage){
//The stage reference is present, so we're already added to the stage
init();
}else{
addEventListener(Event.ADDED_TO_STAGE, init);
}
var player = new Player();
var playerBullets = new playerBullet();
addChild(player.players);
}
private function init(e:Event = null)
{
trace("Added to stage, the stage reference is now populated and stage can be accessed");
}
}
This could be the problem you're having, but it's hard to say since you have not specified any errors. However, this is likely an issue or will be for you, since it's quite common. Inside the init() method you can then set a flag so that when external classes call your Main.blah() method, you can ensure that the stage reference exists before attempting to add something to the stage. Take note however that within your Main class when you simply say:
addChild(someChild);
or
this.addChild(someChild);
you're not adding that child to the stage, but rather to the Main object, which is a MovieClip or Sprite based object that is itself attached to the stage automatically when you set it as the Document class. Hope this info helps.
Update
To explain the display list a little more:
Think of all your movieclips as dishes, and the stage as the table. You can only access the table from the dish, if the dish is placed directly on the table, or if a dish is stacked on top of another dish that touches the table. If you have 10 plates stacked on top of each other, they all touch the table eventually, via their connection to each other. This is essentially a visualization of the flash display list. The way you put dishes on the table is by using addChild(dish). If you have not placed an object somewhere on the table, and try to access the table from that object, you're going to fail. You're getting the "access to undefined" error because you're calling the "blah()" method, which accesses the stage (table) before the bullet (dish) has been added to the stage (table). So you must first either directly add the bullet to the stage, or add it to another object that has already been added to the stage. Change your code like so:
var myBullet:Bullet = new Bullet();
stage.addChild(myBullet);
//Or, if this class, assuming it's the player class, has already been added to the stage, just do this:
this.addChild(myBullet);
myBullet.blah();
Even so, you should still have some error checking within your "blah" method to ensure that the stage is available:
function blah(){
var someSprite = new someSprite();
if(stage){
Main.addChild(someSprite);
stage.addChild(someSprite);
root.addChild(someSprite);
}else{
trace("error, stage not present");
}
}
However you should also note that by adding this child to Main, then stage, then root all in sequence, this does not duplicate the someSprite object. When you add a display object to a new parent object, the object is automatically pulled from it's current parent and moved to the new one. So all this code will do is eventually add someSprite to root, which I believe will fail because root is not a display object, but rather a global reference mainly used to access global objects such as the stage and the Loader object used to load the SWF.
You shouldn't ever be calling stage.addChild. There should be only one child of the Stage, and that's the document class.
You make a MovieClip display on the screen by adding it to the stage's display list.
Stage
+ Main Timeline
+Anything
+Else
+You
+Want
So assuming that Main is your document class for the main timeline...
// inside of Main's constructor...
public function Main(){
var anything:MovieClip = new MovieClip();
var Else:TextField = new TextField();
var you:SimpleButton = new SimpleButton();
var want:Sprite = new Sprite();
this.addChild(anything);
this.addChild(Else);
this.addChild(you);
this.addChild(want);
}
Then in order to add children even lower, for example if you want something to be a child of "Anything" such that you have....
Stage
+ Main Timeline
+Anything
+And
+Everything
+Else
+You
+Want
public function Main(){
var anything:MovieClip = new MovieClip();
var Else:TextField = new TextField();
var you:SimpleButton = new SimpleButton();
var want:Sprite = new Sprite();
this.addChild(anything);
this.addChild(Else);
this.addChild(you);
this.addChild(want);
var And:Sprite = new Sprite();
var everything:Sprite = new Sprite();
anything.addChild(And);
anything.addChild(everything);
}
EDIT: Ascension Systems asks why you should never add any display object directly as a child of the stage. The simplest answer is that you can't ever guarantee that what you believe you're creating as a document class, or as a main timeline in fact actually is going to be used as such. Your use of the stage may later preclude your swf from being loaded as a child of a larger application depending on what it is you've done, exactly. Relying directly on the stage can mean that you're making some assumptions about the nature of the display list that may not hold in the future. That's the way in which it breaks modularity (which is not the same as breaking oop).
Why add to the stage when you could just create your entire application as a MovieClip that is completely self-contained with no reliance on the concept of a "stage" beyond that which is required for learning world coordinates? That way you can be much more modular in your design and you sacrifice nothing.
In some people's work this may be considered an edge case. In my work this has happened both to me when I've created applications that I thought at the time were purely stand-alone that ended up being repurposed later to be a module, and also to swfs that other people created that were intended to be strictly stand-alone, but that I was then to integrate as a module into a larger application. In all cases there were some nasty side effects to contend with. That's where I learned not to rely too closely on the stage for much beyond world coordinates.
Every display object has a property called stage, which is null until that object is added to the display tree.
When you are unsure if an object has been added to the stage, there is a listener you can employ for that purpose:
public class Main extends MovieClip
{
import flash.events.Event;
public function Main():void
{
if(stage) {
init();
} else {
this.addEventListener(Event.ADDED_TO_STAGE,init);
}
}
private function init(evt:Event = null):void
{
this.removeEventListener(Event.ADDED_TO_STAGE,init);
//object is now definitely on the display tree
}
}
I'm gonna take a wild stab in the dark here.
stage is a property implemented something like so:
public function get stage():Stage {
var s:DisplayObject = this;
while(s.parent) s = s.parent;
return s as Stage;
}
root is very similar but stops a level below stage (root is a child of stage).
These properties only work when the object you're calling them on is on the stage somewhere. Doesn't matter where, because the while loop will walk up the hierarchy to get to the stage node at the top. But if it's not on the stage, then parent will be null.
So if your movieclip is not on the stage, then its reference to stage will be null. Same goes for root.
I'm guessing that you're calling blah before the bullets are added to the stage? In which case your call stage.addChild(someSprite) will be a Null Reference error (stage is null).
So you either need to add the bullets to stage first, or you need to pass stage in as a parameter:
function blah(s:Stage){
var someSprite = new someSprite();
s.addChild(someSprite);
}