|
-
Having trouble with multiple instances of the same object
I'm relatively new to Flash Actionscript but I've been programming in OOP for years so it only took a few days to get up to speed, however I've ran into a few weird quirks that make me wonder if I'm doing something wrong.
I've got a single scene, a main timeline within that scene and within certain frames of the main scene I've got mc's that play based on Actionscript commands. All of that works fine until I get into one of the mcs. Within them I have buttons defined on certain frames and I've assigned instance names to those buttons.
The problem is when I try to reuse an instance name on two different key frames. Within the parent scene it works fine, within the child mc only the first instance works. If I target a different frame that has the same button and instance name in the ActionScript, it does not work unless I rename the instance name in the second key frame. It sounds confusing so here is an example:
//Example_mc.Frame1 (keyframe1)
//Goes to frame 5 in Example_mc
this.button1_btn.onPress = function() {
gotoAndPlay(5)
}
//Example_mc.Frame10 (keyframe2)
//Should go to frame 5 in Example_mc but does not
this.button1_btn.onPress = function() {
gotoAndPlay(5)
}
//Example_mc.Frame10 (keyframe2)
//Properly goes to Frame5 after renaming button1 instance to button1a
this.button1a_btn.onPress = function() {
gotoAndPlay(5)
}
In the example above, when button1_btn is pressed frame 1 is properly displayed. When I try to reuse the button and button instance in frame 10, the button does not work until I rename the instance name to a unique name.
However, in the main scene, all of my navigation is done through re-usable button instances which is why I do not understand why it is not working in the child mcs.
My main question is, shouldn't I be able to re-use instance names in a child mc? If so, then what am I doing wrong?
-
Amazed and Amused
I think you're probably dealing with issues of scope. Within a movie clip's timeline, functions defined on frame 1 still remain valid throughout that timeline. So, for example, you really shouldn't have two onRelease functions defined for the same button in the same timeline. Better to have one onRelease function, and then make it do different things depending on where you're at in the timeline. For example:
(and put this on frame 1 of the timeline)
Code:
this.button1_btn.onRelease = function() {
if(currentFrame == 1) {
gotoAndPlay(5);
} else if (currentFrame == 10) {
gotoAndPlay(5);
} //.... etc.....
}
BTW, using onRelease rather than onPress gives the user an opportunity to change their mind. If they've clicked the button, but then roll off before releasing, it doesn't activate if you use onRelease.
Hope you're having fun with actionscript. I'm a so-so programmer, so I think I should be taking lessons from you on the OOP stuff. Anyway, I read these forums, and attempt to help out where I can. I find as I do, it helps me in the process.
-
Thanks for the reply, I'll try that tomorrow at work. What you said makes perfect sense, after reading a few tutorials I use an action layer to store all of the code for every mc (keeps it simple), but I was setting keyframes in the action layer to match the keyframes in the content and main layers. It now makes perfect sense to not set any key frames at all in the action layer which will allow me to have only a single instance of any given block of code. Previously I was c/p'ing the code from the previous frames into the new keyframe which now makes no sense at all.
I guess I'll put my stop(); statements in the key frames of the main layer and reserve the rest of my code for the action layer.
Tomorrow I'll remove all of my keyframes from my action layer and try that, also I'll change my event handler to onRelease, thanks for the tip. I don't have to use if statements though, I've designed the site so that any given instance of a button will perform a single function regardless of what frame it is on. That function will follow that instance name throughout the site. Keeps the code simple.
Actionscript has been interesting so far, it seems to use js syntax and custom objects and properties of those objects similar to VBA, so overall its been fun so far. Of course I'm just getting started so figuring out how to tie the code to the rest of the app has been the challenge.
-
No go with removing the keyframes, same problem. Seems I'm not the only one with this problem though, found the following blog when I searched:
http://www.flalog.com/?p=9
I'm still not sure how to fix it. All of my mcs already have instance names, so assigning an instance name to the mc was not the problem.
-
I finally found the answer, in case anyone else has this problem below is the solution:
Flash 8 only carries instance names until the movie playhead encounters the next keyframe. On the next keyframe, it is not possible to re-use an instance name, I guess this is a limitation of Flash8. The workaround is to create a separate layer, place any objects you wish to carry on this layer, and do not add keyframes to the layer. Not a pretty workaround but better than recoding instance names for every keyframe on the content layer.
-
The G5 SP
If the instance is on say Layer 8, and you add a keyframe, that instance will be in that keyframe with the instance name intact.
-
 Originally Posted by N_R_D
