Click to See Complete Forum and Search --> : Read variable from mc?
jimventola
08-08-2006, 01:49 PM
I know that variables in moviclips are local. I am using a multipage contentpane to show some texts. I think that when the user clicks on the arrow to advance to the next page a variable called "pagenumber" is incremented. I want to use this information to link to a given audio file. That is, when the user chooses a page, I will use the value in "pagenumber" to determine which audio file is played when he hits the play audio button.
To test this, I created a dynamic textbox and added the AS "txt1var = pagenumber" but when I run it, the display is "Undefined." Makes sense, since "pagenumber" is in MC1 and so is local. Following info on a thread here, I tried adding "_global." on both sides of the assignment with no luck. Naturally, "mc1.pagenumber" is also an absurdity to KM. So how can I read the value in a variable within a movieclip?
BTW, entering all the pages by hand is tedious, especially since the interface does not remember that I am adding files and not using transitions and you can't select multiple files in the file selector. You have to do a lot of clicking. It would be nice if the interface remembered your last choices.
I know I could do this with actionscript as in the KC8Demo. I tried to follow that and the API in the Flash 8 Examples folder and the Users Guide, but the code eludes me. Is there more newbie-oriented information somewhere about using the components with AS? These components are terrific, so I want to learn how to get the most from them.
blanius
08-08-2006, 02:16 PM
if the contentpane is in mc1 then you can query the current page with mc1.contentpane1.getPage()
Not sure if there is an event you can watch for. Maybe onTransit
blanius
08-08-2006, 02:17 PM
"mc1.pagenumber" is also an absurdity to KM. So how can I read the value in a variable within a movieclip?
Actually this is fine, I imagine though that pagenumber is just not set.
jimventola
08-08-2006, 03:04 PM
Well, maybe my assumption is wrong. I was basing it on this from the contentpane AS
/* ** KC8 ContentPane ***
properties :
c [track, bar, button, arrow, background, textpaper, pagenumber] : colors
s [raise, angle, component shadow alpha] : styling options
_x, _y : location
autoHide : 0=none, 1=horizontal scroller, 2=vertical scroller, 3 = both scrollers
*/
I did notice that a search for "pagenumber" in that script came up empty. But now I think this is just a parameter, maybe a boolian to turn pagenumber on and off? As you see, I am flying blind here.
Still, the page number does appear (you can even set the color in the options) so it must be available.
blanius
08-08-2006, 03:13 PM
contentpane has function getPage() for just that usage....
Look in API document.
blanius
08-08-2006, 03:21 PM
here is this what you want to do?
jimventola
08-08-2006, 03:35 PM
That's it. Thanks so much.
So then, after
_root.txt1.text=contentpane1.getPage()
I could do something like
foo = _root.txt1.text
and then use foo in a string to manipulate which file is used?
Chris gave me this code
root.mySound = new Sound(mySprite);
_root.mySound.loadSound("song"+foo+".mp3", true);
So would foo now correctly add the pagenumber to the mp3 filename?
(I am really not too lazy to try, but I wanted to get the code into this thread in case any future forum searchers want to use this approach.)
Again, thanks so much for the help.
w.brants
08-08-2006, 04:24 PM
If you don't have to display it, you can even combine itmySound.loadSound("song"+contentpane1.getPage()+".mp3", true);The pagenumber parameter in the c array you are referring to is just the text color of the page number.
Edit:
If you wish you can combine it with the onTransit event to more or less sync it.
Chris_Seahorn
08-08-2006, 06:42 PM
mySound.loadSound("song"+contentpane1.getPage()+".mp3", true);
That's the ticket right there :thumbsup:
blanius
08-08-2006, 08:18 PM
If you don't have to display it, you can even combine itmySound.loadSound("song"+contentpane1.getPage()+".mp3", true);The pagenumber parameter in the c array you are referring to is just the text color of the page number.
Edit:
If you wish you can combine it with the onTransit event to more or less sync it.
I thought to suggest that but from reading the API info I wasn't entirely sure that was the way to go. But I think that's exactly what he was looking to do
Chris_Seahorn
08-08-2006, 08:24 PM
I'm hoping the way we construct (concatenation) dynamic variables becomes clear. Whether a "foo" we create or a "v" we scarf from what exists (as part of the component variables) he's understanding how it all works bit by bit. Right Jim?
jimventola
08-08-2006, 09:53 PM
If you don't have to display it, you can even combine it...The pagenumber parameter in the c array you are referring to is just the text color of the page number.
Elegant! Since the contentpane already displays the page number, I won't have to display it.
I had a hunch I was assuming too much from the semantics of "pagenumber" but that it meant color I would never have figured.
Thanks for the component and the help.
jimventola
08-08-2006, 10:03 PM
I'm hoping the way we construct (concatenation) dynamic variables becomes clear. Whether a "foo" we create or a "v" we scarf from what exists (as part of the component variables) he's understanding how it all works bit by bit. Right Jim?
Yes indeed, Chris. Bit by itsy bitsy bit. But Oh what fun!
Chris_Seahorn
08-08-2006, 10:27 PM
The more you realize you can skin the cat multiple ways...the more ideas pop in your head. All Wilberts components have those little dippers of data you can dip into. Without doubt one the five best reasons to buy this program are those puppies.
oz_emb
09-03-2006, 10:46 PM
Actually this is fine, I imagine though that pagenumber is just not set.
Friends,
I have been working on this same problem.
First of all you should know this thread came up with a Google search for "koolmoves movie clip event can't read movie clip variables"
I have a variable (msg = "Hello World!") declared in an actionscript statement located on a frame within my movieclip's timeline.
However, I want to call on that variable from an onClipEvent handler which is assigned to the movieclip itself.
If I assign a similar handler to a button on the other hand, there's no problem:
on(Press)
{
_root.txt1var = mc1.msg
}
...renders the familiar Hello World! statement in my root timeline text box.
However, the same statement when assigned to the movieclip does *not* produce results, and this bugged the heck out of me because I really do need actionscript in both parts of the MC (as a clip event and in the frames).
I finally figured out what was happening when I got curious and assigned this clip event to the movieclip:
on(Press)
{
_root.txt1var = this; // what's my name for crying out loud?
}
...and found out that mc1 has been thinking of itself as _level0.mc1
After that I had no problem accessing variable msg as _level0.mc1.msg.
A very minor discovery, to be sure, but one that bugged me for a good hour.
And I wonder if this explains why jimventola had so much trouble accessing a variable within a movieclip, even though (as blanius noted) it should have been entirely possible.
Anyone care? :-D
blanius
09-03-2006, 11:08 PM
I beleive _level0 is the same thing as _root and indeed if you need to access a variable in mc1 from whithin another clip you'd have to refer to it _root.mc1 or _parent.mc1
pherbrick
09-05-2006, 03:27 PM
_root is a direct reference to the main timeline of the current level. This means that witihin an mc within _level2, _root refers to the main timeline of _level2.
blanius
09-05-2006, 04:54 PM
No don't think so... _root is just that _root it is the swf that is into which all else is loaded. This is why it's better to use _parent if you don't know at what level you movie will reside. so mc1's full name is _level0.mc1 or _root.mc1
try this.Create a textfiled on the stage, Put a clip on the stage . Add this code to the clip mc1's time line
_root.txt1.text="hi from "+this
and you will get hi from _level0.mc1 in the text box.
Now change _root to _level0 and you will see that it still works.
So _root and _level0 are the same. Unless your clip uses lockroot command.
blanius
09-05-2006, 04:57 PM
If I read him right all oz_emb had to do was use
on(Press)
{
_root.txt1var = msg
}
or
on(Press)
{
_root.txt1var = this.msg
}
the way it is now
on(Press)
{
_root.txt1var = mc1.msg
}
would send the variable msg from a clip called mc1 within mc1.
pherbrick
09-05-2006, 06:02 PM
No don't think so... {explanation deleted}
try this.Create a textfiled on the stage, Put a clip on the stage . Add this code to the clip mc1's time line
_root.txt1.text="hi from "+this
and you will get hi from _level0.mc1 in the text box.
Now change _root to _level0 and you will see that it still works.
So _root and _level0 are the same. Unless your clip uses lockroot command.
I'm a little pressed for time, so I can't give my usual really detailed and heavily researched essay answer, but your example above is consistent w/ my answer, rephrased as follows: _root within _leveln = _leveln. Your example is limited to _level0, so of course it works. _level0 = the original document loaded into the flash player.
In a movie w/ only one level, by default _root = _level0. All swfs start at _level0 when they are created, but swfs can be loaded into other swfs at levels other than _level0, ie, multiple level projects are created by loading swfs into levels other then _level0. It is a means of creating modular programming with independantly developed modules. It is also why _root is so important - if you create a swf (x.swf) that is going to be loaded into another swf's levels (y.swf), then 1) coding _level0 in x.swf will refer to the main timeline of y.swf when x.swf is loaded into _level(n>0), but 2) coding _root in x.swf will always refer to the main timeline of x.swf, regardless of what level x.swf is loaded into in y.swf.
If x.swf is loaded into _level0 of y.swf, it in effect replaces y.swf in the flash player. This is usually a bad idea, unless you are daisy-chaining swf execution. This was actually covered a while back as a way to circumvent flash player swf size limitations.
Levels are used to organize the "document stack" - movies (swfs) that are running together but not nested - sort of running in parallel. You would use the _leveln.mcn.varname path syntax to communicate between these movies running simultaneously within the flash player (passing values or controlling clips). By convention, _level0 usually contains the code that coordinates the actions of the swfs in the other levels, and the frame rate defined in the movie loaded into _level0 is applied to all the movies running in the other levels - I can't recall if the frame rate changes if a new swf is loaded into _level0; I don't think it does.
Gotta go, hope this didn't confuse you any worse. I think Remus is the most experienced w/ using levels.
Peter
blanius
09-05-2006, 08:56 PM
Hmm Peter I guess we agree but the way you word it "_root is a direct reference to the main timeline of the current level." just didn't sound like right to me. But again I guess we are saying the same thing. In essense _root and _level0 are always the same thing the MAIN timeline, and yes you CAN load a swf into that level I've never seen much point in that.
The mistake that was being described here was a movie clip referencing itself by it's name doesn't work and you must use this or it's full reference in this case _root.mc1
All good. I too hope the conversation didn't confuse but in fact helped.
flashkit.com
Copyright WebMediaBrands Inc., All Rights Reserved.