2

I have three movie clips all linked to the stage and I want them to behave like a button/ But I am not using a button because I have not found a way to have each part (up, over, down, hit) be animated and not just change when the mouse is in use with it. So far I have been able to have all three appear on my stage and show when I have the mouse over and as well when I click, but I think I'm doing something wrong with removeChild. Each MC should appear one at a time and now all three show up when I hover over and seem to "flash". Here's my code:

var mainMoon:swayingMoon = new swayingMoon();
mainMoon.x = 50;
mainMoon.y = 10;
addChild(mainMoon);

var hoverMoon:glowMoon = new glowMoon();
hoverMoon.x = 50;
hoverMoon.y = 10;

var movieMoon:clickedMoon = new clickedMoon();
movieMoon.x = 50;
movieMoon.y = 10;

mainMoon.addEventListener(MouseEvent.ROLL_OVER, showHoverMoon);
mainMoon.addEventListener(MouseEvent.ROLL_OUT, hideHoverMoon);
hoverMoon.addEventListener(MouseEvent.CLICK, startMovieMoon)
function showHoverMoon(event:MouseEvent):void
{
 addChild(hoverMoon);

}
function hideHoverMoon(event:MouseEvent):void
{
removeChild(hoverMoon)
}
function startMovieMoon(event:MouseEvent):void
{
addChild(movieMoon);
}
Vesper
  • 18,599
  • 6
  • 39
  • 61

3 Answers3

0

I don't recommend doing it this way as it can make things needlessly complex. For a single button now, you have 3 times as many movie clips/sprites, add/remove child event handlers, and other variables to check/debug for. Multiply this by however many buttons you have.

Instead, I'd recommend using or extending the SimpleButton{} class or writing your own class to encapsulate the behaviour.

SimpleButton class: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/SimpleButton.html

mitim
  • 3,169
  • 5
  • 21
  • 25
0

I think you should encapsulate your three moon states into a separate MovieClip that will control all the moon phase changing by itself. If it's already so, fine. The general principle with such a MovieClip-Button type objects is that the listener is assigned to the parent instance, not the parts of that button.

public class Moon extends Sprite {
    private var upState:swayingMoon=new swayingMoon();
    private var overState:glowMoon=new glowMoon();
    private var downState:clickedMoon=new clickedMoon();
    private var areWeClicked:Boolean;
    private var areWeOver:Boolean;
    public function Moon() {
        areWeClicked=false;
        areWeOver=false;
        addChild(upState);
        addEventListener(MouseEvent.ROLL_OVER, showHoverMoon);
        addEventListener(MouseEvent.ROLL_OUT, hideHoverMoon);
        addEventListener(MouseEvent.CLICK, showClickedMoon);
        addEventListener(Event.COMPLETE, hideClickedMoon);
    }
    private function showHoverMoon(e:MouseEvent):void {
        areWeOver=true;
        if (areWeClicked) return;
        removeChild(upState);
        addChild(overState);
    }
    private function hideHoverMoon(e:MouseEvent):void {
        areWeOver=false;
        if (areWeClicked) return;
        removeChild(overState);
        addChild(upState);
    }
    private function showClickedMoon(e:MouseEvent):void {
        if (areWeClicked) {
            downState.gotoAndPlay(1);
            return;
        }
        if (overState.parent) removeChild(overState); else removeChild(upState);
        addChild(downState);
        downState.gotoAndPlay(1); // your clicked moon seems to be a playing MC, so starting it over
        areWeClicked=true;
    }
    private function hideClickedMoon(e:Event):void {
        if (e.target!=downState) return; // not our event
        if (!areWeClicked) return;
        areWeClicked=false;
        removeChild(downState);
        if (areWeOver) addChild(overState); else addChild(upState);
    }
}

Now, your parent class is controlling what happens and when. I was under assumption that your clickedMoon MC will only play once fully, then dispatch an event Event.COMPLETE to itself, so that its parent will get notified and will act.

Originally, your event listener structure prevented you from hiding mainMoon MC, otherwise your other listener will never act, now you are using parent object to listen to events, and can safely remove parts of your moon.

Vesper
  • 18,599
  • 6
  • 39
  • 61
0
Just for a custom button you are going too much complex way. one movieClip is enough for create the button. First create a movieClip and inside that movieClip's timeline create two more frame for 'hover' and click effect. here is little bit of code to start.


var myButton_btn:CustomButton = new CustomButton();
addChild(myButton_btn);
myButton_btn.x = 100;
myButton_btn.y = 100;

myButton_btn.addEventListener(MouseEvent.ROLL_OVER, onOver);
myButton_btn.addEventListener(MouseEvent.ROLL_OUT, onOut);
myButton_btn.addEventListener(MouseEvent.CLICK, onClick);

function onOver(event:MouseEvent):void {
    //trace('over');
    event.target.gotoAndStop('hover');
}

function onOut(event:MouseEvent):void {
    //trace('normal');
    event.target.gotoAndStop('normal');
}

function onClick(event:MouseEvent):void {
    //trace('click');
    event.target.gotoAndStop('click');

}
naresh kumar
  • 2,165
  • 3
  • 19
  • 21