首页 > 解决方案 > 如何同时hittestObject多个影片剪辑

问题描述

我有一个简单的拖放到目标系统,它工作正常,除了我没有人能够拖放到已经有电影剪辑的地方。决定在目标上 hittestObject 以检查那里是否有影片剪辑。如果有,它只是将对象发送回其原始位置。但是,当我尝试为多个对象编写相同的代码时,它仅适用于其中一个对象。

我尝试将我的对象放在一个数组中并将它们放在一个变量中,但是当我需要同时hitestobject多个对象时,我只能在数组或变量中hitestobject一个对象。请帮忙

    if (char2.hitTestObject(target0))
    {
        slot0=true;
    }

    if (!char2.hitTestObject(target0))
    {
        slot0=false;
    }

        if (char2.hitTestObject(target1))
    {
        slot1=true;
    }

    if (!char2.hitTestObject(target1))
    {
        slot1=false;
    }

        if (char2.hitTestObject(target2))
    {
        slot2=true;
    }

    if (!char2.hitTestObject(target2))
    {
        slot2=false;
    }

我还需要在 char1 和 char0 上点击测试对象。此代码有效,但是如果我尝试简单地复制代码并将 char2 替换为 char1,则只有一个其他代码块可以工作,请帮助。也是另一种选择

if (char2 || char1.hitTestObject(target0))
{
    slot0=true;
}

会有帮助的。请帮助告诉我如何做到这一点,或者让我知道它是否可能。谢谢你!!

完整代码:

var slot0:Boolean;
var slot1:Boolean;
var slot2:Boolean;

addEventListener(Event.ENTER_FRAME, fl_EnterFrameHandler);

function fl_EnterFrameHandler(et:Event):void
{
trace(slot0);
}
// Unlike the Object class, that allows String keys only
// the Dictionary class allows you to store and
// access data by the object instance.
var theValids:Dictionary = new Dictionary;
var theCharacters:Dictionary = new Dictionary;
// We'll store the original (x,y) coordinates here.
var theOrigin:Point = new Point;
// The Sprite class is the superclass of MovieClip, furthermore,
// the startDrag method defined for Sprite class, so unless you
// create your own dragging code, you are bound to use Sprites,
// while you cannot drag SimpleButtons and TextFields this way.
// We'll store the current dragged object here.
var theObject:Sprite;
var theChar:Sprite;

// This first argument is the object you want to be draggable.
// The "...targets:Array" means you can call this method with
// any number of arguments, the first one is mandatory, the
// rest will be passed in a form of Array (empty Array if you
// call this method with a single argument).
function setupDraggable(source:Sprite, ...targets:Array):void
{
    // Make the object draggable.
    source.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
    source.mouseChildren = false;
    source.mouseEnabled = true;
    source.buttonMode = true;

    // Keep the list of the object's targets so it can be
    // retrieved later by the key of the object itself.
    theValids[source] = targets;
}
// Ok, let's setup the objects and link them to their designated
// targets. The whole point of the rest of the code is to make
// this one part as simple as it possible: you just edit
// these lines to tell which one objects go where.
// This object can be dropped to a single target.
// setupDraggable(obj_1 , target1);

// These objects can go to two targets each.
// setupDraggable(obj_10, target1, target10);
// setupDraggable(obj_2 , target2, target20);
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot1);
function setslot1(e:MouseEvent):void{
    if (character1.hitTestObject(target0))
        {
            slot0=true;
        }
        if (!character1.hitTestObject(target0))
        {
            slot0=false;
        }
            if (character1.hitTestObject(target1))
        {
            slot1=true;
        }
        if (!character1.hitTestObject(target1))
        {
            slot1=false;
        }
            if (character1.hitTestObject(target2))
        {
            slot2=true;
        }
        if (!character1.hitTestObject(target2))
        {
            slot2=false;
        }
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot2);
function setslot2(e:MouseEvent):void{
    if (character0.hitTestObject(target0))
        {
            slot0=true;
        }
        if (!character0.hitTestObject(target0))
        {
            slot0=false;
        }
            if (character0.hitTestObject(target1))
        {
            slot1=true;
        }
        if (!character0.hitTestObject(target1))
        {
            slot1=false;
        }
            if (character0.hitTestObject(target2))
        {
            slot2=true;
        }
        if (!character0.hitTestObject(target2))
        {
            slot2=false;
        }
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, setslot3);
function setslot3(e:MouseEvent):void{
    if (char2.hitTestObject(target0))
        {
            slot0=true;
        }
        if (!char2.hitTestObject(target0))
        {
            slot0=false;
        }
            if (char2.hitTestObject(target1))
        {
            slot1=true;
        }
        if (!char2.hitTestObject(target1))
        {
            slot1=false;
        }
            if (char2.hitTestObject(target2))
        {
            slot2=true;
        }
        if (!char2.hitTestObject(target2))
        {
            slot2=false;
        }
}
// This one object can be dropped to any of targets.
setupDraggable(character0, target0, target1, target2, redslot);
setupDraggable(character1, target0, target1, target2, greenslot);
setupDraggable(char2, target0, target1, target2, blueslot);
// The MOUSE_DOWN event handler.

