Groups | Blog | Home
all groups > flash actionscript > july 2006 >

flash actionscript : Possible to pass a 'case' value?


swim4it
7/14/2006 9:36:06 PM
Is it possible to pass a 'case' value from one layer to another?

One of my buttons on level 300 is trying to pass a 'case' value to level 100.
Here is the code on the button:

pizza.vbut.onRelease = function() {
_level100.pizza_slice=2;
tellTarget("_level100");
}

Is this correct?

Then, level100 is trying to retrieve it by this code:

switch (pizza_slice) {
case 1 :
title.pageTitle.htmlText = "Logos";
myMCL.loadClip("images/logo1.jpg", this.thumb1);
myMCL.loadClip("images/logo2.jpg", this.thumb2);
myMCL.loadClip("images/logo3.jpg", this.thumb3);
myMCL.loadClip("images/logo4.jpg", this.thumb4);
break;
case 2 :
title.pageTitle.htmlText = "Advertisements";
myMCL.loadClip("images/ad1.jpg", this.thumb1);
myMCL.loadClip("images/ad22.jpg", this.thumb2);
myMCL.loadClip("images/ad3.jpg", this.thumb3);
myMCL.loadClip("images/ad4.jpg", this.thumb4);

break;
}


Am I missing something in this code? I know it's not passing the variable
because when I add the line:
"pizza_slice=2;"
above the switch statement it works fine.

blemmo
7/15/2006 11:34:14 AM
This is possible, but the switch code has to be executed after the assignment.
It looks like it's on a frame, so it will only execute when the player enters
this frame; if you change the pizza_slice var afterwards, it will have no
effect as long as the player frame doesn't enter that frame again.
You could put the switch code into a function, and execute this when the
button is pressed:
--
function switchAndLoad(){
switch (pizza_slice) {
case 1 :
title.pageTitle.htmlText = "Logos";
myMCL.loadClip("images/logo1.jpg", this.thumb1);
myMCL.loadClip("images/logo2.jpg", this.thumb2);
myMCL.loadClip("images/logo3.jpg", this.thumb3);
myMCL.loadClip("images/logo4.jpg", this.thumb4);
break;
case 2 :
title.pageTitle.htmlText = "Advertisements";
myMCL.loadClip("images/ad1.jpg", this.thumb1);
myMCL.loadClip("images/ad22.jpg", this.thumb2);
myMCL.loadClip("images/ad3.jpg", this.thumb3);
myMCL.loadClip("images/ad4.jpg", this.thumb4);

break;
}
}
--
Then your button code can call this function:
--
pizza.vbut.onRelease = function() {
_level100.pizza_slice=2;
_level100.switchAndLoad();
}
--
There's no need to use tellTarget, it's outdated, use the dot syntax instead
like you did in the line before.

hth,
blemmo
blemmo
7/16/2006 12:00:00 AM
You have to call the function from level 300. The code in level 100 executes
only when the player arrives at the frame, so when it stops there, you can
change pizza_slice as you want, it won't execute again as long as you don't
make it so. That's why the function is added, you can call that anytime. Put
_level100.switchAndLoad();
in your button code to make it execute when the button is clicked.
To see if it's executing, you can put this line at the beginning of the
function:
trace("switchAndLoad called");
This will show that line in the output window whenever the code executes.

cheers,
blemmo
swim4it
7/16/2006 12:10:49 AM
Ok - so I replaced this:

_level100.pizza_slice=2;
tellTarget("_level100");

with this:

_level100.pizza_slice=2;

then on level 100 I put the switch function switchAndLoad() - or rather just
made the code I had into that function - on frame 6 (there is no stop action on
any frame prior to that) . Then, because that function doesn't even exist until
frame 6 on level 100 call the switchAndLoad() function after it is defined
within that same frame (6).

It's still not working. Antyhing else I'm missing? I can slap up the fla
files if that would help.



