2014-01-13 143 views
0

嘿傢伙,所以我創建了這個代碼的迷宮,我想hitTest牆壁,但是當我嘗試沿線if(mc.hitTestObject(瓷磚))的東西它不會做任何事情。而且我不知道爲什麼,這裏的代碼:迷宮碰撞檢測與瓷磚AS3

package 
{ 

import flash.display.MovieClip; 
import flash.events.KeyboardEvent; 
import flash.events.Event; 
import flash.display.DisplayObject; 


public class Main extends MovieClip 
{ 


    private var mc:hero = new hero; 
    private var tile:tile1 = new tile1; 
    private var mcSpeed:uint = 5; 

    private var lab1Array:Array = new Array; 
    private const TILE_WIDTH:uint = 25; 
    private const TILE_HEIGHT:uint = 25; 
    private const STAGE_WIDTH:uint = 550; 
    private const STAGE_HEIGHT:uint = 400; 
    private var horizontalBlocks = STAGE_WIDTH/TILE_WIDTH; 
    private var verticalBlocks = STAGE_HEIGHT/TILE_HEIGHT; 
    private var hitBlock:DisplayObject; 


    private var leftDown: Boolean = false; 
    private var rightDown: Boolean = false; 
    private var botDown: Boolean = false; 
    private var upDown: Boolean = false; 



    public function Main() 
    { 


     mc.x = 160; 
     mc.y = 385; 
     addChild(mc); 


     for (var i:uint = 0 ; i < verticalBlocks ; i++) 
     { 
      lab1Array[i] = new Array; 

      for (var j:uint=0; j < horizontalBlocks; j++) 
       { 
       lab1Array[i].push(0); 
       } 


     } 


     lab1Array = [ 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1], 
     [1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1] 
     ]; 



     stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown); 
     stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp); 
     stage.addEventListener(Event.ENTER_FRAME, charManagement); 

    } 


    function checkKeysDown (event:KeyboardEvent):void 
    { 


     if (event.keyCode == 37 || event.keyCode == 65) 
     { 
      leftDown = true; 
     } 
     if (event.keyCode == 38 || event.keyCode == 87) 
     { 
      upDown = true; 
     } 
     if (event.keyCode == 39 || event.keyCode == 68) 
     { 
      rightDown = true; 
     } 
     if (event.keyCode == 40 || event.keyCode == 83) 
     { 
      botDown = true; 
     } 
    } 

    function checkKeysUp (event:KeyboardEvent):void 
    { 
     if (event.keyCode == 37 || event.keyCode == 65) 
     { 
      leftDown = false; 
     } 
     if (event.keyCode == 38 || event.keyCode == 87) 
     { 
      upDown = false; 
     } 
     if (event.keyCode == 39 || event.keyCode == 68) 
     { 
      rightDown = false; 
     } 
     if (event.keyCode == 40 || event.keyCode == 83) 
     { 
      botDown = false; 
     } 
    } 

    function charManagement(event:Event) : void 
    { 


      if (leftDown) 
      { 
       mc.x -= mcSpeed; 
      } 
      if (rightDown) 
      { 
       mc.x += mcSpeed; 
      } 
      if (upDown) 
      { 
       mc.y -= mcSpeed; 
      } 
      if (botDown) 
      { 
       mc.y += mcSpeed; 
      } 

    } 

     function createLab():void 
    { 
     for (var i = 0; i < lab1Array.length ; i++) 
     { 
      for (var j = 0; j < lab1Array[i].length ; j++) 
       { 
        if (lab1Array[i][j] == 1) 
        { 
         tile= new tile1; 
         tile.x = TILE_WIDTH*(j+0.5); 
         tile.y = TILE_HEIGHT*(i+0.5); 
         addChild(tile); 

        } 

       } 

     } 
    } 

} 

}

+1

你可以在你的'mc.hitTestObject(tile)'測試中提供代碼嗎?因爲在這裏,我沒有看到這個代碼中有碰撞的部分。 – blaazzze

+0

它沒有工作,所以我刪除它,但我想把它放在charManagement函數,檢查每一幀,並做類似:if(mc.hitTestObject(tile)== true && leftDown){} else if(leftDown && mc.hitTestObject(tile)== false){mc.x - = mcSpeed; } 這與每一個箭頭,但它不起作用=( – user2993550

+0

好吧,事實上,我意識到,當我打擊測試「瓷磚」它只測試最後一個創建任何想法如何打擊測試所有這些? – user2993550

回答

0

它很難給出了基於斷你的代碼的答案,但hitTestObject應該是關鍵斷檢測裏面的CharManagement函數內

if(leftDown) 
{ 
mc.x += mcSpeed; 
if mc.hitTestObject(tiles) 
{ 
    //add code for whatever you want to do 
} 
} 

我還需要看到你的tile1類,因爲我認爲這是可能不是一個對象,因此爲什麼hittestObject從未返回true。

P.S.當你說它沒有工作,這是否意味着它給你錯誤?或者它只是沒有達到你想要的?

+0

它doesn我做了什麼,但沒有造成任何錯誤,我的tile1類實際上是空的,只是用瓷磚創造了它,讓我嘗試了你的建議,但它也沒有給出任何結果。我必須要測試「tile」還是其他的東西? – user2993550

+0

好吧,事實上我意識到,當我打擊測試「瓷磚」時,它只測試最後一個創建的任何關於如何打擊測試所有的想法? – user2993550

0

您可以使用瓷磚爲玩家進行測試 這是通過在瓷磚類中使用更新功能來完成的,而不是您正在使用的更新功能。這樣做就像 mc.hittestobject(this);