0

我正在尝试对一个在地图中移动的角色(perso)进行编程,生成一个数组。我的变量“t”调用了我的“Tuiles”MovieClip,创建了一个网格(格栅),在我的“地图”数组上每个“1”都有新的瓷砖。我首先用一个瓦片子修复了碰撞函数,所以,我创建了“地图”和“网格”函数。我需要“t”变量来创建世界和角色碰撞。如果我将变量放在我的函数“creeDecor”上,Flash 会生成它,但说我的碰撞函数不存在变量“t”。如果我将“t”变量作为一般变量放在我的代码顶部,Flash 会在舞台右下角生成一个平铺,碰撞功能运行良好......

我怎么能把它们混合两次?可能吗 ?

我让你我的整个代码。如果您希望它工作,您只需创建一个 MovieClip 类名称“Tuiles”(正方形:32*32)和第二个 MC“Perso”。

//Creation of perso

var perso:Perso = new Perso();

var grille:MovieClip = new MovieClip();

var t:Tuiles = new Tuiles();

var T:int = 32;



var map:Array = [

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],

                 ]



// temporary stockage

var stock:Array = [

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],

                 [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

                 ]



creeDecor()                                                                        

initHero();




stage.addEventListener(KeyboardEvent.KEY_DOWN, clavierDown);

stage.addEventListener(KeyboardEvent.KEY_UP, clavierUp);

stage.addEventListener(Event.ENTER_FRAME, animation);



// Stage collision

var minX = 0;

var minY = 0;

var maxX = stage.stageWidth - perso.width;

var maxY = stage.stageHeight - perso.height;

var speedHero = 5;



function clavierDown(e)

{

    switch(e.keyCode)

    {  

        case 37:

            perso.speedX = -speedHero;

            break;

        case 39:

            perso.speedX = speedHero;

            break;

        case 38:

            perso.speedY = -speedHero;

            break;

        case 40:

            perso.speedY = speedHero;

            break;

}

}



function clavierUp(e)

{

    switch(e.keyCode)

    {

        case 37:

            perso.speedX = 0;

            break; 



        case 39:

            perso.speedX = 0;

            break;



        case 38:

            perso.speedY = 0;

            break;



            case 40:

            perso.speedY = 0;

            break;

    }

}



function animation(e)

{

    animeHero();

}

   // Stage border collision function

function animeHero()

{       

       if(perso.x + perso.speedX >= minX && perso.x + perso.speedX <= maxX)

   {

       perso.x += perso.speedX;

   } else if (perso.speedX < 0)

   {

       perso.x -= Math.abs(perso.x-minX);

               //trace(Math.abs(perso.x-minX));

   } else {

       perso.x += Math.abs(perso.x-maxX) ;

   }



   if(perso.y + perso.speedY >= minY && perso.y + perso.speedY <= maxY)

   {

       perso.y += perso.speedY;

   }

  collision();

}



function initHero()

{

    perso.speedX = 0;

    perso.speedY = 0;



    perso.x = 200;

    perso.y = 300;


    addChild(perso);

}



function collision() {



    if (perso.hitTestObject(t))

    {

        if (perso.x >= t.x + t.width - speedHero && perso.x <= t.x +t.width + 2)

        {

            perso.x = t.x + t.width;

        }



        if (perso.x + perso.width >= t.x - 2 && perso.x + perso.width <= t.x + speedHero)

        {

            perso.x = t.x - perso.width;

        }

        if (perso.y >= t.y + t.height - speedHero && perso.y <= t.y +t.height + 2)

        {

            perso.y = t.y + t.height;

        }



        if (perso.y + perso.height >= t.y - 2 && perso.y + perso.height <= t.y + speedHero)

        {

            perso.y = t.y - perso.height;

        }

    }

}



// level creation

function creeDecor():void{

    for (var i:int=0; i<20; i++){                                // boucle sur les 20 colonnes

        for (var j:int=0; j<15; j++){                            // boucle sur les 15 lignes de chaque colonne

            var f:int = map[j][i] 



            if(f>0)  {     
                t.x= i*T;  

                t.y= j*T;  

                t.gotoAndStop(f)             

                grille.addChild(t)         


                // Specialisation

                if (f==1)

                stock[j][i] = t;

            }



            else

            {

                stock[j][i] = []                               

            }

        }

    }

}

addChild(grille)    
4

1 回答 1

1

你想做很多不同Tuiles的事情,并检查每个人的碰撞。听起来您最初是在制作新TuilescreeDecor(),但除非您将新瓷砖存储在此之外,否则只creeDecor()知道它们。(这就是为什么 Flasht说不存在于collision().)这个关于函数范围的页面可能会有所帮助,如果您还没有阅读该文档的其余部分,那么它可能会有所帮助。

因此,您可能会Tuiles在顶层准备所有您的列表,例如:

var tList:Vector.<Tuiles> = new Vector.<Tuiles>();

这意味着两者都creeDecor()可以collision()看到tList。然后就像你以前做的那样,每次你需要一个新Tuiles的,并将它们添加到列表中creeDecor()

if(f>0)  {
    var newT:Tuiles = new Tuiles();
    tList.push(newT);
    // Set up newT's position etc...

然后,您可以使用“for each... in”为每个Tuilesin运行命中测试tList

for each (var t:Tuiles in tList) {
    if (perso.hitTestObject(t))
        // Your collision code...
    }
}

如果你只有简单的方块,那么进行碰撞检测的方法可能比使用hitTestObject多次更快,因此如果速度较慢,调整它可能是一个很好的下一步。

于 2013-02-02T14:37:22.027 回答