
Button.onPress/on(press)?
Permenant Link To: Button.onPress/on(press)?
Today, I'd like to answer a reader's email. Maybe it's something you've wondered about for a while.
Here's the question:
I would like to ask if what is the difference between the two sample script below. Because when I try it, they work exactly the same. Which script is better to used and why? Why is Sample 1 is written in that way, is there an advantage of using it instead if the other?
I put this script on frame 1 of my movie.
Sample 1:
myButton.onPress = function(){
gotoAndPlay(_currentframe + 1);
}
I put this script on the myButton.
Sample 2:
on(press){
gotoAndPlay(_currentframe + 1);
}
Thank you.
Here is my response:
There is no real difference in the end result that is achieved. There is a benefit to using the first sample script, however. Here's how.
If you have 5 buttons on the stage, and 4 movie clip instances, placing scripts on each one individually can become a workflow nightmare. You probably know the kind, 'which button/movie clip instance is doing what, I've got to click on each one to find out.' The syntax as shown in your Sample 1 can help eliminate this problem because it allows you to script the functionality of all the current elements on the stage, from a single script that exists on a frame on the timeline. In other words, instead of having scripts in 9 locations (confusing), they would all exist in a single location, a frame on the timeline - easier to edit and debug.
On a side note, placing scripts on the timeline in this manner is recommended by Macromedia. It's a smarter way to code.
More information can be found about using the syntax shown in Sample 1 on pages 75-82 of my book, Macromedia Flash ActionScripting: Advanced Training from the Source, or pages 487-499 of my other book, Macromedia Flash MX Creative Web Animation and Interactivity.
Here's the question:
I would like to ask if what is the difference between the two sample script below. Because when I try it, they work exactly the same. Which script is better to used and why? Why is Sample 1 is written in that way, is there an advantage of using it instead if the other?
I put this script on frame 1 of my movie.
Sample 1:
myButton.onPress = function(){
gotoAndPlay(_currentframe + 1);
}
I put this script on the myButton.
Sample 2:
on(press){
gotoAndPlay(_currentframe + 1);
}
Thank you.
Here is my response:
There is no real difference in the end result that is achieved. There is a benefit to using the first sample script, however. Here's how.
If you have 5 buttons on the stage, and 4 movie clip instances, placing scripts on each one individually can become a workflow nightmare. You probably know the kind, 'which button/movie clip instance is doing what, I've got to click on each one to find out.' The syntax as shown in your Sample 1 can help eliminate this problem because it allows you to script the functionality of all the current elements on the stage, from a single script that exists on a frame on the timeline. In other words, instead of having scripts in 9 locations (confusing), they would all exist in a single location, a frame on the timeline - easier to edit and debug.
On a side note, placing scripts on the timeline in this manner is recommended by Macromedia. It's a smarter way to code.
More information can be found about using the syntax shown in Sample 1 on pages 75-82 of my book, Macromedia Flash ActionScripting: Advanced Training from the Source, or pages 487-499 of my other book, Macromedia Flash MX Creative Web Animation and Interactivity.
0 Comments:
Posting Guidelines - Please Follow The Rules!
I am happy to provide this commenting feature to enable discussion of the articles posted. In fact, I appreciate and encourage you to participate.
Like everything else I put my name on, I hold this site to a very high standard. What you say in your comments reflect indirectly on me. Therefore, please be aware of the following guidelines/rules for posting your thoughts.
After familiarizing yourself, please post away! :)
Keep it Clean.
Foul and offensive language is not only moronic and childish, but any posts containing it WILL be deleted!
Play Nice.
Posts containing personal attacks of any kind will be deleted. If you disagree with someone's opinion, provide an opposing one, without attacking the person. For example, "Derek, your stance on eating doughnuts will harm future generations." Not, "Derek, you're an idiot, and your stance on eating doughnuts is the dumbest thing I've ever heard..."
Religion and Politics .
Once again, since this site carries my name, posts containing religious and/or political views (nice or not) will be deleted.
Stay on Topic - Where Possible.
I know it's easy to get off on another tangent, and it's not really that big of a deal, but try to confine your comments to the discussion at hand. :)
Add Value.
If you're going to say something, please make it as useful as you can.
Ownership.
By choosing to post here, you understand that as owner of this site, I reserve the right to freely make use of the content within your comment, without first obtaining your permission. Otherwise, comment content belongs to its perspective poster.
Have Fun .
Enjoy participating!
For more detailed information, please be sure to review our privacy policy and terms of service.

