当鼠标移动到as3时移动对象

时间:2010-04-06 07:54:00

标签: flash

我做了一个flash应用程序,但我的问题如下 我有一个电影剪辑,我想显示它当用户鼠标悬停到任何背景图像,我也想要相同的剪辑跟随鼠标光标。如果这样做。 谢谢

1 个答案:

答案 0 :(得分:1)

在舞台上放了3个动画片段 首先命名mc1,然后命名为mc2,最后命名为mouse_mc

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    public class Main extends MovieClip
    {
        public function Main()
        {
            stage.addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove );
            mouse_mc.mouseEnabled = false;
            mc1.alpha = 0;
            mc2.alpha = 0;
        }
        private function _onMouseMove (event:MouseEvent):void
        {
            if(event.target is MovieClip)
            {
                var target:MovieClip = (event.target as MovieClip);
                if(target.alpha == 0){
                    target.alpha = 1;               
                }
                //trace( (event.target as MovieClip).name );                
            }       

            // follow mouse
            mouse_mc.x = mouseX;
            mouse_mc.y = mouseY;

            event.updateAfterEvent();
        }
    }
}

这将使mouse_mc跟随您的鼠标并在翻转时显示隐藏的动画片段。

对于完美的跟随鼠标,您也可以使用本机光标 http://active.tutsplus.com/tutorials/actionscript/quick-tip-setting-a-native-cursor-using-flash-player-10-2/

享受