0
votes

I'm trying to program a character (perso) who moves in a map generates with an Array. There my variable "t" who call my "Tuiles" MovieClip, creating a grid (grille) with new tile each "1" on my "map" Array. I first fixed collision function with one tile Child, so, I created "map" and "grid" functions. I need "t" variable for the world creat AND the character collision. If I put variable on my function "creeDecor", Flash generates it good but saying me variable "t" doesn't exist for my collision function. If I put "t" variable on top of my code as a general variable, Flash generates one tile on right-bottom of stage with collision function working good...

How could I mix them twice ? Is it possible ?

I let you my entire code. If you want it to work you just have to create a MovieClip class name "Tuiles" (square : 32*32) and a second 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)    
1

1 Answers

1
votes

You want to make lots of different Tuiles and also check collisions with every one of them. It sounds like you were making new Tuiles in creeDecor() originally, but unless you store the new tiles outside of that, only creeDecor() knows about them. (That's why Flash said t didn't exist in collision().) This page about Function scope might help, as well as the rest of that document in general if you haven't read it yet.

So, you might prepare a list of all of your Tuiles at the top level, like:

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

That means that both creeDecor() and collision() can see tList. Then as you did before, make new Tuiles in creeDecor() each time you need one, and add them to the list:

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

Then, you can run your hit test for every Tuiles in tList using "for each... in"

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

If you only have simple squares, there are probably faster ways of doing collision detection than using hitTestObject lots of times, so adjusting that might be a good next step if it's slow.