0

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; 
    }
}
CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
user3082874
  • 61
  • 2
  • 11

1 Answers1

0

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
}
CodeMouse92
  • 6,840
  • 14
  • 73
  • 130
  • Hello, thanks for the pointer. I was getting a feeling, an intuition! With the nesting function it would cause me hassle further dowm the line, I would need to do that with about 20 objects on the stage. That's why I figured i would come here and as someone such as yourself who know greater that than me about AS3 - I'm still new to it and it's considerably different to AS2. Thanks Jason, I will have a tinker over the next couple of days. :-) – user3082874 Jan 09 '14 at 09:30
  • If you can, get your hands on the ActionScript 3.0 Bible. AS3 has a lot of snazzy new features that will come in handy. Arguably, it has graduated to a full-blown OOP language. http://www.amazon.com/ActionScript-3-0-Bible-Roger-Braunstein/dp/0470135603 – CodeMouse92 Jan 09 '14 at 16:24