flash function help
- Started
- Last post
- 12 Responses
- NOVOLUME
I have created an onPress function on the main timeline which I am trying to call from inside a movieclip. The button that I am trying to call it from does not exist on the first frame of the movieclip and as a result the function doesn't seem to work. Any ideas how I can get around this and still keep all my function code on the root of my movie? Thanks
- blaw0
you could place your MC on frame one, then after declaring your function set the _visible property to false. in the second from set the _visible to true.
- alloyd0
_global.myFunctionName = function(){ //add code here; };
call it using _global.myFunctionName();
What the other guy said too. put it on the first frame, set _visible to true/false;
- NOVOLUME0
Thanks guys. Is that generally good practice? I've only just started using functions in an attempt to keep my code much cleaner.
- speedlab0
Can your provide the code or fla ?
- NOVOLUME0
contact_info.ichat.mybutton.onPr... = function() {
*/function details/*
};Basically i'm using the same button across my movie, so rather than duplicate it several times I wanted to reuse the same one and someone advised me to do it that way
- speedlab0
Should work fine as long you've named the button mybutton and it's located in a movie clip called ichat, and that clip is placed in a movieclip called contact_info. Make sure the layer your scripts are on extend to the frame where the button is located
- NOVOLUME0
Ok cool, I was just making sure that the _visible thing is generally a good thing to do
- mrbee28280
I wouldn’t say it was “best practice”. You do want to consolidate your code and not have scripting all over the place. Your scripting decisions should be weighed against the type of project you’re developing. If it’s a simple animation/banner type thing with a launch button, something scripted in all within Flash at a few various points in AS1 is just fine. Don’t let anyone tell you different.
If you are developing a large site or application with lots of interaction points you want to go with a nice OOP model and leverage AS2 and external class files. This keeps a few initialization lines of code up front in the fla and the majority of the code within .as files.
I usually don’t find a middle ground anymore. If I do anything that will require even the smallest amount of debugging or possible client changes I go AS2 and class files. It keeps me sharp and it’s easier to isolate problems. From what you have described I would simply add a function on the main timeline in frame 1 that does some action. Within the movieclip that acts as the button I would place an event to call the previous mentioned function. You could then keep all your code in the first frame of each “important” timline.
- caseyc0
Try to use onRelease instead of onPress. It's more natural to the user to have some kind of control over when the button executes, even if conciously they dont realize it.
Now, for the code part... I'm assuming you that you want your button clip to have some code but you dont want to put the code in the actual clip to try and keep things organized (on the root timeline)
If you are just starting with functions, I'm not sure you want to dive right in to AS2 so you'll want to look into "classes" in AS1, you can define a bunch of them on the first frame and then use Object.registerClass() and movie clip linkage ids to associate them with specific clips (like your button)
Here's an example:
http://pastebin.coconut.se/?id=4…The macromedia flash help also has some decent tutorials on this type of thing.
Cheers,
- alloyd0
You 'could' also just add the keyframe for that button in the first frame, scale it really small, and just move it off stage somewhere...
This way you don't have to mess around with _visible, still have access to the button clip, but won't be able to touch it since it will be off stage.
That would be the easiest way.
- System-Idle0
I would do it a similar way to mrbee2828 but instead of calling the function on the main timeline I would assign the function to the button using the =
If yr button functions are the same or yr reusing the same btn this is ideal
write yr function on the main timeline 'myfunction' etc
then in the button (when it appears on stage) first frame
this.onRelease = _parent.myfunctionor whatever the desired target for the function is
- cram0
what system-idle said.
main timeline:
myfunc = function(){
//do stuff;
}in button timeline:
btn.onPress = function(){
_parent.myfunc();
}that way all you are doing is triggering the function inside the button. you still keep the code that would likely need editing/debugging all in the same place.
good luck