如何同时测试多个影片剪辑

时间:2019-06-05 05:25:54

标签: actionscript-3 flash-cs5

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

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

    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上命中testtestobject。此代码有效,但是如果我尝试简单地复制代码并将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可以工作,slotslot 1和2无法工作,并且只要我输入了多个以上特定代码,便会发生这种情况。

0 个答案:

没有答案