Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onRelease vs. on (release) 2

Status
Not open for further replies.

bsquared18

Technical User
Jun 10, 2001
329
US
Hi,

Being new to object oriented programming, I'm working through the first chapter of a book on ActionScript 2 for beginners (I'm working in Flash MX 2004 for Professionals). In the example, the event on (release) is used. Elsewhere, every similar event I've seen has been one word, such as onRelease. The on (release) format seems strange to me. Could someone explain why one word isn't used, the way all the commands listed in the Actions Toolbox seem to be?

Thanks!

Bill
 
[tt]On(release)[/tt] is an old style event handler, needs to be attached to a Movie Clip/Button to get triggered by the mouse event "release".

[tt]MovieClip.onRelease/Button.onRelease[/tt] is more versatile, as it can reside anywhere - typically in a centralised location in the main timeline for all the scripts and/or in an external Class file.

For example, you can achieve the same thing using either of them:

[tt]//
on(release){
trace("boo")
}
//[/tt]

...is the same as:

[tt]//
this.onRelease = function(){
trace("boo")
}
//[/tt]

...but you can't use [tt]on()[/tt] anywhere other than the actual Button or Movie Clip. Nor you can apply it dynamically. Avoid it, I'd say!

Kenneth Kawamoto
 
Thanks, Kenneth. Yes, the script is attached to a button. I tried changing it and placing it with the other script on the Actions layer, but it didn't work. Maybe by looking at the actual button script, you can suggest how to do it.

Previously, data on the bill amount has been loaded from an external text file, and the user has entered the amount they are paying. What the button press is supposed to do is calculate whether someone has underpaid, overpaid, or paid just the right amount on a bill for which $60 is due. When the button is released, everything returns to its original status.

Below is the script as the authors have it. Let's assume I give the button the instance name of myButton. How would you suggest I change the code below so that it doesn't have to reside on the button?

on (press) {
var amountPaid:Number = Number (paid_txt.text);
var amountOwed:Number = Number (owed_txt.text);
if (amountPaid < amountOwed) {
var difference:Number = amountOwed - amountPaid;
stamp_mc.gotoAndStop ("underpaid");
message_txt.text = "You underpaid your bill by " + difference + " dollars.";
} else if (amountPaid > amountOwed) {
var difference:Number = amountOwed - amountPaid;
stamp_mc.gotoAndStop ("overpaid");
message_txt.text = "You overpaid your bill by " + difference + " dollars.";
} else {
stamp_mc.gotoAndStop ("paid_in_full");
message_txt.text = "You have paid your bill in full.";
}
}
on (release) {
stamp_mc.gotoAndStop ("none");
message_txt.text = "";
}

Thanks!

Bill

 
What Kenneth didn't say, is that when defining the button's or movie clip's handler (onRelease, or whatever handler...), that button or movie clip must be present on stage on the frame you define it's handler...

Thus if you define the handler on the first frame of the main timeline of your movie, and your button only appears on frame 5 of some movie clip, that itself only appears on frame 10 of the main timeline, it simply won't work.

You'd have to define it on frame 5 of that movie clip's timeline, or simply leave it on the button itself, using the on(release) format...

Regards. Affiliate Program - Web Hosting - Web Design

03/13/05 -> OLDNEWBIE VS FLASHK
 
Hmmm. I kind of get what you're saying but will have to play around with it some to really understand it.

Are you saying that in this case it may make more sense to leave the script on the button, as it is now?

Bill
 
While what oldnewbie said about the object presence on Stage is absolutely right,

>It's certainely much easier! And it works. Why try to fix it?

I say that's because it's not elegant. It's not elegant to look for a piece of cord buried in a Button in the frame 5 of a Movie Clip buried in the frame 10 of another Movie Clip if you want to debug a Movie when you are on a tight deadline. If you put all your cord in the "Actions" layer in the main timeline (or external Class), at least you or your co-worker knows where to look for the cord. It also forces you to construct your movie leaner, simpler and more efficient - you will think twice before nesting a Movie Clip within a Movie Clip within a Movie Clip; often there's another, cleaner way.

Anyway, looking at your code, it will always end up with stamp_mc being in the frame "none" and the message_txt showing nothing, regardless of the amount paid. Because whatever happens on(press), you are cancelling everything with on(release)!

If you want to translate your code into more object-orientated way with "onPress" and if it's not working, start with one line at a time:

[tt]//Main timeline
this.myButton.onPress = function() {
var amountPaid:Number = Number(paid_txt.text);
trace("amountPaid: "+amountPaid);
};
//[/tt]

Do you get the correct output? (Make sure "myButton" is on Stage as in oldnewbie's post.)

Kenneth Kawamoto
 
Kenneth,

You're absolutely right about what happens when the button is released, but remember, this isn't "my" code; it's code provided by the authors in the first chapter of a book teaching beginners how to use ActionScript 2. It's intended as a first example to walk the reader through, and I think it has served that purpose well.

Your insights on how to make coding more "elegant" will be very helpful as I become more skilled at using AS2.

Thanks to both Oldnewbie and you for answering my original question and discussing the pros and cons of attaching code to a button.

Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top