Skip to main content
Known Participant
April 20, 2006
Answered

Access a component from within own class

  • April 20, 2006
  • 14 replies
  • 1097 views
Hello guys,

I have the problem to access all methods of a DataGrid instance within my own class.

System: Flash 8, AS2

I have two files:

mp3Player.as
mp3Player.fla

The scene is simple:

Library consists of the DataGrid component and one movie clip. In the movie clip is an instance of the DataGrid component with instance name playList. An instance of the movie clip is placed in the scene and the movie clip has been linked to the mp3Player class in the as file.

The mp3Player class is very simple. It contains a DataGrid variable playList and a constructor to initialize the DataGrid playList in the scene.

What the constructor can do is e.g. playList._visible=false or playList.setStyle(...). But as soon as i start with playList.addColumn or playList.addItem it does not work..

What am I doing wrong?

schnizZzla
This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Peter Lorent
It's not a bug, it's the 'nested assets problem'. The method isn't available yet, so the call has no meaning. Solution: you have to wait for the components properties and methods to load. And that's what the attached class does. Explanation: we use a setInterval to poll whether one of the methods of the component is available at which point you can safely assume that all properties and methods are available. You pass a reference (this) to the current object as a parameter to the setInterval to be able to call other methods in the class.

14 replies

Known Participant
April 26, 2006
I thought about this "nested assets problem", I mean that macromedia developers are aware of this and maybe there is some method for components which can tell you when an component is initialized completely and all methods are accessible. I only found UIObject.doLater and UIObject.load.
doLater: "Although any approach that delays access of the property will resolve this problem, the simplest and most direct solution is to use the doLater() method."
see: Components Language Reference > UIObject class > UIObject.doLater()
So it seems exactly their solution for this problem. I also thought that UIObject.load could help, but unfortunately when the mp3Player constructor is called both methods (doLater, load) are undefined so you would have to use setInterval to wait for them... and like this it makes no sense to call doLater, I can do everything within the interval.
It seems doLater works only if the script is attached to the component directly... am I right?
Inspiring
April 26, 2006
>>It seems doLater works only if the script is attached to the component directly... am I right?
Yes, with a minor detail: the component instance
Known Participant
April 27, 2006
yes of course i mean the instance. you have helped me quite a lot. i'll give it back to the forums when i'm not such an AS noob anymore ;-)
Known Participant
April 24, 2006
One strange thing happens with the setInterval trick. When the instance of the mp3Player mc is called "mp3Player" it works, but if it has another name TWO columns are added, it seems the addColumn method is called twice... How is this possible?

It's even more confusing. If I put two instances of the mp3Player mc in the scene, the addColumn method will be called twice for each of them, but only as long as I don't name ONE of the instances "mp3Player". Then the method is called once.

This is some kind of strange magic to me ;-) I mean it's not the aim of classes, normally I don't know how the instance will be named...
Inspiring
April 24, 2006
Please clarify: (what instance)
>>When the instance of the mp3Player mc is called "mp3Player" it works
Show the code you are using.
Known Participant
April 24, 2006
yeah, it's a bit confusing, sorry. the code is the same as you posted.
I only mean, I have this movie clip in the library of my FLA callod "mp3Player" (it is linked to the mp3Player class).
When i drag an instance of it into the scene and name the instance of it "mp3Player" it works (one column "title" is added). when I name it "whatever" => two columns (title) are added. when i drag two instances of the mp3Player mc from the library to the scene and name only one of them "mp3Player" it works. Is none of them named like this again two columns are added...
Did you understand what I mean, now?

Inspiring
April 23, 2006
You're welcome.
Known Participant
April 23, 2006
Kujanos, LuigiL THANK YOU SO MUCH!!!!!!!!!
I'm trying to understand this problem since 5 days!!

First I learned something about components and everything worked without classes.
But knowing the benefits of classes, I wanted to make my own mp3Player class and then i stuck into this problem.
And I just didn't want to do it without classes!

THANK YOU AGAIN!!!

schnizZzla
---
You can't prove it won't happen.
Participant
April 20, 2006
Try to wait one frame before setting any data to your playList. Usually components have this bug.
Peter LorentCorrect answer
Inspiring
April 20, 2006
It's not a bug, it's the 'nested assets problem'. The method isn't available yet, so the call has no meaning. Solution: you have to wait for the components properties and methods to load. And that's what the attached class does. Explanation: we use a setInterval to poll whether one of the methods of the component is available at which point you can safely assume that all properties and methods are available. You pass a reference (this) to the current object as a parameter to the setInterval to be able to call other methods in the class.