使movieclip数组彼此不重叠

时间:2017-09-19 08:39:49

标签: actionscript-3 air

我目前正在开发一款带有AS3的游戏,其中玩家需要通过倾斜手机来避免掉落随机物体。这些对象使用延迟计时器使它们每行掉落一次。所以我现在在屏幕上随机出现两个物体的水平上工作,这些物体是类名为planet1的动画片段“水银”和类名为“planet2”的“venus”。问题是,有时它们在坠落时会相互重叠。我希望这两个动画片段在开始下降时不会相互重叠。我曾寻找过脚本并尝试过它,但它不起作用。我是AS3的初学者。

这是脚本:

var mercury:planet1;
var mercuryArray:Array = new Array();

var mercuryClock = new Timer(5000, 0);
mercuryClock.addEventListener(TimerEvent.TIMER, spawnMercury);

var venus:planet2;
var venusArray:Array = new Array();

var venusClock = new Timer(2000, 0);
venusClock.addEventListener(TimerEvent.TIMER, spawnVenus);


addEventListener(Event.ENTER_FRAME, onMyEnterFrame);

function onMyEnterFrame(e:Event)
{
    galawMercury();
    galawVenus();

    checkCollision();
}

/*==================PLANET 1=====================*/

function spawnMercury(e:Event)
{
    gawaMercury();
}

function gawaMercury()
{
    var mercury:planet1 = new planet1();
    mercury.x = Math.random() * 400; 
    mercury.y = 0; 
    addChild(mercury);
    mercuryArray.push(mercury);
}

function galawMercury()
{
    for(var ee:int = mercuryArray.length - 1; ee >= 0; ee--)
    {
        mercuryArray[ee].y += 13;

        if(mercuryArray[ee].y > stage.stageHeight)
        {
            mercuryArray[ee].parent.removeChild(mercuryArray[ee]);
            mercuryArray.splice(ee, 1);
        }
    }
}

/*==================PLANET 2=====================*/

function spawnVenus(e:Event)
{
    gawaVenus(); 
}

function gawaVenus()
{
    var venus:planet2 = new planet2();
    venus.x = Math.random() * 400; 
    venus.y = 0; 
    venus.width = 126;
    venus.height = 200;
    addChild(venus);
    venusArray.push(venus); 
}

function galawVenus()
{
    for(var yy:int = venusArray.length - 1; yy >= 0; yy--)
    {
        venusArray[yy].y += 12;

        if(venusArray[yy].y > stage.stageHeight)
        {
            venusArray[yy].parent.removeChild(venusArray[yy]);
            venusArray.splice(yy, 1);
        }
    }
}

function checkCollision()
{
    for(var oo:int = mercuryArray.length - 1; oo >= 0; oo --)
    {
        if(stella.hitStella.hitTestObject(mercuryArray[oo]))
        {
            keme();
        }
    }

    for(var uu:int = venusArray.length - 1; uu >= 0; uu --)
    {
        if(stella.hitStella.hitTestObject(venusArray[uu]))
        {
            keme();
        }
    }
}

function keme()
{
    soundtrackChannel.stop();
    soundtrackChannel = stellaDead.play();
    removeEventListener(Event.ENTER_FRAME, onMyEnterFrame);
    mercuryClock.stop();
    venusClock.stop();
    score.stop();
    stella.play();
    blah.text = lbl_iskor.text;
    removeObjects();
}

function removeObjects()
{
    for each (var mercury:planet1 in mercuryArray)
    {
        mercuryArray.splice(mercury, 0);
        removeChild(mercury);
    }
    for each (var venus:planet2 in venusArray)
    {
        venusArray.splice(venus, 0);
        removeChild(venus);
    }
}

感谢高级。 :)

0 个答案:

没有答案