If the instance is on say Layer 8, and you add a keyframe, that instance will be in that keyframe with the instance name intact.
Only if that instance was present in the previous keyframe. I've finally narrowed down my problem and have a workable solution. Earlier I failed to mention that in parts of my timeline I remove the button instances. If the mc lands on a keyframe that does not have the instance, then travels farther down the timeline and lands on a keyframe that does have the instance, the Actionscript at that subsequent keyframe does not work unless the new instance has a unique name.
So for example
Frame1
bio_btn
Frame20 //no bio_btn
Frame50
bio_btn
If the mc landed on Frame20 where the bio_btn does not exist and there was a keyframe between Frame1 and Frame20, then the ActionScript in Frame50 referencing bio_btn would not work if there was a keyframe between Frame20 and Frame50 until bio_btn in Frame50 was renamed to bio1_btn (something unique).
I am pretty certain by now. I researched everything I could find including Adobe's LiveDocs and it appears Flash has always had problems with keyframes and persistent instance names. My workaround is to make a new instance name incremented by 1 after any keyframes within which the instance does not exist. Everything in my project is working fine now.
-
The G5 SP
Do you have the instance in the frame, and clear the frames, or do you copy and paste the instance on the new Key frame?
-
I've tried both ways. I've tried extending an existing keyframe, then adding a new keyframe in the middle and deleting the instance...no go. I also tried copying the original instance from a keyframe prior to the keyframe that is lacking the instance and pasting it after the keyframe where the instance has been removed.
What is strange is that, in my prior example, if the mc were to jump from Frame1 to Frame50 both instances of the button would work until the mc landed on Frame20. Any instances of bio_btn after Frame20 on the timeline would then cease to work.
Another workaround that I think will work would be to set bio_btn to invisible in Frame20 vs removing it, that may be simpler than trying to track which instances of an object have been deleted then added and would let me stick with a single instance name.
this.bio_btn._visible = false;
Last edited by diesel5599; 06-07-2006 at 06:51 PM.
-
The G5 SP
Hmm that is weird...
What I ususally do it make a function for placment, then one for removal
function ditchThem(){
btn1._x = 1000;
}
function returnThem(){
btn1._x = 293.3;
}
Kind of like that.....
-
That would work too since it moves btn1 off the stage vs deleting it. It looks like anything will work as long as you do not delete the instance. I prefer changing the visible property since then I don't have to go into a previous frame and find the original value of _x for each removed object. When working with many objects I think it would get more complicated than just hiding/unhiding existing objects wherever they are.
-
The G5 SP
I have had issues with visible, and with enable ... thats why I yank them off the stage.
-
Really? I've only been programming in Flash for about 4 days and already running into all these 'issues'. I thought Flash was more mature than that. I guess I'm used to languages and development platforms that have been around forever.
Thanks for the tip, looks like I'll be yanking them off the stage too. I don't want to spend another day trying to work around more 'issues'. I'm ready to get to coding.
I know what you mean about enable, I've already ran into issues with that which is why I thought deleting the objects would be easier.
-
The G5 SP
Ah but as we have foud..... deleting htme is not a good thing either 
Just repostion them ..... and its -1000 that I use....
I like full window flash, so the _x = 1000 would not work really good, or you would get floating buttons
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|