1

我是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();
}

}
4

1 回答 1

0

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

zlec 翻了个身。zlec 删除了倒带事件监听器(不存在)。

我不知道您是否有一些外部 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(); }
}
于 2012-12-01T11:31:12.817 回答