swim4it
7/16/2006 4:22:31 PM
Do I have to call the function in level 300 even though the function actually
resides in 100? I didn't think I could change dynamic text until the dynamic
text box is actually on the frame the function it is called (which is on frame
6 in level 100).
blemmo
7/16/2006 4:38:57 PM
Yes, you have to call it to make it execute. The location doesn't matter as
long as you specify the path to it. That goes for all objects and functions, so
you could have your textfield in another frame or level, as long as it is
present when the function executes and the path to it is right.
I assume the movie stops in frame 6 in your setup. When the function is
defined there (it could also have been defined in frame 1) and the textbox
exists on stage in that frame, you can call the function from anywhere in your
movie with the _level100 prefix.
blemmo
7/17/2006 12:00:00 AM
Right, this should work. But there's a possible trap here, because the code in
level 300 executes after the code in lower levels, I think. So the function in
level 300 should be defined before frame 6 when you want to call it in that
frame from level 100. The other way round it would be ok, the function gets
defined in the code of frame 6 of level 100 and can be called in the same frame
from level 300.
So to be on the safe side, define the function in an early frame, maybe frame
1, then you can call it later without problems.

greets,
blemmo
swim4it
7/17/2006 2:46:30 AM
swim4it
7/17/2006 6:29:26 PM
OK. I've uploaded the root level, level 100, and level 300 that I'm working
with:

http://www.midnight007.com/pizza/layer_root.fla
http://www.midnight007.com/pizza/layer_300.fla
http://www.midnight007.com/pizza/layer_100slices.fla

With what I am doing now, the root layer loads first, then layer 300 - which
is the menu. Specifically I'm dealing with the buttons on the pizza (the
pepperoni slice right now). The code is in the pizza movieclip on the
pepperoni button. (Yes...I know there's an easier way to do the buttons than
putting the code here, but I haven't gotten the other movie to work yet - so
currently the code is there...)

Anyway, when the pepperoni slice button is clicked at the end of the onRelease
function I put "_level100.pizza_slice=2;" which should define the pizza_slice
variable for my switch statement. Then, when it's clicked another switch
statement of "page=4" is defined along with the "gotoAndPlay" frame 14 of the
root level. The playhead will play frames 14 - 28 on the timeline and stp at 28
- where the switch statement calls the variable of "page" and executes the
script to pull up level 100 with layer_100slices.swf.

Then, once level 100 is pulled up - within frame 6 is where the switch
statement for pizza_slice is defined and called after the definations. I can't
call the statement until frame 6 because the dynamic text fields aren't there
until frame 6.

So - I still don't understand why it isn't working.

It works if I definate pizza_slice within frame 6 - as I tested it. So it
looks like the variable of pizza_slice from level 300 is not being passed.


blemmo
7/17/2006 10:27:15 PM
Sorry, but I'm quite lost here. I had a look at the .flas you provided, but
couldn't find an MC with 28 frames as you described. If you use _root in level
300, it will resolve to the root clip of level 300, not the root clip in the
player. You can get to the 'real' root with the _level0 prefix.
But back to your pizza_slice var: the button in the level 300 clip is actually
2 buttons, one instance from frame 1-9, another one from frame 11-17. In frame
10 it's just a bitmap. Now these 2 instances have slightly different codes on
them, which confused me a lot until I got behind that. I think in the first
run, the code from frames 1-9 is used, after that only frames 11-17, is that
right? So take care that the scripts are identical.
Now if the script on the button includes this:

_level100.pizza_slice=2;
trace("_level100.pizza_slice = "+_level100.pizza_slice);
_level100.switchAndLoad();

and in level 100, the switchAndLoad function starts with this:

trace("switchAndLoad called: pizza_slice = "+pizza_slice);

the output is this:
switchAndLoad called: pizza_slice = undefined
_level100.pizza_slice = 2
switchAndLoad called: pizza_slice = 2
switchAndLoad called: pizza_slice = undefined

So the var passing works, but it seems that the level 100 movie gets replayed
or the switchAndLoad gets called again with an undefined pizza_slice. This is
just the case for the first click, the following clicks just output
_level100.pizza_slice = 2
switchAndLoad called: pizza_slice = 2
so it's all fine there. I couldn't figure out the difference between the first
and the following clicks, but I guess you know, so you should try to find and
avoid the 2nd 'undefined' call.

