AS3并行动画片段+链接

时间:2012-11-04 17:22:09

标签: actionscript-3 flash

我是as3的初学者。我想制作一个盒子,其上有两个平行的影片剪辑,鼠标悬停时加载并在鼠标移出时重新加入。我想添加电影片段的链接,但很长一段时间后我什么都没有。

代码如下所示:

import flash.events.MouseEvent;

for (var fl_ChildIndex:int = 0;
fl_ChildIndex < this.numChildren;
fl_ChildIndex++)
{
this.getChildAt(fl_ChildIndex).addEventListener(MouseEvent.MOUSE_OVER, nawierzch);
}

function nawierzch(event:MouseEvent):void
{
this.addChild(event.currentTarget as DisplayObject);
}
zlec.stop();

zlec.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
zlec.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
zlec.addEventListener(MouseEvent.CLICK, onClick);

zlec.buttonMode = true;
zlec.mouseChildren = true;


function onClick(event:MouseEvent):void
{
    trace("click")
var url:String = "http://www.motoopcja.pl";
var request:URLRequest = new URLRequest(url);
try {
       navigateToURL(request, '_blank');
    } catch (e:Error) {
        trace("Error occurred!");
    }

}

function onMouseOver(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

mc.removeEventListener(Event.ENTER_FRAME, rewind);

mc.play();
mc.addEventListener(Event.ENTER_FRAME, advance);
}

function onMouseOut(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

mc.removeEventListener(Event.ENTER_FRAME, advance);

mc.prevFrame();
mc.addEventListener(Event.ENTER_FRAME, rewind);
}

function advance(e:Event):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

if (mc.currentFrame == mc.totalFrames)
{
    mc.stop();
    mc.removeEventListener(Event.ENTER_FRAME, advance);
}
}

function rewind(e:Event):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

if (mc.currentFrame == 1)
{
    mc.stop();
    mc.removeEventListener(Event.ENTER_FRAME, rewind);
}
else
{
    mc.prevFrame();
}
}
wykonaj.stop();

wykonaj.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver2);
wykonaj.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut2);
wykonaj.addEventListener(MouseEvent.CLICK, onClick2);

wykonaj.buttonMode = true;
wykonaj.mouseChildren = true;

function onClick2(e:MouseEvent):void
{
trace("click")  
}

function onMouseOver2(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

mc.removeEventListener(Event.ENTER_FRAME, rewind);

mc.play();
mc.addEventListener(Event.ENTER_FRAME, advance);
}

function onMouseOut2(e:MouseEvent):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

mc.removeEventListener(Event.ENTER_FRAME, advance);

mc.prevFrame();
mc.addEventListener(Event.ENTER_FRAME, rewind);
}

function advance2(e:Event):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

if (mc.currentFrame == mc.totalFrames)
{
    mc.stop();
    mc.removeEventListener(Event.ENTER_FRAME, advance);
}
}

function rewind2(e:Event):void
{
var mc:MovieClip = MovieClip(e.currentTarget);

if (mc.currentFrame == 1)
{
    mc.stop();
    mc.removeEventListener(Event.ENTER_FRAME, rewind);
}
else
{
    mc.prevFrame();
}

}

1 个答案:

答案 0 :(得分:0)

问题可能是您没有导入所需的一切。 您还必须小心不要删除不存在的eventlistener。 这可能在您的代码中经常发生。这是一个例子:

zlec被推翻了。 zlec删除了rewind eventlistener(不存在)。

我不知道你是否有一些防止它的外部AS3,但它可能会引发错误。

这是一个更清洁的解决方案:

import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.events.Event;//imports

zlec.addEventListener(MouseEvent.ROLL_OVER,turnPar);
zlec.addEventListener(MouseEvent.ROLL_OUT,turnPar);
zlec.addEventListener(MouseEvent.CLICK,clickedBox);
zlec.buttonMode = true;//make the cursor change on roll over

for(var curChild:uint=0;curChild<this.numChildren) {
    this.getChildAt(curChild).addEventListener(Event.ENTER_FRAME,playPar);
}

var theState:String = "MouseEvent.ROLL_OUT";//by default rewind to frame 1
function turnPar(event:MouseEvent):void { theState = event.type; }

function clickedBox(event:MouseEvent):void {
    var url:String = "http://www.motoopcja.pl";//your website
    var req:URLRequest = new URLRequest(url);//as URLRequest
    navigateToURL(req,'_blank');//open in a new window
}

function playPar(event:Event):void {
    if("MouseEvent.ROLL_OUT" == theState && event.currentTarget.currentFrame > 1) { event.currentTarget.prevFrame(); }
    else if("MouseEvent.ROLL_OVER" == theState && event.currentTarget.currentFrame < event.currentTarget.totalFrames) { event.currentTarget.nextFrame(); }
}