AS3 - Tools that drag and rotate in flash TOOL MODE

483 Views Asked by At

I'm making an app/program that allows you to design your own piece of art with two basic functions by clicking the buttons on the UI, We'll call the 'Modes' for now.

Tweezer mode: you can drag the selected set of objects about in this mode. Rotate Mode: this allows you to rotate a certain set of movie clips on the stage.

Assigning it. I want it so that when rotate mode or tweezer mode is active the other is disenaged (enabled = false) or to that effect. I have arrived at a fork in the road where I need group them under method rotate or method tweezer. When tweezer is clicked, you move stuff about (only), and when rotate mode is selected you can rotate the movie clips...(only) this works fine until after you come away from rotate mode back tweezer that you can still rotate the movie clip! Could anyone shed some light on this so when I leave this mode you still can rotate it? Could any suggest the best way to organize this sort of functionality?

Thanks for your help - I'm an AS3 newbie.

// UI btns TOOLS ---------------------

spinny_mc.addEventListener(Event.ENTER_FRAME, fl_RotateContinuously);

function fl_RotateContinuously(event:Event)
{
    spinny_mc.rotation += 20;
}

 rotate_btn.visible = true;
 tweezer_btn.visible = false;

 //----- rotate tool

rotate_btn.addEventListener(MouseEvent.CLICK, spinmode);

function spinmode(event:MouseEvent):void
{
    Mouse.hide();
    stage.addEventListener(MouseEvent.MOUSE_MOVE,followspin);
    function followspin(evt:MouseEvent)
    {
    spinny_mc.x = mouseX;
    spinny_mc.y = mouseY;
    rotate_btn.visible = false;
    tweezer_cur.visible = false;
    tweezer_btn.visible = true;
    rotate_btn.enabled = true;  
    tweezer_btn.enabled = false;        
    skullface_mc.addEventListener(MouseEvent.CLICK, turnerbone);

        function turnerbone(event:MouseEvent):void
        {
            skullface_mc.rotation+=45;
        }
    }
}

// ------------------------ tweeze tool
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
function follow(evt:MouseEvent){
    tweezer_cur.x = mouseX;
    tweezer_cur.y = mouseY;
}

tweezer_btn.addEventListener(MouseEvent.CLICK, tweezer);

function tweezer(event:MouseEvent):void
{
    Mouse.hide();
    stage.addEventListener(MouseEvent.MOUSE_MOVE,tweezer);
    function tweezer(evt:MouseEvent){
        tweezer_cur.x = mouseX;
        tweezer_cur.y = mouseY;
        rotate_btn.visible = true;
        tweezer_cur.visible = true;
        tweezer_btn.visible = false;
        spinny_mc.visible = false;
        rotate_btn.enabled = false; 
        tweezer_btn.enabled = true; 
    }
}
1

There are 1 best solutions below

2
On

The problem comes down to your event listeners. They don't go away after you leave the function, as they are essentially objects in and of themselves. You need to do that manually.

At the top of the spinmode() function, add the line stage.removeEventListener(MouseEvent.MOUSE_MOVE,tweezer);

At the top of the tweezer() function, add the line stage.removeEventListener(MouseEvent.MOUSE_MOVE,followspin);

WARNING: I cannot rightly recall if this will throw an error if the event listener has not yet been created. If so, you can get around that using a Try/Catch, or an if-statement that links to a variable indicating what mode the user is in.


You may also need to rearrange your code a bit. I do not recommend nesting functions that affect the stage, as they can make life kinda difficult. Define the functions separately, and then call the other functions from the one you want.

Keep in mind, ActionScript is Object-Oriented, not procedural (and NOT strictly top-down). I'm not sure if you're coming from another language, but I know that took a lot of getting used to for me, coming from a procedural top-down technique.

WRONG

function myFunction1()
{
   function myFunction 2()
   {
      //foobar
   }
}

RIGHT

function myFunction1()
{
   myFunction2();
}

function myFunction2()
{
   //foobar
}