0

我已经用 Flash 开发超过 15 年了,最近开始用 Flash Actionscript 3 开发游戏。我遇到了一些困难,需要一些帮助。我花了几天时间试图找到解决方案,但没有运气。

我有一个主 SWF,它加载子 SWF 动画。一个对象可以有 5 种不同的动画/SWF 与之关联。假设我有一只鸡,chicken01.swf,chicken02.swf,...

我将 MOUSE_DOWN 事件分配给第一个加载的 SWF,然后根据单击对象时使用的工具,它将加载其他动画。我的问题是每个 SWF 周围都有空白区域,可以点击。我只需要可点击的对象,而不需要空白空间,因为一些对象可以相互重叠,这使得点击另一个对象后面的对象变得很困难。

子 SWF / 动画位于单个时间轴上,我使用位图跟踪来删除导入的 PNG 对象周围的空白区域。如果我将舞台大小减小到对象后面,则此方法有效,但由于舞台大小小于对象,因此会增加加载的 SWF 的大小。因此,当我为对象分配宽度和高度时,使用较小的阶段,对象就很大。如果我将舞台大小限制为对象的大小,即使是跟踪位图图像,舞台仍然是可点击的。我试图从主 SW​​F 将 MOUSE_DOWN 事件分配给子 SWF 上的对象,但这会产生错误。

我的目标是加载一个子 SWF,分配 MOUSE_DOWN 事件,并且只有对象可点击,而不是舞台或对象周围的空白空间。

这可能吗?我还尝试过创建一个不可见的按钮,但这使得分配给 300 多个不同形状和大小的对象变得困难。

下面是我正在使用的一些代码。

var loadimage = foreground_list[i].imagelocation + foreground_list[i].image;

var loader:SWFLoader = new SWFLoader(loadimage,{container:tn_mc,x:current_tn_x,y:current_tn_y,name:current_name,alpha:1,width:current_tn_w,height:current_tn_h,rotation:0});

loader.load();
tn_mc.buttonMode = true;

tn_mc.addEventListener( MouseEvent.MOUSE_DOWN, tn_down );
tn_mc.addEventListener( MouseEvent.MOUSE_UP, tn_up );

addChild( tn_mc );


function tn_down(e:MouseEvent):void
{
    switch (MovieClip(this.root).PointerTool)
    {
        case "move" :

            stage.addEventListener(MouseEvent.MOUSE_UP, stage_up );

            e.target.startDrag();
            break;
        case "play" :

            var loader4:SWFLoader = new SWFLoader(foreground_list.imagelocation + foreground_list.playimage,{container:tn_mc,name:e.target.name,x:foreground_list.setx,y:foreground_list.sety,width:foreground_list.setw,height:foreground_list.seth,rotation:0});
            tn_mc.removeChildAt(0);
            tn_mc.addEventListener( MouseEvent.MOUSE_DOWN, tn_down );
            tn_mc.addEventListener( MouseEvent.MOUSE_UP, tn_up );

            loader4.load();
            loader4.addEventListener(Event.COMPLETE, completeactionHandler);
            break;

        default :
            //Some other animation
            break;
    }
}
4

1 回答 1

1

创建一个movieclip - 每个swf 内的一个矢量形状,与您的可点击区域的形状相同。将矢量填充颜色的 alpha 设置为 0%。给它和诸如activeArea之类的实例名称,并将您的事件侦听器分配给它而不是外壳 moveiclip。

另一种可能有效的方法是在MOUSE_DOWN事件上使用hitTestObject(),这将允许您选择忽略透明度。

编辑

很难在没有看到的情况下准确地说出你想要做什么。我实际上并没有编译它,所以我不确定它是否会按原样工作,但理论上它应该很接近。它与您使用的方法略有不同。我使用 Loader() 而不是 SWFLoader,并稍微清理了这个想法。作为旁注,您应该避免在 as3中使用root 。

var _swfLoader:Loader;

var loadimage = foreground_list[i].imagelocation + foreground_list[i].image;

var loader:SWFLoader = new SWFLoader(loadimage,{container:tn_mc,x:current_tn_x,y:current_tn_y,name:current_name,alpha:1,width:current_tn_w,height:current_tn_h,rotation:0});

loader.load();
tn_mc.buttonMode = true;

tn_mc.addEventListener( MouseEvent.MOUSE_DOWN, tn_down );

addChild( tn_mc );

function tn_down(e:MouseEvent):void
{
    tn_mc.addEventListener( MouseEvent.MOUSE_UP, tn_up );

    switch (MovieClip(this.root).PointerTool)
    {
        case "move" :

            stage.addEventListener(MouseEvent.MOUSE_UP, stage_up );
            e.target.startDrag();

        break;

        case "play" :

            _swfLoader = new Loader();
            var req:URLRequest = new URLRequest(foreground_list.imagelocation + foreground_list.playimage);
            _swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, animationLoaded);

            _swfLoader.load(req);

        break;

        default :

            //Some other animation

        break;
    }
}

function tn_up(e:MouseEvent):void
{
    tn_mc.removeEventListener( MouseEvent.MOUSE_UP, tn_up );
}

function animationLoaded(evt:Event):void
{
    _swfLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, animationLoaded);

    tn_mc.removeChildAt(0);

    var loadedSwf = evt.target.content;
    loadedSwf.x = foreground_list.setx;
    loadedSwf.y = foreground_list.sety;
    loadedSwf.width = foreground_list.setw;
    loadedSwf.height = foreground_list.seth;
    loadedSwf.rotation = 0;

    loadedSwf.addEventListener(MouseEvent.MOUSE_DOWN, onAnimationStart);

    // might wanna add theses to an array to keep track of them and run clean up later on

    // now add to some display list
}

function onAnimationStart(evt:MouseEvent):void
{
    loadedSwf.addEventListener(MouseEvent.MOUSE_UP, onAnimationStop);

    // play your animation or whatever else
    evt.target.play();
}

function onAnimationStop(evt:MouseEvent):void
{
    loadedSwf.removeEventListener(MouseEvent.MOUSE_UP, onAnimationStop);

    // stop your animation or whatever else
    evt.target.stop();
}
于 2011-03-24T18:07:29.817 回答