greets,
blemmo
swim4it
7/18/2006 4:20:40 PM
Ok the Movie Clip you asked about that had 28 frames is actually layer_300 -
not a movie clip but a movie. And yes, I realized the frames 1-9 button
instance has a different code than 11-17 - I was working primarily with 1-9
then I was planning on changing 11-17 once I got it worked out.

Alright, I changed the script on the button to include:
_level100.pizza_slice=2;
trace("_level100.pizza_slice = "+_level100.pizza_slice);
_level100.switchAndLoad();

Then I added this to frame 6 on level 100:
trace("switchAndLoad called: pizza_slice = "+pizza_slice);

I removed the switchAndLoad(); at the end of the actions in frame 6 so it's
not called once again. And it is coming up with _level100.pizza_slice = 2 in
the trace, however, it's not reading it properly because if it were, the title
would change and say 'Advertisements'.

For this, don't you have to wait until frame 6 over level 300 to load to
actually call the function with _level100.switchAndLoad(); - because if it's
called in level 300 and frame 6 hasn't loaded - the default of 'Logos' will
show up in the title.

I've re-uploaded the new fla files:

http://www.midnight007.com/pizza/layer_root.fla
http://www.midnight007.com/pizza/layer_300.fla
http://www.midnight007.com/pizza/layer_100slices.fla


blemmo
7/18/2006 5:35:47 PM
Ok... it's still confusing to me, but it's getting clearer. When the movie
starts, the level 100 MC isn't loaded. You can set the pizza_slice var, but
there's no switchAndLoad() to execute. The MC gets loaded when level 300 comes
to frame 28, but then the pizza_slice gets deleted.
I think you should change your setup. The switch function and the pizza_slice
var could be in level 0, so they will always be present. The function
definition can go in frame 1, so it's always available. Then change the path in
the level 300 code to _level0.pizza_slice and _level0.switchAndLoad().
You could also pass the value directly to the function, if this is the only
time pizza_slice is used, so you don't need it as an extra var. Then it would
be enough to say _level0.switchAndLoad(2), and in the function, use the
parameter:
--
function switchAndLoad(selection:Number){

trace("switchAndLoad called: selection = "+selection);

switch (selection) {
case 1 :
...
--

Now I'm hungry.

greets,
blemmo
swim4it
7/18/2006 7:36:23 PM
blemmo
7/18/2006 8:02:28 PM
That's a good point. What I don't understand: it seems as if the level 100
movie always gets unloaded when you click one of the slices, and then another
movie gets loaded into level 100. So there's no use setting the var and the
title text in level 100 with the click, because it will unload the movie right
after that. Is that right?
I guess it's better then to have the variable in level 0, and read it from
level 100 when the movie loads (again). That's basically how you started,
except that pizza_slice would be on level 0. Then you wouldn't need that
switchAndLoad function, but could go with that simple switch statement in frame
6 in level 100. Just set _level0.pizza_slice in the button code, and let the
movie unload and reload by the code in the frames of level 300. When the new
level 100 movie plays, it switches _level0.pizza_slice in frame 6 and sets the
title and pictures. That way the pizza_slice var would still be present after
the reload of level 100.
Hope this does it... I really want some pizza now. :)
swim4it
7/18/2006 10:00:22 PM
So, I'm not sure I understand correctly. When you say have the variable in
level 0 - do you mean have the switch statement in level 0 too? Or just set the
variable to level0 through the button action scripting of _level0.pizza_slice?
If all I need to do is to set the variable target to level0 - then is there any
action scripting changes needed on level0 - or will it store it without
changing anything?
blemmo
7/18/2006 10:46:47 PM
It's just the pizza_slice var in level 0, so it won't get deleted by the
loading movie in level 100. The switch statement may stay in its frame in level
100 and use the var from level 0 >> switch(_level0.pizza_slice).
So, you just need to change the target path for the var. There's no code
needed in level 0, as long as you don't need a default value (the pizza_slice
var will only exist after the first click on a slice).
unk
7/19/2006 2:36:59 AM
swim4it
7/19/2006 2:40:12 AM
AddThis Social Bookmark Button