Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 04-14-2012, 05:57 AM   #1
LanLing
Member
 
Join Date: Apr 2012
Posts: 52
Default Instances in public array turns null

I have a class that has a lot of functions, and a couple of functions need to use the same array of instances. In order to shorten code, I should create a global array with these instances and all functions in this class can access it, however when I create it, every instance inside turns "null" and no function can access it.

ActionScript Code:
package  {         //imports        public var portraitsArray:Array = [                                                    [instance1, instance2, instance3],                                                       [instance4, instance5, instance6]                                                    ];                public function Story() {}        ...        ...        public function start() {              ...              ...              for each (var portrait:MovieClip in portraitsArray[0])           {               portrait.alpha = 0;           }                 }        public function next() {              ...              ...              for each (var portrait:MovieClip in portraitsArray[1])           {               portrait.alpha = 1;           }         } }

Running this code above will return every instance inside portrairsArray as null, thus functions will return null errors.

This is just example, performing function on instances inside the array.

However, this code below works, although being very redundant:

ActionScript Code:
package  {         //imports            public function Story() {}        ...        ...        public function start() {              ...              ...              var portraitsArray:Array = [                                                    [instance1, instance2, instance3],                                                       [instance4, instance5, instance6]                                                    ];              for each (var portrait:MovieClip in portraitsArray[0])           {               portrait.alpha = 0;           }        }        public function next() {              ...              ...              public var portraitsArray:Array = [                                                    [instance1, instance2, instance3],                                                       [instance4, instance5, instance6]                                                    ];              for each (var portrait:MovieClip in portraitsArray[1])           {               portrait.alpha = 1;           }         } }

Does anyone know why I can't access those instances as global array?

Last edited by LanLing; 04-14-2012 at 06:00 AM.
LanLing is offline   Reply With Quote
Old 04-14-2012, 08:27 PM   #2
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

It is generally a bad idea to define a value of a property when you declare it. You'd want to define it inside a constructor or consequent methods from the constructor like this.
ActionScript Code:
package  {         //imports        public var portraitsArray:Array;                public function Story() {              ...              portraitsArray  = [                         [instance1, instance2, instance3],                            [instance4, instance5, instance6]              ];        }        ...        ...        public function start() {              ...              ...              for each (var portrait:MovieClip in portraitsArray[0])           {               portrait.alpha = 0;           }         }        public function next() {              ...              ...              for each (var portrait:MovieClip in portraitsArray[1])           {               portrait.alpha = 1;           }         } }
I have a feeling that you will still get null with that because I see nowhere you are setting those instance1, instance2 and so on. So just make sure you are setting the value of portraitsArray array after you define those instances.
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 04-14-2012, 08:55 PM   #3
LanLing
Member
 
Join Date: Apr 2012
Posts: 52
Default

Those instances already exist on stage. They're all movieclips placed on the same frame in the Story class. I didn't have to define those instances when I put the array into each function though. It's just when I move the array globally, they turn null for some reason.
LanLing is offline   Reply With Quote
Old 04-14-2012, 11:16 PM   #4
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Try this and see if this works for you.
[as]
ActionScript Code:
package  {         //imports     public var instance1:MovieClip;     public var instance2:MovieClip;     public var instance3:MovieClip;     public var instance4:MovieClip;     public var instance5:MovieClip;     public var instance6:MovieClip;            public var portraitsArray:Array;                public function Story() {              ...              portraitsArray  = [                         [instance1, instance2, instance3],                            [instance4, instance5, instance6]              ];        }        ...        ...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 04-15-2012, 12:41 AM   #5
LanLing
Member
 
Join Date: Apr 2012
Posts: 52
Default

Nope still says the instances are null... Damn it. "TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Story/nextTextBlock()"
I don't get this at all, if I define the array in the function each time I use it, it works, if I define it globally, it doesn't.
All the instances are on the same frame!
LanLing is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:24 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.