MX: onClipEvent phenomena

Doh!
Having got my AS to work nicely - I was happy as the everything fitted into to place - so I moved onto adding a few more buttons placed in some new MCs…

…problem: When I press buttons that having nothing to do with the main script a dynamic border ,that changes size to reveal a new picture when it is loaded after a preload, decides to do a loop. Even the main picture flickers as if it has also been reloaded even though that script is executed by a button with an instance name.

The border MC uses a onClipEvent (enterframe) command to initiate the script - does this mean it reponds everytime any MC has something happen to it??

If so, can I somehow write the AS so that the onClipEvent refers to the event of a specific MC so it does not respond to all MC events??

Osc
ps I hope that made sense:)

Hi,

enterFrame runs in a loop. So its always looking at itself incase something happens, if something happens then it acts accrodingly.

If you want post your fla and i’ll have a look for you, it’ll cost you one chocolate biscuit tho :thumb:

Viru.

Thats very kind - howveer I am not at home so son’t have access to my files or tin of biscuits:)

So if the enterframe works as a loop (learn something everyday) and acts when something happens to it (eg a variable is changed) then why does it act when a button is pressed on anothre MC (that button has no script in it!)

Although -just had a thought - I do use the following for a script on the main stage relating to buttons with the instance name ‘thum1’, ‘thum2’ etc .If i’m honest I do not understand this part of the script…

[AS]
Button.prototype.onRelease = function() {
this.Name = string(this)
this.Num = this.Name.substr(this.name.length -1, 1)
//then my script to load jpegs and set variables - these variables affect the MC with enterframe.
}
[/AS]

BUT - the unrelated MC has a completely different instance name!
Do you wreckon this could be a problem?

And yes…I have a good supply of chocolate biscuits:)

If your problem is onEnterFrame running a script when you don’t want it to, one solution is to assign “null” to onEnterFrame when it is not supposed to do anything.

When you want onEnterFrame to work simply assign whichever function you want to onEnterFrame at that time. Inside that function be sure to set myClip_mc.onEnterFrame = null; so it will run once and then be set to null.

This worked for me when I was building a timer that I wanted to run for, say, 3 seconds, do something and then go away. I assigned the timer function (with above statement in it) to a movie clip. It used onEnterFrame to check and update the “clock” and when time was up set onEnterFrame to null thus eliminating itself.

Hope this helps.

All movieclips play over and over again by default. Unless you stop them with a stop() or gotoAndStop(), they will simply play through their frames, then start over again at the beginning. If you only have one frame in your movieclip, then it will play that single frame over and over again.

onEnterFrame, as mentioned above, is triggered each time a frame is entered. Since you aren’t stopping the playhead anywhere, onEnterFrame will keep executing indefinitely.

Two solutions:

  1. Stop the playhead so the next onEnterFrame() won’t execute.
  2. Disable the onEnterFrame() by either deleting the onEnterFrame:

delete myClip_mc.onEnterFrame;

or set the onEnterFrame to null as JDLenner suggested:


myClip_mc.onEnterFrame = null;

That could be a solution, does this mean that once I have set the enterframe to null, I can set it to true?:

[AS]
myClip_mc.onEnterFrame = true;
//to restart loop?
[/AS]

Interestingly, once I removed the following:
[AS]
Button.prototype.onRelease = function() {
this.Name = string(this)
this.Num = this.Name.substr(this.name.length -1, 1)
//then my script to load jpegs and set variables - these variables affect the MC with enterframe.
}
[/AS]
It worked fine and the onenterframe script in the other MC did not run as this script had not been run again after being started by an unrelated button release!

Either I keep the above prototype script and get the onenterFrame to set to true when I want it to run or paste the main AS for loadingthe jpegs on every thumbnail button - however this will make changes to the script labourious as I have to change it on 60 buttons rather than 1 frame!!!
Any suggestions on reliability?

I’m not sure I totally understand you, but using prototype will cause that script to be affected for all buttons. The prototype property has to do with OOP, that dreaded Object Oriented Programming. If you want a simpler explanation (I hope it’s simpler ;)), check out a few articles on my website:

http://www.absconditus.com/articles/oop1.htm
http://www.absconditus.com/articles/oop2.htm

That might explain better what the prototype property does.

In any case, know that if you attach anything to the prototype, then it will be available to every instance of that class. So anything attached to


Button.prototype

will be available to every button in your movie. Likewise, anything attached to


MovieClip.prototype

will be available to every MovieClip in your movie.

For example, if you want to make a MovieClip into a button, you simply have to add a click handler function to it, something like onPress, onRelease, onRollOut, etc. E.g. I could turn a clip named mySquare_mc into a button simply by adding this code:


mySquare_mc.onPress = function() {
    trace("movieclip has been clicked");
} // end mySquare_mc.onPress()

Try it, you’ll see that now you can move your mouse over the movieclip, at which point it will turn to a hand cursor, and when you click it, the trace will execute. Simple enough.

Now suppose we wanted to turn every movieclip into a button just like this. We simply add the above onPress() function to MovieClip.prototype:


MovieClip.prototype.onPress = function() {
    trace("movieclip has been clicked");
} // end mySquare_mc.onPress()

If you do that, you’ll notice that every single movieclip in the .fla will now act like a button…when your mouse passes over it, it turns to a hand cursor, and you can click it and the trace will execute.

So your Button.prototype does basically the same thing. That’s why when you click one button, other’s start looping too.

You’re right, it will be laborious if you remove it and then have to add that code to every button, but it might make things a little clearer.

Another alternative is to leave the prototype code in there, but just make sure that it’s going to do the right thing. Be sure that the code executed in Button.prototype.onRelease() can safely be executed by every button in your movie. If you have code that’s specific for a button, perhaps pass a variable into the function and then use an if statement. Suppose I have two buttons: a_btn and b_btn. I could add a variable to each one:


a_btn.buttonName = "a_btn";
b_btn.buttonName = "b_btn";

Then, inside the Button.prototype, I could something like this:


Button.prototype.onRelease = function() {
    trace("button has been clicked"); // do this for all buttons
    // determine which button via buttonName variable,
    // and execute only some code accordingly
    if (this.buttonName == "a_btn") {
        trace("this is the a_btn");
    } else if (this.buttonName == "b_btn") {
        trace("this is the b_btn");
    } else { 
        trace("this is neither a_btn nor b_btn");
    } // end if statement
} // end onRelease() method

Something like that might be a way to go.

And a button cannot hold variables.

Ah, good point. :slight_smile: So it’s best to use movieclips as button instead of buttons.

aurelius
I was doing a search on click handler, and this thread came up in my search. I wanted to thank you for sharing links to your articles on object-oriented programmed. Very easy to understand- good information for those of us trying to get a grip on AS-
Thanks!

:nerd:

Cool, I’m glad they helped. :wink: Feel free to email me any constructive criticism, they’re still just in draft form.

Goodness! Thank you - it certainly answers many of my questions.

Using the if statement is useful but if I have over 60 buttons it is perhaps not as useful -however - can I tell the script not to do something if certain buttons are pressed eg 5 buttons to be precise? I’ll give it a try.

Osc

:love: aurelius

:wink:

I wonder why people have problemswith this OOP thing? I just went on learning more of as, and I suddenly realized after reading some tuts and articles that I knew OOP! :smiley: Handy.

its not all that hard. Its just a matter of remembering what does what and finding a good explanation of it all (there seems to be little)

Yeah, senocular is the only good source of OOP explanation I have found. :wink:

But more than just good explanation, what’s so great about senocular is his willingness to take the time to explain things to the hyper-inquisitive and brain-fart…ive (like me).