使图像在FLEX / AS闪存中移动

时间:2011-04-24 07:33:47

标签: actionscript-3 flash mxml mxmlc

在下面的代码中,点击水果图像如何使水果图像在框图像中落下并具有适当的效果(即,应该显示水果图像掉落到框图像中)。

          <?xml version="1.0" encoding="utf-8"?>
           <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

           <mx:Script>
              <![CDATA[
              import mx.controls.Button;
              import mx.controls.Alert;

              public function clickhandler(event:Event):void
              {

              }
                 ]]>

           </mx:Script>

               <mx:Canvas id="myCanvas" 
                 height="800" width="800"
                 borderStyle="solid" 
                 backgroundColor="#A9C0E7">

                 <mx:Image 
                   height="50" width="50" 
                   x="100" y="10"
                   source="@Embed(source='fruits.jpg')" 
                   click="clickhandler(event)" />

                 <mx:Image 
                   height="200" width="200" 
                   x="300" y="350" 
                   source="@Embed(source='box.jpg')" />
                </mx:Canvas>


           <!--<mx:TextInput x="231" y="175" id="txtLogin"/>-->



           </mx:Application>

2 个答案:

答案 0 :(得分:1)

http://www.greensock.com/tweenlite/

下载tweenLite库

然后您可以使用以下代码:

public function clickhandler(e:Event):void
{
    TweenLite.to(e.target, 0.5, {x: 300, y: 350});
}

答案 1 :(得分:1)

您可以使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
    <![CDATA[
        public function clickhandler(event:Event):void
        {
            fruitAnimation.play();
        }
    ]]>
    </mx:Script>

    <mx:Move id="fruitAnimation" target="{fruitImage}" xTo="375" yTo="450" />

    <mx:Canvas backgroundColor="#A9C0E7" borderStyle="solid" height="800" id="myCanvas" width="800">
        <mx:Image click="clickhandler(event)" height="50" id="fruitImage" source="@Embed(source='fruits.jpg')"
            width="50" x="100" y="10" />
        <mx:Image height="200" source="@Embed(source='box.jpg')" width="200" x="300" y="350" />
    </mx:Canvas>
</mx:Application>

如果您想添加抛物线移动,可以使用以下代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application layout="absolute" xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:Script>
    <![CDATA[
        import mx.effects.easing.Quadratic;
        public function clickhandler(event:Event):void
        {
            fruitAnimation.play();
        }
    ]]>
    </mx:Script>

    <mx:Parallel id="fruitAnimation" target="{fruitImage}">
        <mx:AnimateProperty property="x" toValue="375" easingFunction="{Quadratic.easeOut}" />
        <mx:AnimateProperty property="y" toValue="450" />
    </mx:Parallel>

    <mx:Canvas backgroundColor="#A9C0E7" borderStyle="solid" height="800" id="myCanvas" width="800">
        <mx:Image click="clickhandler(event)" height="50" id="fruitImage" source="@Embed(source='fruits.jpg')"
            width="50" x="100" y="10" />
        <mx:Image height="200" source="@Embed(source='box.jpg')" width="200" x="300" y="350" />
    </mx:Canvas>
</mx:Application>

希望这有帮助!