简单的益智游戏结束需要AS3

时间:2014-05-26 19:32:15

标签: actionscript-3 procedure puzzle

我是新来的,是AS3和Flash世界的新手。首先,抱歉'我的英语不好。 :/

我正试图让游戏结束,也就是说,当玩家在矩阵中对拼图的所有部分进行排序时,我想显示文本“Game Over”。

如何解决这个难题有几种选择,所以我需要制作某种功能(我认为),但我不知道如何。

Here is the printscreen of puzzle game.

plavo,ljubicasto,zuto,crveno,narandzasto和zeleno是对象的名称(拼图)

Here is code:

stop();


var zutoStartX:Number = 210;
var zutoStartY:Number = 630;
var crvenoStartX:Number = 640;
var crvenoStartY:Number = 210;
var plavoStartX:Number = 430;
var plavoStartY:Number = 675;
var narandzastoStartX:Number = 890;
var narandzastoStartY:Number = 170;
var zelenoStartX:Number = 860;
var zelenoStartY:Number = 400;
var ljubicastoStartX:Number = 640;
var ljubicastoStartY:Number = 502;


var bojeArr:Array = new Array (plavo, zuto, crveno,
 zeleno, ljubicasto, narandzasto);
for (var i:uint =0; i < bojeArr.length; i++)
{
    bojeArr[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
    bojeArr[i].addEventListener(MouseEvent.MOUSE_UP, drop);
}
function drag(event:MouseEvent):void
{
    event.currentTarget.startDrag();
}
function drop(e:MouseEvent):void
{
    stopDrag();

     //testiranje i vracanje na mesto ukoliko dodje do preklapanja
    if (crveno.hitCrv.hitTestObject(plavo.hitPl))
    {


        plavo.x = plavo.x;
        plavo.y = plavo.y;
        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
    }
    else if (crveno.hitCrv.hitTestObject(zuto.hitZt))
    {


        zuto.x = zuto.x;
        zuto.y = zuto.y;
        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
    }
    else if (crveno.hitCrv.hitTestObject(zeleno.hitZl))
    {


        zeleno.x = zeleno.x;
        zeleno.y = zeleno.y;
        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
    }
    else if (crveno.hitCrv.hitTestObject(ljubicasto.hitLjub))
    {


        ljubicasto.x = ljubicasto.x;
        ljubicasto.y = ljubicasto.y;
        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
    }
    else if (crveno.hitCrv.hitTestObject(narandzasto.hitNar))
    {


        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
        narandzasto.x = narandzasto.x;
        narandzasto.y = narandzasto.y;


    }
    else if (plavo.hitPl.hitTestObject(crveno.hitCrv))
    {


        crveno.x = crveno.x;
        crveno.y = crveno.y;
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;


    }
    else if (plavo.hitPl.hitTestObject(zuto.hitZt))
    {


        zuto.x = zuto.x;
        zuto.y = zuto.y;
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;


    }
    else if (plavo.hitPl.hitTestObject(zeleno.hitZl))
    {


        zeleno.x = zeleno.x;
        zeleno.y = zeleno.y;
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;


    }
    else if (plavo.hitPl.hitTestObject(ljubicasto.hitLjub))
    {


        ljubicasto.x = ljubicasto.x;
        ljubicasto.y = ljubicasto.y;
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;


    }
    else if (plavo.hitPl.hitTestObject(narandzasto.hitNar))
    {


        narandzasto.x = narandzasto.x;
        narandzasto.y = narandzasto.y;
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;


    }
    else if (zuto.hitZt.hitTestObject(crveno.hitCrv))
    {


        crveno.x = crveno.x;
        crveno.y = crveno.y;
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;


    }
    else if (zuto.hitZt.hitTestObject(plavo.hitPl))
    {


        plavo.x = plavo.x;
        plavo.y = plavo.y;
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;


    }
    else if (zuto.hitZt.hitTestObject(zeleno.hitZl))
    {


        zeleno.x = zeleno.x;
        zeleno.y = zeleno.y;
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;


    }
    else if (zuto.hitZt.hitTestObject(ljubicasto.hitLjub))
    {


        ljubicasto.x = ljubicasto.x;
        ljubicasto.y = ljubicasto.y;
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;


    }
    else if (zuto.hitZt.hitTestObject(narandzasto.hitNar))
    {


        narandzasto.x = narandzasto.x;
        narandzasto.y = narandzasto.y;
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;


    }
    else if (zeleno.hitZl.hitTestObject(crveno.hitCrv))
    {


        crveno.x = crveno.x;
        crveno.y = crveno.y;
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;


    }
    else if (zeleno.hitZl.hitTestObject(plavo.hitPl))
    {


        plavo.x = plavo.x;
        plavo.y = plavo.y;
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;


    }
    else if (zeleno.hitZl.hitTestObject(zuto.hitZt))
    {


        zuto.x = zuto.x;
        zuto.y = zuto.y;
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;


    }
    else if (zeleno.hitZl.hitTestObject(ljubicasto.hitLjub))
    {


        ljubicasto.x = ljubicasto.x;
        ljubicasto.y = ljubicasto.y;
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;


    }
    else if (zeleno.hitZl.hitTestObject(narandzasto.hitNar))
    {


        narandzasto.x = narandzasto.x;
        narandzasto.y = narandzasto.y;
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;


    }






      //smestanje zutog dela


    if (zuto.zt1_bt.hitTestObject(tar1))
    {
        zuto.x = 150;
        zuto.y = 200;
    }
    else if (zuto.zt1_bt.hitTestObject(tar2))
    {
        zuto.x = 250;
        zuto.y = 200;
    }
    else if (zuto.zt1_bt.hitTestObject(tar3))
    {
        zuto.x = 350;
        zuto.y = 200;
    }
    else if (zuto.zt1_bt.hitTestObject(tar4))
    {
        zuto.x = 450;
        zuto.y = 200;
    }
    else if (zuto.zt1_bt.hitTestObject(tar5))
    {
        zuto.x = 150;
        zuto.y = 300;
    }
    else if (zuto.zt1_bt.hitTestObject(tar6))
    {
        zuto.x = 250;
        zuto.y = 300;
    }
    else if (zuto.zt1_bt.hitTestObject(tar7))
    {
        zuto.x = 350;
        zuto.y = 300;
    }
    else if (zuto.zt1_bt.hitTestObject(tar8))
    {
        zuto.x = 450;
        zuto.y = 300;
    }
    else if (zuto.zt1_bt.hitTestObject(tar9))
    {
        zuto.x = 150;
        zuto.y = 400;
    }
    else if (zuto.zt1_bt.hitTestObject(tar10))
    {
        zuto.x = 250;
        zuto.y = 400;
    }
    else if (zuto.zt1_bt.hitTestObject(tar11))
    {
        zuto.x = 350;
        zuto.y = 400;
    }
    else if (zuto.zt1_bt.hitTestObject(tar12))
    {
        zuto.x = 450;
        zuto.y = 400;
    }
    else
    {
        zuto.x = zutoStartX;
        zuto.y = zutoStartY;
    }

//smestanje narandzastog dela


    if (narandzasto.nar1_bt.hitTestObject(tar1))
    {
        zuto.x = 150;
        zuto.y = 200;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar2))
    {
        narandzasto.x = 250;
        narandzasto.y = 200;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar3))
    {
        narandzasto.x = 350;
        narandzasto.y = 200;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar4))
    {
        narandzasto.x = 450;
        narandzasto.y = 200;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar5))
    {
        narandzasto.x = 150;
        narandzasto.y = 300;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar6))
    {
        narandzasto.x = 250;
        narandzasto.y = 300;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar7))
    {
        narandzasto.x = 350;
        narandzasto.y = 300;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar8))
    {
        narandzasto.x = 450;
        narandzasto.y = 300;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar9))
    {
        narandzasto.x = 150;
        narandzasto.y = 400;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar10))
    {
        narandzasto.x = 250;
        narandzasto.y = 400;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar11))
    {
        narandzasto.x = 350;
        narandzasto.y = 400;
    }
    else if (narandzasto.nar1_bt.hitTestObject(tar12))
    {
        narandzasto.x = 450;
        narandzasto.y = 400;
    }
    else
    {
        narandzasto.x = narandzastoStartX;
        narandzasto.y = narandzastoStartY;
    }

