Search code examples
actionscript-3rollovercss

AS3 CSS- like classes for Objects


I'm very new to AS3.

I'd like to give certain objects in a MC the "class" ( meaning css like class ) rollover. So I can automatically create a rollOver that fades up all objects declared as rollOver objects.

jQuery exmaple

$("#myObject .rollOverObject").animate(...);

What would be the best way to achieve something like that in AS3?

Thank you in advance and best regards.


Solution

  • There is no such thing as CSS classes in AS3. What you can do, however, is create an EventListener on the MC's and do your animations within there. It'll look like this.

    for (var i : int = 0; i < mcContainer.numChildren; i++) 
    {
        // reference to a child of the container
        var mcChild:MovieClip = mcContainer.getChildAt(i) as MovieClip;
    
        // validate by name
        if(mcChild.name == "something you want to check")
        {
            mcChild.addEventListener(MouseEvent.ROLL_OVER, onMcRollOver);
        }
    }
    
    function onMcRollOver(event:MouseEvent) : void
    {
        // create a reference to the MovieClip that is rolled over
        var mcTarget:MovieClip = event.currentTarget as MovieClip;
    
        // do what you like with the mcTarget
        mcTarget.alpha = 0.5;
    }
    

    If you're done using the MovieClips, or don't need the ROLL_OVER listeners anymore you can remove these listeners in a simular way.

    for (var i : int = 0; i < mcContainer.numChildren; i++) 
    {
        // reference to a child of the container
        var mcChild:MovieClip = mcContainer.getChildAt(i) as MovieClip;
    
        // validate by name
        if(mcChild.hasEventListener(MouseEvent.ROLL_OVER))
        {
            mcChild.removeEventListener(MouseEvent.ROLL_OVER, onMcRollOver);
        }
    }
    

    A quick Google search can help you further with your learning.