Movie Clip as a button and constantly checking rollover variable?
Hello,
I have a movie clip that I need to use as a button.
I am cuurently using 'on (rollOver)', and it is working, except that I need the movieclip/button to constantly update/read a variable.
My current code:
on (rollOver) { if ((_currentframe<10) &&
(_root.Playable == true)) {
checkFourH();
}
}
If the mouse is over this movieclip, and the variable 'Playable' changes from 'false' to 'true', it currently can't figure this out, until the mouse is moved away from the movie clip, and then moved over it again.
Is there a way to make the movie clip constantly check this variable, OR make the 'on (rollOver)' code constantly update?
I am guessing that an enterFrame event may be the way to go, but I don't know the command for "If mouse is over me" in an enterFrame event
(I am using Flash MX)
Last edited by RobotGames; 06-24-2003 at 06:51 PM.
on (rollOver) {
on (enterFrame) {
if ((_currentframe<10) && (_root.Playable == true)) {
checkFourH();
} // end if-statement
} // end on(enterFrame)
} // end on(rollOver)
"I really must get a thinner pencil. I ca'n't manage this one a bit: it writes all manner of things that I don't intend". // Alice, Through the Looking Glass
// button code
button_mc.onRollOver = function() {
this.onEnterFrame = function() {
if (someCondition) {
trace("condition is true");
} // end if statement
} // end onEnterFrame()
} // end onRollOver()
"I really must get a thinner pencil. I ca'n't manage this one a bit: it writes all manner of things that I don't intend". // Alice, Through the Looking Glass
Yes, the function is inside the movie clip, and it is ok for it to constantly run. (I think everything will work better if it does).
I have an alternate function that is supposed to run when the mouse leaves the movie clip. (This function clears all of the variables that checkFourH() affected).
This function, checkOut2(), had previously been under an on (rollOut) event.
I am now trying the following instead, yet it doesn't seem to work:
Check out how I did it in the attachment. As pellepiano said, you can't nest on() functions. Since you're using MX, it's much better to use function literals (defining the function like I did above). That way you can nest as many event functions as you want. And the onEnterFrame = function() makes the code repeat over and over again, which is what you want.
"I really must get a thinner pencil. I ca'n't manage this one a bit: it writes all manner of things that I don't intend". // Alice, Through the Looking Glass
The code is ok. I put a trace on it and it executes on every frame so it must be your function. Is it also inside the movieclip?How does it look like, what does it target?
Originally posted by retrotron This works (check out the attachment):
code:
// set a variable
var someCondition = true;
// button code
button_mc.onRollOver = function() {
this.onEnterFrame = function() {
if (someCondition) {
trace("condition is true");
} // end if statement
} // end onEnterFrame()
} // end onRollOver()
Thank you as well.
I didn't realize that 'if (someCondition)' would return a true or false on its own. (As opposed to "if (someCondition == true") That is very helpful.
Also, I often see variables initialized like so: "var someCondition"
What is the purpose of the 'var'? Is it something from c++ that just happens to work in flash, or does it have a purpose?
"I really must get a thinner pencil. I ca'n't manage this one a bit: it writes all manner of things that I don't intend". // Alice, Through the Looking Glass
Ok, it turns out the two functions conflict with eachother.
Is there a way to make the movieclip constantly check the state of the global/root variable called "Playable", yet only run the checkFourH() function once for each 'session' that the mouse is over it?
I basically need the following scenerios to work:
Scenerio 1: 'Playable' is set to true. Mouse is placed over the movie clip. The function runs once.
Scenerio 2: 'Scenerio 1' has taken place. Now, the mouse is moved away from the movie clip, then moved over the movie clip again. ('Playable' is still set to true). The function runs again, one time.
Scenerio 3: 'Playable' is set to false. The mouse is moved over the movie clip. The function should not run. A few seconds later, 'Playable' is set to true. The mouse has not moved, it is still over the movie clip. The function should now run one time.
I guess I should try using variables and if statements that keep the function from running more than once in a given occurance of the mouse being over the movie clip.
Is there a way to make the movieclip constantly check the state of the global/root variable called "Playable", yet only run the checkFourH() function once for each 'session' that the mouse is over it?
Something like this:
code:
button_mc.onRollOver = function() {
checkFourH(); // executes only once each time mouse rolls over
this.onEnterFrame = function() { // do this over and over again
if (Playable) { // if Playable is true
// perform actions here
} // end "if (Playable)"
} // end onEnterFrame()
} // end onRollOver()
Here's the thing. Movieclips by default will loop over and over again. If you have a clip with one frame, and you don't put a stop() in there, it will simply play that one frame, at which point it reaches the end of the timeline, so it starts over again, playing that frame again, and again, and again. So it plays that one frame over and over. Each time a movieclip comes to a new frame, it triggers an event called onEnterFrame. If you don't tell Flash to do something when that event is triggered, then it won't do anything, but if you do tell it do something, it will. You tell it to do something with the onEnterFrame with the onEnterFrame function:
code:
this.onEnterFrame = function() {
// do this everytime the frame is played
} // end onEnterFrame()
Only the code inside the onEnterFrame function will execute at every frame. In the above example, I put the checkFourH() function call outside the onEnterFrame(), so it will only execute once each time the onRollOver() does.
Of course, once we declare the onEnterFrame function and start it executing, then we have to manually stop it, or it will keep going. We can either delete the function altogether:
code:
delete this.onEnterFrame;
or we can just stop the movieclip from playing so that it doesn't enter any more frames and onEnterFrame() is consequently not triggered:
code:
this.stop();
So, you probably want this to be stopped when they roll out, so do this:
code:
button_mc.onRollOut = function() {
delete this.onEnterFrame;
} // end onRollOut()
Or, if you wanted to just use play() and stop(), maybe do it like this:
code:
// stop the button_mc clip from playing
// (i.e. from looping the 1st frame over and over again)
button_mc.stop();
// declare the onEnterFrame() function
// which will not execute until the movie plays a frame
button_mc.onEnterFrame = function() {
// do this code over and over again at each frame
} // end onEnterFrame()
// declare the onRollOver events and the onRollOut events
button_mc.onRollOver = function() {
// execute checkFourH() only once
checkFourH();
// start the frames playing so as to start onEnterFrame()
this.play();
} // end onRollOver()
button_mc.onRollOut = function() {
this.stop(); // stop the clip so onEnterFrame stops executing
} // end onRollOut()
"I really must get a thinner pencil. I ca'n't manage this one a bit: it writes all manner of things that I don't intend". // Alice, Through the Looking Glass