//smestanje crvenog dela


    if (crveno.crv1_bt.hitTestObject(tar1))
    {
        crveno.x = 200;
        crveno.y = 200;
    }
    else if (crveno.crv1_bt.hitTestObject(tar2))
    {
        crveno.x = 300;
        crveno.y = 200;
    }
    else if (crveno.crv1_bt.hitTestObject(tar3))
    {
        crveno.x = 400;
        crveno.y = 200;
    }
    else if (crveno.crv1_bt.hitTestObject(tar5))
    {
        crveno.x = 200;
        crveno.y = 300;
    }
    else if (crveno.crv1_bt.hitTestObject(tar6))
    {
        crveno.x = 300;
        crveno.y = 300;
    }
    else if (crveno.crv1_bt.hitTestObject(tar7))
    {
        crveno.x = 400;
        crveno.y = 300;
    }
    else if (crveno.crv1_bt.hitTestObject(tar9))
    {
        crveno.x = 200;
        crveno.y = 400;
    }
    else if (crveno.crv1_bt.hitTestObject(tar10))
    {
        crveno.x = 300;
        crveno.y = 400;
    }
    else if (crveno.crv1_bt.hitTestObject(tar11))
    {
        crveno.x = 400;
        crveno.y = 400;
    }
    else
    {
        crveno.x = crvenoStartX;
        crveno.y = crvenoStartY;
    }

