The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 8 of 8

Thread: Popping bubble wrap HitTest

  1. #1

    Popping bubble wrap HitTest

    Hi, thank you all for the good tips they helped me go forward in my project!

    I'm working on this not-new-idea of bursting bubble warp. I figured it's a good way of learning . But I'm missing something and can't quite figure it out.

    I have a movie clip called "button_mc" that has the 3 states of the bubble:
    -A starting state "_up",
    -Cracking the bubble "_down" ( because this one has the popping sound);
    - and cracked "_downQuiet" ( the same without the noise) .

    I would like to be able to pop the bubbles with the mouse click and stay popped.
    I found this code that works ( thanks guys for the hitTest tutorial!):

    onMouseDown= function () {
    mx=_xmouse;
    my=_ymouse;
    if (button_mc.hitTest(mx, my)) {
    button_mc.gotoAndPlay("_down");
    }
    }

    BUT... if i click again on top it replays the _down with sound... which is not the idea because the bubble is already popped.
    So i tried to add and "else". but it didn't work :S

    onMouseDown= function () {
    mx=_xmouse;
    my=_ymouse;
    if (button_mc.hitTest(mx, my)) {
    button_mc.gotoAndPlay("_down");
    }
    else {gotoAndPlay("_downQuiet");
    }

    }

    What am I doing wrong? Some help would be appreciated.

    Plus one more thing. I wanted to go real pro and make 4 or 5 kinds so it gives a realistic effect But 4 or 5 wont be enough to fill the scene. Would i have to duplicate the MC's, change the name and make a separate code for each bubble? I guess this shows that I'm a newbie.

    Thanks already for the help!!

  2. #2
    1,391
    posts
    Registered User
    ok - so first, you've got the right idea, but the condition needs to be predicated on a different parameter - because here it is simply asking hitTest or not, but on the 'second' click hitTest is still true - so one solution would be to use the current frame position as the parameter - for instance:

    if( button_mc.hitTest(mx,my) && button_mc.currentFrame != 1 ) { ... }

    unfortunately under AS2 we cannot ask for the frame 'label' (we can in AS3) so you'll have to use a number, assuming that the 'up' state is the only state you'd want a pop in, the above will only work under that condition (that it is 'not' on frame 1)

    if 'downQuiet' state is after '_down' on the timeline and contains a stop, you shouldn't need to tell it to play again after calling play on the 'down' state - thereby eliminating the 'else' condition

    but further - if you have multiple buttons (which is would assume since there's going to be a lot of bubbles) - you should really add listeners to each button instance rather than using onMouseDown - additionally, when you assign the event handlers, you do not need to use hitTest as this is inherent in the interactive object - so you may want to consider using something more like this:
    Code:
    var bubbles:Array = [ bubble_mc ];
    
    function init():Void {
        for( var b in bubbles ) {
            bubbles[b].buttonMode = true;
            bubbles[b].onRelease = bubblePop;
        }
    }
    init();
    
    function bubblePop():Void {
        if( this.currentFrame != 1 ) this.gotoAndPlay( "_down" );
    }
    with the above, each element you place in the array will gain the same interactivity

    ------------------------
    there are couple of different solutions to the second issue, you could either, 1) have a MC with a few different animations on the 'down' state frame, and randomly select and play a chosen sequence, or 2) attach a randomly selected MC to the stage independent of the button itself
    Last edited by cbeech; April 25th, 2012 at 04:57 PM.

  3. #3
    Thank you so much for your response.

    I read your message 5 or 6 times. I do understand the logic of it, the array for multiple buttons ( which works), why an instant ( i read about it and it seems logical) and why not to use Hittest.

    I tried this :
    var bubbles:Array = [ bubble_mc, bubble_mc2 ];

    function init():Void {
    for( var b in bubbles ) {
    bubbles[b].buttonMode = true;
    bubbles[b].onRelease = bubblePop;
    }

    for( var b in bubbles ) {
    bubbles[b].buttonMode = true;
    bubbles[b].onRollOver = bubbleStay;
    }

    for( var b in bubbles ) {
    bubbles[b].buttonMode = true;
    bubbles[b].onRollOut = bubbleQuiet;
    }
    }
    init();

    function bubblePop():Void {
    if( this.currentFrame !=1 ) this.gotoAndPlay( "_down" );
    if( this.currentFrame !=2 ) this.gotoAndPlay( "_downQuiet" );
    }

    function bubbleStay():Void {
    if( this.currentFrame !=1 ) this.gotoAndStop( "_up" );
    if( this.currentFrame !=2 ) this.gotoAndStop( "_downQuiet" );
    }

    function bubbleQuiet():Void {
    if( this.currentFrame !=1 ) this.gotoAndStop( "_up" );
    if( this.currentFrame !=2 ) this.gotoAndStop( "_downQuiet" );
    }


    And of course, it works half way. I also tried an 'else' logic but with no success. The flaw is still when you roll over it goes to "_downQuiet" ( which is good) even if there was no button release (no good) and when you release it still stays on "_down" so it "pops" again. Sorry to ask again, I dont understand if my mistke is in syntax or logic.

    thanks!

    sara

  4. #4
    1,391
    posts
    Registered User
    cool - you've got the idea there in the loops but one can also assign those handers within the first loop, like in the below

    but yeah i think you've got some competing conditions in the logic here - really what i see is that normal up,over functionality should persist unless the bubble is popped, right? so what you need to ask in the over,out conditions is if the playhead is 'on' the correct frame, if not, do nothing

    so assuming that you have a '_over' frame, and it seems as though you have these on each frame (so that frame1 is _up, frame2 is _over), we only need to ask if it is on the opposing frame - once the pop is activated it will 'play' through _down, to the _downQuite frame and stop, never to be activated again

    i've also renamed the methods to demonstrate the functionality a bit clearer

    Code:
    var bubbles:Array = [ bubble_mc, bubble_mc2 ];
    
    function init():Void {
        for( var b in bubbles ) {
            bubbles[b].buttonMode = true;
            bubbles[b].onRelease = bubblePop;
            bubbles[b].onRollOver = bubbleOver;
            bubbles[b].onRollOut = bubbleOut;
        }
    }
    init();
    
    function bubblePop():Void {
        if( this.currentFrame < 3 ) this.gotoAndPlay( "_down" );
    }
    
    function bubbleOver():Void {
        if( this.currentFrame == 1 ) this.gotoAndStop( "_over" );
    }
    
    function bubbleOut():Void {
        if( this.currentFrame == 2 ) this.gotoAndStop( "_up" );
    }

  5. #5
    Final Solution !


    Thanks for the help again, but it did not work, although ut kept me going!! So thanks again for helping me and others. Fortunately some info came across and i find out how to make it work.

    This was the final solution:

    var bubbles:Array = [ bubble_mc, bubble_mc2 ];


    function init():Void {
    for( var b in bubbles ) {
    bubbles[b].buttonMode = true;
    bubbles[b].onRelease = bubblePop;

    }
    }
    init();

    function bubblePop():Void {
    this.gotoAndPlay( "whateveryouwant" );

    }


    If I understood correctly to call the bubble_mc a button "true" & choose to use the values _up , _over & _down, AS automatically sets the values as a button mode so it really becomes a button in its 3 states.

    All i needed to do is call my instances with other names and just make it play once to pop from frame 1, then just leave it to rest. I hope this sounds coherent enough, but it worked!

    So I had a lot of double coding for nothing.

    Anyway, fantastic. I learned a lot.

  6. #6
    1,391
    posts
    Registered User
    i'm glad that you got it worked out - yes, under AS2 invoking those labels causes the instance to act as a button, but only if you also apply a handler - however it is not necessarily needless to code for the states, simply because there is often can be other things that need to be done at that time

    the code does work properly - however i erred in the property name using the AS3 version(currentFrame) rather than AS2(_currentframe) - and i did not test it when i wrote it, so did not catch it - my bad

    - but i also notice that you may want to 'deactivate' the instance on click - so set the enabled property to false:
    Code:
    var bubbles:Array = [ button_mc, button_mc2 ];
    
    function init():Void {
        for( var b in bubbles ) {
            bubbles[b].buttonMode = true;
            bubbles[b].onRelease = bubblePop;
        }
    }
    init();
    
    function bubblePop():Void {
      this.enabled = false;
      this.gotoAndPlay( "_down" );
    }

  7. #7
    Yes! it definitely does work.
    Thanks so much.

    I was wondering today: Is there any possibility that if somebody pops all the bubbles ( so that means that all the instances have been played) could i display a message.

    Is there a code that controls the array? Something like: " if all bubblepops of the array variable are done, then go to frame 5?

  8. #8
    1,391
    posts
    Registered User
    yeah you can do something like that - but you'll need something to check 'for' - for example, if a button has been 'disabled' you know it's been popped - so by iterating through the array you can check each instance to see if it's enabled - if ANY are then break out of the loop and return the method - if not, then they are all popped and you could invoke a gameover routine or message of some type - it would go something like this:
    Code:
    function checkBubbles():Void {
        for( var b in bubbles ) {
            if( bubbles[b].enabled ) return;
        }
    
        //ALL POPPED (do other stuff)
        gotoAndStop(5);
    }
    then call checkBubbles() from the bubblePop() method as the last line - this will check the array each pop

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012