A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: Movie Clip as a button and constantly checking rollover variable?

  1. #1
    Member
    Join Date
    Jun 2003
    Posts
    35

    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.

  2. #2
    thinking is design retrotron's Avatar
    Join Date
    Apr 2003
    Location
    Utah
    Posts
    141
    Something like this?
    code:

    on (rollOver) {
    on (enterFrame) {
    if ((_currentframe<10) && (_root.Playable == true)) {
    checkFourH();
    } // end if-statement
    } // end on(enterFrame)
    } // end on(rollOver)


    absconditus.com // experiments in actionscripted design

    "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

  3. #3
    Member
    Join Date
    Jun 2003
    Posts
    35
    Thanks, however I am getting the following error:

    Frame=1: Line 2: on handlers may not nest within other on handlers
    on (enterFrame) {

  4. #4
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    Button event handlers are not allowed on movieclips ( im surprised you don get an error).
    And you can not have enterFrame code inside it.

    Instead you should use hitTest........It checks if the mouse position is ove the movieclips bounding box.

    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root._xmouse, _root._ymouse)) {
    		if ((_currentframe<10) && (_root.Playable == true)) {
    			checkFourH();
    		}
    	}
    }
    Note that when mouse overed it will constantly trigger your function ( is the function inside the movieclip?).

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  5. #5
    thinking is design retrotron's Avatar
    Join Date
    Apr 2003
    Location
    Utah
    Posts
    141
    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()

    Attached Files Attached Files

    absconditus.com // experiments in actionscripted design

    "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

  6. #6
    Member
    Join Date
    Jun 2003
    Posts
    35
    Thank You, that is incredibly helpful.

    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:

    Code:
    onClipEvent (enterFrame) {
    	if (this.hitTest(_root._xmouse, _root._ymouse)) {
    		if ((_currentframe<10) && (_root.Playable == true)) {
    			checkFourH();
    		}
    	} else {
    		checkOut2();
    	}
    }

  7. #7
    thinking is design retrotron's Avatar
    Join Date
    Apr 2003
    Location
    Utah
    Posts
    141
    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.

    absconditus.com // experiments in actionscripted design

    "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

  8. #8
    Senior Member pellepiano's Avatar
    Join Date
    Feb 2000
    Location
    Stockholm, Sweden
    Posts
    15,151
    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?

    -Pelle Piano
    // Image Gallery
    www.studiobild.com
    // Photo Blog
    http://talesofthepixel.blogspot.com

  9. #9
    Member
    Join Date
    Jun 2003
    Posts
    35
    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?

  10. #10
    thinking is design retrotron's Avatar
    Join Date
    Apr 2003
    Location
    Utah
    Posts
    141
    yeah, if(variable) tests whether variable = true or false.

    for more on "var", check out this thread:
    http://www.actionscript.org/forums/s...457#post145457

    absconditus.com // experiments in actionscripted design

    "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

  11. #11
    Member
    Join Date
    Jun 2003
    Posts
    35
    Oops...


    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.

  12. #12
    thinking is design retrotron's Avatar
    Join Date
    Apr 2003
    Location
    Utah
    Posts
    141
    the two functions conflict with eachother.
    Which two functions? How do they conflict?
    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()


    absconditus.com // experiments in actionscripted design

    "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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center