как выполнить тестированиеОбъект нескольких видеоклипов одновременно - PullRequest
0 голосов
/ 05 июня 2019

У меня есть простое перетаскивание в целевую систему, оно отлично работает, за исключением того, что я не могу кого-то, кто мог бы бросить туда, где уже есть мувиклип. решил проверить объект на цели, чтобы проверить, есть ли там клип. если он есть, он просто возвращает объект в исходное положение однако, когда я пытаюсь закодировать одно и то же для более чем одного объекта, он работает только на одном из объектов.

Я пытался поместить свои объекты в массив и поместить их в переменную, однако я могу ударить только по одному объекту в массиве или переменной за один раз, когда мне нужно 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, сетслоты 1 и 2 не работают, и это происходит каждый раз, когда я добавляю более одного этого кода.

...