//smestanje plavog dela


    if (plavo.pl1_bt.hitTestObject(tar1))
    {
        plavo.x = 200;
        plavo.y = 200;
    }
    else if (plavo.pl1_bt.hitTestObject(tar2))
    {
        plavo.x = 300;
        plavo.y = 200;
    }
    else if (plavo.pl1_bt.hitTestObject(tar3))
    {
        plavo.x = 400;
        plavo.y = 200;
    }
    else if (plavo.pl1_bt.hitTestObject(tar5))
    {
        plavo.x = 200;
        plavo.y = 300;
    }
    else if (plavo.pl1_bt.hitTestObject(tar6))
    {
        plavo.x = 300;
        plavo.y = 300;
    }
    else if (plavo.pl1_bt.hitTestObject(tar7))
    {
        plavo.x = 400;
        plavo.y = 300;
    }
    else if (plavo.pl1_bt.hitTestObject(tar9))
    {
        plavo.x = 200;
        plavo.y = 400;
    }
    else if (plavo.pl1_bt.hitTestObject(tar10))
    {
        plavo.x = 300;
        plavo.y = 400;
    }
    else if (plavo.pl1_bt.hitTestObject(tar11))
    {
        plavo.x = 400;
        plavo.y = 400;
    }
    else
    {
        plavo.x = plavoStartX;
        plavo.y = plavoStartY;
    }

      //smestanje zelenog dela


    if (zeleno.zl1_bt.hitTestObject(tar1))
    {
        zeleno.x = 200;
        zeleno.y = 150;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar2))
    {
        zeleno.x = 300;
        zeleno.y = 150;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar3))
    {
        zeleno.x = 400;
        zeleno.y = 150;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar5))
    {
        zeleno.x = 200;
        zeleno.y = 250;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar6))
    {
        zeleno.x = 300;
        zeleno.y = 250;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar7))
    {
        zeleno.x = 400;
        zeleno.y = 250;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar9))
    {
        zeleno.x = 200;
        zeleno.y = 350;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar10))
    {
        zeleno.x = 300;
        zeleno.y = 350;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar11))
    {
        zeleno.x = 400;
        zeleno.y = 350;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar13))
    {
        zeleno.x = 200;
        zeleno.y = 450;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar14))
    {
        zeleno.x = 300;
        zeleno.y = 450;
    }
    else if (zeleno.zl1_bt.hitTestObject(tar15))
    {
        zeleno.x = 400;
        zeleno.y = 450;
    }
    else
    {
        zeleno.x = zelenoStartX;
        zeleno.y = zelenoStartY;
    }

//smestanje ljubicastog dela


    if (ljubicasto.ljub1_bt.hitTestObject(tar1))
    {
        ljubicasto.x = 200;
        ljubicasto.y = 150;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar2))
    {
        ljubicasto.x = 300;
        ljubicasto.y = 150;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar3))
    {
        ljubicasto.x = 400;
        ljubicasto.y = 150;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar5))
    {
        ljubicasto.x = 200;
        ljubicasto.y = 250;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar6))
    {
        ljubicasto.x = 300;
        ljubicasto.y = 250;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar7))
    {
        ljubicasto.x = 400;
        ljubicasto.y = 250;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar9))
    {
        ljubicasto.x = 200;
        ljubicasto.y = 350;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar10))
    {
        ljubicasto.x = 300;
        ljubicasto.y = 350;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar11))
    {
        ljubicasto.x = 400;
        ljubicasto.y = 350;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar13))
    {
        ljubicasto.x = 200;
        ljubicasto.y = 450;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar14))
    {
        ljubicasto.x = 300;
        ljubicasto.y = 450;
    }
    else if (ljubicasto.ljub1_bt.hitTestObject(tar15))
    {
        ljubicasto.x = 400;
        ljubicasto.y = 450;
    }
    else
    {
        ljubicasto.x = ljubicastoStartX;
        ljubicasto.y = ljubicastoStartY;
    }
}//restart dugme
resButton.addEventListener(MouseEvent.CLICK, reset);
function reset(event:MouseEvent):void
{
    zuto.x = zutoStartX;
    zuto.y = zutoStartY;
    plavo.x = plavoStartX;
    plavo.y = plavoStartY;
    crveno.x = crvenoStartX;
    crveno.y = crvenoStartY;
    narandzasto.x = narandzastoStartX;
    narandzasto.y = narandzastoStartY;
    zeleno.x = zelenoStartX;
    zeleno.y = zelenoStartY;
    ljubicasto.x = ljubicastoStartX;
    ljubicasto.y = ljubicastoStartY;



}

1 个答案:

答案 0 :(得分:0)

这里有很多代码重复,可以转换成函数(如果你做了两次,你也可以做任何次数)。

你也有很多不必要的代码,如:

narandzasto.x = narandzasto.x;  
narandzasto.y = narandzasto.y;

您要将变量设置为当前值。

现在,至于解决方案,我认为最简单的答案是跟踪拼图块与网格的碰撞测试,另外我认为你必须跟踪拼图块与拼图块碰撞以检测任何重叠,当所有部件都安装到网格上并且没有重叠时,你应该有一个完整的解决方案。