function onDown(e:MouseEvent):void
{       
        if (char2.hitTestObject(target0))
        {
            slot0=true;
        }
        if (!char2.hitTestObject(target0))
        {
            slot0=false;
        }
            if (char2.hitTestObject(target1))
        {
            slot1=true;
        }
        if (!char2.hitTestObject(target1))
        {
            slot1=false;
        }
            if (char2.hitTestObject(target2))
        {
            slot2=true;
        }
        if (!char2.hitTestObject(target2))
        {
            slot2=false;
        }

// Clean-up. Remove the reference, the object is no longer
    // being dragged, so you won't need to keep it.
    //theObject = null;

    // Get the reference to the object under the mouse.
    theObject = e.currentTarget as Sprite;

    // Keep the object's initial position.
    theOrigin.x = theObject.x;
    theOrigin.y = theObject.y;

    // Put the dragged object on top of anything else.
    // We are operating in the parent context of all these
    // objects here so there's no need to address anObj.parent.
    setChildIndex(theObject, numChildren - 1);

    // Start dragging.
    theObject.startDrag(true);

    // Listen to the MOUSE_UP event, which could happen offstage
    // and out of the dragged object, so the only reliable
    // way is to listen it from the Stage. That's why we
    // are keeping theObject reference as an additional
    // variable, without relying on event's data.
    stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}

// The MOUSE_UP event handler.
function onUp(e:MouseEvent):void
{   

    // Unsubscribe the MOUSE_UP event handler.
    //stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);

    // Stop the dragging process.
    theObject.stopDrag();

    // Let's assume there could be more than a single collision.
    // We need to figure the one target that is closest.
    var theTarget:DisplayObject;
    var theDistance:int = 100000;

    // Store the dragged object position so we can
    // measure distances to the valid collisions, if any.
    var thePlace:Point = theObject.localToGlobal(new Point);


    // Now, the magic. Lets browse through the
    // valid targets and see if there's a collision.
    for each (var aTarget:DisplayObject in theValids[theObject])
    {

        if (theObject.hitTestObject(aTarget))
        {

            // Let's see if the current collision is closer
            // to the dragged object, than the previous one
            // (if any, that's what initial 100000 for).
            var aPlace:Point = aTarget.localToGlobal(new Point);
            var aDistance:int = Point.distance(aPlace, thePlace);

            if (aDistance < theDistance)
            {
                theTarget = aTarget;
                theDistance = aDistance;
            }
        }
    }
    // If there's at least one collision,
    // this variable will not be empty.

   if (theTarget)
    {
                // Make the object non-interactive.
        // theObject.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);
        // theObject.mouseEnabled = false;
        // theObject.buttonMode = false;
        // Glue the dragged object to the center of the target.
        theObject.x = theTarget.x;
        theObject.y = theTarget.y;
    }

    else
    {
        // If we're here, that means there was no valid collisions,
        // lets return the object to its designated place.
        theObject.x = theOrigin.x;
        theObject.y = theOrigin.y;
    }
    if(slot0==true){

    if (theObject.hitTestObject(target0))
        {
        theObject.x = theOrigin.x;
        theObject.y = theOrigin.y;
        }
}
if(slot1==true){

    if (theObject.hitTestObject(target1))
        {
        theObject.x = theOrigin.x;
        theObject.y = theOrigin.y;
        }
}
if(slot2==true){

    if (theObject.hitTestObject(target2))
        {
        theObject.x = theOrigin.x;
        theObject.y = theOrigin.y;
        }
}
// Clean-up. Remove the reference, the object is no longer
    // being dragged, so you won't need to keep it.
    //theObject = null;
}

只有setslot3 工作setslot 1&2 不起作用,每当我放置多个此特定代码时都会发生这种情况。

标签: actionscript-3flash-cs5

解决方案


推荐阅读