难以在Flex 4中创建动画皮肤

时间:2011-03-29 21:51:57

标签: flex animation flex4 mouseover skin

我的任务是创建此页面上按钮的图形样式的等效Flex 4实现: http://h.dwighthouse.com/temp/UDOP/2011-3-25_themeDevGlowing/

即,按钮上的动画发光效果。我已经得到了我认为适当的基于程序梯度的皮肤基础,但我有两个问题。

一,我不能让任何鼠标事件在皮肤上做出反应。我似乎无法找到有这个问题的其他人。在下面的代码中,当鼠标悬停时,永远不会调用startAnimation,但事件显然已经注册。要测试此代码,只需将skinClass="ButtonSkin"添加到主应用程序中的按钮声明,下面的代码是ButtonSkin.mxml文件。

<?xml version="1.0" encoding="utf-8"?>
<s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        initialize="init()"
        mouseEnabled="true">

    <fx:Metadata>
        [HostComponent("spark.components.Button")]
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            import flash.events.Event;

            [Bindable]
            private var animatedAlpha:Number = 0.8;
            private var animationDirection:Number = -1;

            private function backAndForth(value:Number, max:Number, min:Number, increment:Number):Number {
                value += (animationDirection * increment);
                if (value < min || value > max)
                {
                    animationDirection *= -1;
                    value += (animationDirection * increment * 2);
                }
                return value;
            }

            private function startAnimation(e:MouseEvent):void {
                animatedAlpha = backAndForth(animatedAlpha, 0.8, 0.3, 0.1); // Or something
                systemManager.addEventListener(MouseEvent.MOUSE_OUT, endAnimation);
            }

            private function endAnimation(e:MouseEvent):void {
                animatedAlpha = backAndForth(animatedAlpha, 0.8, 0.3, 0.1); // Or something
                systemManager.removeEventListener(MouseEvent.MOUSE_OUT, endAnimation);
            }

            private function init():void {
                parent.mouseChildren = true; // Otherwise mouse events don't fire in the skin
                clickableArea.addEventListener(MouseEvent.MOUSE_OVER, startAnimation);
            }
        ]]>
    </fx:Script>


    <s:states>
        <s:State name="up" />
        <s:State name="over" />
        <s:State name="down" />
        <s:State name="disabled" />
    </s:states>

    <s:Group top="0" bottom="0" left="0" right="0" id="clickableArea">
        <s:Rect top="0" bottom="0" left="0" right="0" radiusX="8" radiusY="8">
            <s:fill>
                <s:LinearGradient rotation="90">
                    <s:GradientEntry color="#000000" color.down="#bb0000" color.disabled="#3b3b3b" ratio="0" />
                    <s:GradientEntry color="#353535" color.down="#ff0000" color.disabled="#555555" ratio="1" />
                </s:LinearGradient>
            </s:fill>
        </s:Rect>

        <s:Rect top="0" bottom="0" left="0" right="0" radiusX="8" radiusY="8">
            <s:fill>
                <s:LinearGradient rotation="90">
                    <s:GradientEntry color.over="#8f1b1b" alpha="0" ratio="0" />
                    <s:GradientEntry color.over="#8f1b1b" alpha="0" ratio="0.4" />
                    <s:GradientEntry color.over="#8f1b1b" alpha="{animatedAlpha}" alpha.down="0" ratio="1" />
                </s:LinearGradient>
            </s:fill>
        </s:Rect>

        <s:Rect top="0" bottom="0" left="0" right="0" radiusX="8" radiusY="8">
            <s:stroke>
                <s:SolidColorStroke color="#000000" color.disabled="#333333" weight="1" />
            </s:stroke>
            <s:fill>
                <s:LinearGradient rotation="90">
                    <s:GradientEntry color="#ffffff" color.disabled="#9e9e9e" alpha="0.6" alpha.down="0.7" ratio="0" />
                    <s:GradientEntry color="#ffffff" color.disabled="#848484" alpha="0.2" alpha.down="0.4" ratio="0.45" />
                    <s:GradientEntry color="#ffffff" alpha="0" ratio="0.46" />
                </s:LinearGradient>
            </s:fill>
        </s:Rect>

        <s:Label id="labelDisplay" color="#ffffff" color.disabled="#aaaaaa" textAlign="center" baseline="center" paddingTop="7" paddingBottom="5" paddingLeft="10" paddingRight="10">
            <s:filters>
                <s:DropShadowFilter distance="0" angle="45" blurX="5" blurY="5" />
            </s:filters>
        </s:Label>
    </s:Group>

编辑我发现为什么鼠标事件没有被触发。默认情况下,该按钮显然具有mouseChildren false,这特别可以防止事件比自身低。在构造函数中使用parent.mouseChildren = true;可以解决此问题。关于问题的下一部分:连续动画。

除此之外,我会感谢一些帮助或指出我在每一帧调用一个函数的正确方向,以及使用鼠标事件打开和关闭所述增量调用的能力。 (如果这不是你在Flex中的动画,请原谅我,我对基于Flash的动画一无所知。)谢谢!

1 个答案:

答案 0 :(得分:1)

已找到两个问题的答案:

  1. 按钮会阻止他们的孩子(包括皮肤)接收鼠标事件。通过在皮肤的初始化函数中设置parent.mouseChildren = true;,可以正常接收鼠标事件。

  2. 事实证明,您可以使用与Javascript中的动画相同的方式在Flex(Actionscript)中制作动画:setInterval和clearInterval。