Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:13:20+00:00 2026-05-27T16:13:20+00:00

In my game, if the ball hits the winBox, you get a Win message.

  • 0

In my game, if the ball hits the winBox, you get a Win message. The problem is that the ball bounces, and it might hit the box repeatedly. How can I prevent the message from happening twice per session?

            if (simContacts.isCurrentContact(winBox,gameBall))
        {
            // Won.
            trace ('You won!');
            isFiring=false;

            //Win Message
            winMsg=new win_mc();
            addChild(winMsg);
            winMsg.y=250;
            winMsg.x=40;
            winMsg.gotoAndPlay(2);

        }

Complete Code:

package  {

import flash.display.MovieClip;
import com.actionsnippet.qbox.*;  
import flash.events.MouseEvent;
import flash.events.Event;

[SWF(width = 300, height = 600, frameRate = 60)]  

public class LuckyHitBeta extends MovieClip {

    public var sim:QuickBox2D;
    var winBox:QuickObject;
    var looseBox:QuickObject;
    var looseBox2:QuickObject;
    var gameBall:QuickObject;
    var simContacts:QuickContacts;
    var isFiring:Boolean=false;
    var ballIcon:ballIcon_mc;
    var winMsg:win_mc;



    /**
     * Constructor
     */
    public function LuckyHitBeta()
    {   
        liveIcon();
        sim = new QuickBox2D(this);
        simContacts = sim.addContactListener();
        simContacts.addEventListener(QuickContacts.ADD, contactListener);
        //Important Collisions;

        winBox = sim.addBox({x:2,y:597/30, width:98/30, height:10/30, density:0, skin:stick, skinScale:false});
        looseBox = sim.addBox({x:5,y:597/30, width:98/30, height:10/30, density:0, skin:stick, skinScale:false});
        looseBox2 = sim.addBox({x:8,y:597/30, width:98/30, height:10/30, density:0, skin:stick, skinScale:false});
        //Collisions
        sim.addBox({x:3.5,y:560/30, width:72/30, height:13/30, density:0, skin:stick, skinScale:false, angle:1.57});
        sim.addBox({x:6.5,y:560/30, width:72/30, height:13/30, density:0, skin:stick, skinScale:false, angle:1.57});
        sim.addBox({x:0/30,y:300/30, width:600/30, height:28/30, density:0, skin:stick, skinScale:false, angle:1.57});
        sim.addBox({x:300/30,y:300/30, width:600/30, height:28/30, density:0, skin:stick, skinScale:false, angle:1.57});



        // make obstacles 
        for (var i:int = 0; i<(stage.stageWidth/28); i++){ 
        //End
             sim.addCircle({x:1.2 + i * 0.8, y:16, radius:0.15, density:0, skin:ping, scaleSkin:false});
             sim.addCircle({x:0.6 + i * 0.8, y:15, radius:0.15, density:0, skin:ping, scaleSkin:false});
        }

        for (var o:int = 0; o<(stage.stageWidth/55); o++){  
        //Middle

              sim.addCircle({x:1.3 + o * 1.5, y:14, radius:0.15, density:0, skin:ping, scaleSkin:false});
              sim.addCircle({x:1.3 + o * 1.5, y:13, radius:0.15, density:0, skin:ping, scaleSkin:false});
              sim.addCircle({x:1.3 + o * 1.5, y:12, radius:0.15, density:0, skin:ping, scaleSkin:false});
              sim.addCircle({x:1.3 + o * 1.5, y:11, radius:0.15, density:0, skin:ping, scaleSkin:false});
              sim.addCircle({x:1.3 + o * 1.5, y:10, radius:0.15, density:0, skin:ping, scaleSkin:false});
        }

        for (var u:int = 0; u<(stage.stageWidth/45); u++){    
        //Start
             sim.addCircle({x:0.8 + u * 1.3, y:09, radius:0.15, density:0, skin:ping, scaleSkin:false});
             sim.addCircle({x:1.55 + u * 1.3, y:08, radius:0.15, density:0, skin:ping, scaleSkin:false});
             sim.addCircle({x:0.8 + u * 1.3, y:07, radius:0.15, density:0, skin:ping, scaleSkin:false});
             sim.addCircle({x:1.55 + u * 1.3, y:06, radius:0.15, density:0, skin:ping, scaleSkin:false});

        }
        addEventListener(Event.ENTER_FRAME,onEnterFrm);
        stage.addEventListener(MouseEvent.CLICK, _clicked);
        sim.start();



    }

    /**
     * ..
     * @param e MouseEvent.CLICK
     */
     private function _clicked(e:MouseEvent)
     {  
        if (! isFiring){
            gameBall = sim.addCircle({x:(mouseX/30), y:(2), radius:0.15, density:1, restitution:0.7, friction:0, skin:ball, scaleSkin:false, angularDamping:20});
            isFiring=true;
            ballIcon.gotoAndStop(2);

        }  
     }

    private function liveIcon():void {
        ballIcon=new ballIcon_mc();
        addChild(ballIcon);
        ballIcon.y=30;
    }        

     private function onEnterFrm(e:Event):void
     {          
        ballIcon.x=mouseX;
        if (gameBall && gameBall.body.IsSleeping())
            {
                gameBall.destroy();
                gameBall = null;
                isFiring=false;
                ballIcon.gotoAndStop(1);
            }  
    }

    private function contactListener(e:Event)
    {
        if (simContacts.isCurrentContact(winBox,gameBall))
        {
            // Won.
            trace ('You won!');
            isFiring=false;
            ballIcon.gotoAndStop(1);

            winMsg=new win_mc();
            addChild(winMsg);
            winMsg.y=250;
            winMsg.x=40;
            winMsg.gotoAndPlay(2);

        }

        if (simContacts.isCurrentContact(looseBox,gameBall))
        {
            // Loose.
            trace ('You loose!');
            //gameBall({friction:1,restitution:0});
            isFiring=false;
            ballIcon.gotoAndStop(1);
        }
    }


}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:13:21+00:00Added an answer on May 27, 2026 at 4:13 pm

    To make the “You win!” message fire once per ball, remove the event listener when the player wins, and add it each time the user creates a ball. It’s ok to add an event listener to the same object multiple times; the event listener function will still only get called once.

    private function clicked(e:MouseEvent)
    {  
        if (!isFiring)
        {
            // Add the ball.
            simContacts.addEventListener(QuickContacts.ADD, contactListener);
        }
    }
    
    ...
    
    private function contactListener(e:Event):void
    {
        if (simContacts.isCurrentContact(winBox, gameBall))
        {
            // You win.
            simContacts.removeEventListener(QuickContacts.ADD, contactListener);
        }
        else if (simContacts.isCurrentContact(winBox, gameBall))
        {
            // You lose.
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using AndEngine/Box2d to develop a game. I have a ball that bounces around
I have a game that one player X wants to pass a ball to
I'm making an iPhone game where the main actor is a ball that rolls
I am working on a game in OBJ C that has a ball view
I have a basic game function that drops a ball every 500 milliseconds (with
I'm writing a pong game and I have a ball class that has velocity
I want to create a Pong game with a moving ball and all the
i'm working on a Pong game now and I my ball animation is going
I am developing a game in java just for fun. It is a ball
i want to create a Pong game. I want to move the ball in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.