Actionscript 3将多个对象拖放到目标并完成

时间:2017-04-20 15:03:11

标签: actionscript-3 flash actionscript

拖放有效,但是,我不知道如何创建一个if语句,当所有动画片段都放在目标上时,该语句将转到下一个场景。

我尝试将实例名称放在带有hittestobject的if语句中,但是没有运气。

import flash.events.TouchEvent;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.display.MovieClip;

/* Touch and Drag Event
Allows the object to be moved by holding and dragging the object.
*/
var objectoriginalX:Number;
var objectoriginalY:Number;
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;

var lemons:Array = [lemon1_mc, lemon2_mc, lemon3_mc, lemon4_mc, lemon5_mc];

for each(var lemonMC:MovieClip in lemons)
{
    lemonMC.buttonMode = true;
    lemonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject);
    lemonMC.addEventListener(TouchEvent.TOUCH_END, dropobject);

    lemonMC.startX = lemonMC.x;
    lemonMC.startY = lemonMC.y;    
}
var fl_DragBounds:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);    

function pickobject(event:TouchEvent):void
{
    event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds);
    event.target.parent.addChild(event.target);
    objectoriginalX = event.target.x; 
    objectoriginalY = event.target.y;       
}

function dropobject(event:TouchEvent):void
{
    if(event.target.hitTestObject(target_mc)){
        event.target.buttonMode = false;
        event.target.x = target_mc.x;
        event.target.y = target_mc.y;
        event.target.visible = false;           
    } else {
        event.target.x = event.target.startX;
        event.target.y = event.target.startY;
        event.target.buttonMode = true;
    }   
}

var melons:Array = [melon1_mc, melon2_mc, melon3_mc, melon4_mc, melon5_mc, melon6_mc, melon7_mc];
for each(var melonMC:MovieClip in melons)
{
    melonMC.buttonMode = true;
    melonMC.addEventListener(TouchEvent.TOUCH_BEGIN, pickobject2);
    melonMC.addEventListener(TouchEvent.TOUCH_END, dropobject2);

    melonMC.startX = melonMC.x;
    melonMC.startY = melonMC.y;    
}
var fl_DragBounds2:Rectangle = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);

function pickobject2(event:TouchEvent):void
{
    event.target.startTouchDrag(event.touchPointID, false, fl_DragBounds2);
    event.target.parent.addChild(event.target);
    objectoriginalX = event.target.x; 
    objectoriginalY = event.target.y;       
}

function dropobject2(event:TouchEvent):void
{
    if(event.target.hitTestObject(target_null)){
        event.target.buttonMode = false;
        event.target.x = target_mc.x;
        event.target.y = target_mc.y;
        event.target.visible = false;           
    } else {
        event.target.x = event.target.startX;
        event.target.y = event.target.startY;
        event.target.buttonMode = true;
    }
}

2 个答案:

答案 0 :(得分:0)

如何添加一个等于要拖动的对象数量的计数器,然后每次丢弃对象(并检测它是否在目标上)时,您会减少计数器,并在函数结束时检查它是否为&#39 ; s 0?

答案 1 :(得分:0)

一种简单的方法是在通过命中测试时从阵列中移除柠檬/甜瓜。然后检查每个数组是否为空,如果是这种情况,则继续查看下一个场景。

您实际上可以减少冗余代码并对柠檬和甜瓜使用相同的功能(dropobject)。

function dropobject(event:TouchEvent):void {
   //Figure out which array this belongs to (is it a lemon or a melon)
   var array:Array; //the array the dropped item belongs to
   var hitMC:MovieClip; //the hit object for the lemon or melon

   if(lemons.indexOf(event.currentTarget) > -1){ //if the lemon array contains the currentTarget
       array = lemons;
       hitMC = target_mc;
   }else{
       array = melons;
       hitMC = target_null; 
   }

   if(event.currentTarget.hitTestObject(hitMC)){
        event.currentTarget.buttonMode = false;
        event.currentTarget.x = hitMC.x;
        event.currentTarget.y = hitMC.y;
        event.currentTarget.visible = false;

        //remove the item from it's array
        array.removeAt(array.indexOf(event.currentTarget));

        //check if there are any items left
        if(lemons.length < 1 && melons.length < 1){
            //both arrays are empty, so move on
            play(); //or however you want to move on
        }
   }
}

获得更高级,更好的方法是为你的柠檬,瓜和你想要在未来拖动的任何其他东西制作一个基类。然后,您可以将拖动功能添加到该基类中,并为命中目标添加属性,并为其命中目标时添加事件。这将为您提供一个代码库,可以轻松应用于任何库对象。