簡體   English   中英

如何使用(調用)函數(AS3)中的變量?

[英]How to use (call) a variable from a function (AS3)?

我正在嘗試對在地圖中移動的帶有Array的角色(perso)進行編程。 我的變量“ t”調用了我的“ Tuiles” MovieClip,並在我的“地圖”數組上創建了一個網格(網格),每個網格塊均帶有“ 1”。 首先,我使用一個貼圖Child修復了碰撞功能,因此,我創建了“地圖”和“網格”功能。 我需要“ 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)    

您想制作許多不同的Tuiles ,並檢查與其中每一個的碰撞。 聽起來您最初是在creeDecor()中制作新的Tuiles ,但是除非您在其中存儲新的圖塊,否則只有creeDecor()知道它們。 (這就是為什么Flash說tcollision()不存在的原因。) 關於函數作用域的頁面以及該文檔的其余部分,如果您尚未閱讀,可能會有所幫助。

因此,您可以在頂層准備所有Tuiles的列表,例如:

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

這意味着creeDecor()collision()都可以看到tList 然后像以前一樣,在每次需要時在creeDecor()創建新的Tuiles ,並將其添加到列表中

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

然后,你可以運行你的命中測試每TuilestList使用“為每個...在”

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

如果您只有簡單的正方形,則可能比使用hitTestObject多次hitTestObject更快的碰撞檢測方法,因此如果速度較慢,則進行下一步調整可能是一個不錯的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM