Maze collision detection with tiles AS3

2019-09-18 17:23发布

问题:

Hey guys so I created a maze with this code and I want to hitTest the walls but when i try something along the lines of if (mc.hitTestObject(tile)) it doesn't do anything. And I'm not sure why, here's the code :

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:

Its hard to give an answer based off of your code, but hitTestObject should be inside of the key down detection inside the CharManagement function

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

I would also need to see your tile1 class, because i believe that it is possibly not an object, hence why hittestObject was never returning true.

P.S. when you say it wasnt working, does that mean it gave you errors? or it just didnt accomplish what you wanted?



回答2:

You could use the tile to hit test for the player This is done by using an update function in the tile class, instead of the one you are currently using. This is done like so mc.hittestobject(this);