Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 11-28-2009, 01:35 PM   #1
OMG
flash n00b...forever
cannot access public variable

I think I've made some fundamental error here...
I have a public variable a in class A, and I want to access it in class B, but I cant (access of possibly undefined property...)
what's wrong here?
now every time I want to refer to a variable in class A I need to pass the variable reference to class B's constructor
OMG is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 11-28-2009, 02:37 PM   #2
BeerOclock
Registered User
Quote:
Originally Posted by OMG View Post
I think I've made some fundamental error here...
I have a public variable a in class A, and I want to access it in class B, but I cant (access of possibly undefined property...)
what's wrong here?
now every time I want to refer to a variable in class A I need to pass the variable reference to class B's constructor
Whenever possible, post code along with your question.
A public variable in class A should be accessable from class B so something is wrong with your code. What? We'll never know till you post it
BeerOclock is offline   Reply With Quote
Old 11-28-2009, 04:11 PM   #3
OMG
flash n00b...forever
sry here's the simplified code:
ActionScript Code:
package {
 public class Menu extends MovieClip {
  //...
  public var itemNum:int;
  //...
  public function Menu() {
   itemNum=10;
  }
 }
}

second class:
ActionScript Code:
package {
 public class MenuItem extends MovieClip {
  public function MenuItem() {
   trace(Menu.itemNum);//error
  }
 }
}
OMG is offline   Reply With Quote
Old 11-28-2009, 04:32 PM   #4
IqAndreas
Registered User
Location Värmland, Sweden (and Illinois, USA)

Posts 1,330
You can solve this in two ways.

A, create a new instance of Menu:
Code:
var menu1:Menu = new Menu();
trace(menu1.itemNum);
B, make the property "itemNum" static, meaning only one instance of it is ever created, and all accessed properties will point to this one value:
Code:
package {
 public class Menu extends MovieClip {
  //...
  public static var itemNum:int = 10;
  //...
  public function Menu() {
   //Do whatever here
  }
 }
}

//Now, access that property from anywhere like before
trace(Menu.itemNum);

__________________
Give someone code, and they will have code for a day.
Teach someone to code, and they will have code for life.

Support a starving developer. Click ads in my blog...
http://iqandreas.blogspot.com/
IqAndreas is offline   Reply With Quote
Old 11-28-2009, 04:38 PM   #5
OMG
flash n00b...forever
oh god that's right...I totally forgot itemNum isn't a static variable >_<
sorry lol...guess my brain was stuck
OMG is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 -4. The time now is 06:40 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com