Oscar Pico
Oscar Pico

Reputation: 5

Changing cursor in an AS3 game using buttons

I am trying to make my game change the cursor when pressing a button. However after pressing one of the buttons, I'm unable to change it again (seems like the cursor gets in the way). I thought setting mouseCursor.mouseEnabled = false; would fix it, but it seems like it didn't. Here's my code, could someone tell me what am I doing wrong please?

package
{
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import flash.ui.*;

import Game.*;

public class GameController extends MovieClip
{
    private var mouseCursor:MovieClip;

    public function GameController()
    {

    }

    private function followMouse(evt:Event)
    {
        mouseCursor.x = mouseX;
        mouseCursor.y = mouseY;
    }

    public function startGame()
    {   
        needleOn = false;

        mcGameUI.btnMixBlue.addEventListener(
                MouseEvent.CLICK, mixBlue);

        mcGameUI.btnMixRed.addEventListener(
                MouseEvent.CLICK, mixRed);

        mcGameUI.btnMixYellow.addEventListener(
                MouseEvent.CLICK, mixYellow);

        mcGameUI.btnNeedle.addEventListener(
                MouseEvent.CLICK, activateNeedle);


        mcGameUI.mouseEnabled = false;
    }       

    private function mixBlue(evt:MouseEvent)
    {
        if (mouseCursor != null)
        {
            removeChild(mouseCursor);
            mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);         
            mouseCursor = null;
        }
            mouseCursor = new BlueBubble(mouseX,mouseY);
            mouseCursor.gotoAndPlay("idle");
            mouseCursor.mouseEnabled = false;
            mouseCursor.addEventListener(Event.ENTER_FRAME,followMouse);            
            addChild(mouseCursor);
    }
    private function mixRed(evt:MouseEvent)
    {
        if (mouseCursor != null)
        {
            removeChild(mouseCursor);
            mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);         
            mouseCursor = null;
        }
            mouseCursor = new RedBubble(mouseX,mouseY);
            mouseCursor.gotoAndPlay("idle");
            mouseCursor.mouseEnabled = false;
            mouseCursor.addEventListener(Event.ENTER_FRAME,followMouse);            
            addChild(mouseCursor);
    }
    private function mixYellow(evt:MouseEvent)
    {
        if (mouseCursor != null)
        {
            removeChild(mouseCursor);
            mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);         
            mouseCursor = null;
        }
            mouseCursor = new YellowBubble(mouseX,mouseY);
            mouseCursor.gotoAndPlay("idle");
            mouseCursor.mouseEnabled = false;
            mouseCursor.addEventListener(Event.ENTER_FRAME,followMouse);            
            addChild(mouseCursor);
    }

    private function activateNeedle(evt:MouseEvent)
    {
        if (mouseCursor != null)
        {
            removeChild(mouseCursor);
            mouseCursor = null;
            mouseCursor.removeEventListener(Event.ENTER_FRAME,followMouse);         
        }
        mouseCursor = new Needle();
        mouseCursor.mouseEnabled = false;
        addChild(mouseCursor);
        mouseCursor.addEventListener(Event.ENTER_FRAME,followMouse);            

    }


}   
}

Upvotes: 0

Views: 112

Answers (1)

you should set:

mouseCursor.mouseEnabled = false;
mouseCursor.mouseChildren = false;

mouseEnabled turns off only the container mouseCursor but inside this clip you still can have mouseEnabled elements thus mouseChildren should be also turned off.

mouseChildren is defined in DisplayObjectContainer which means that cursors made out of shape or bitmap will not have this property, so you need to test if it exist first.

if(mouseCursor is DisplayObjectContainer){
{
    mouseCursor.mouseChildren = false;
}

Upvotes: 1

Related Questions