嘿傢伙,所以我創建了這個代碼的迷宮,我想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);
}
}
}
}
}
}
你可以在你的'mc.hitTestObject(tile)'測試中提供代碼嗎?因爲在這裏,我沒有看到這個代碼中有碰撞的部分。 – blaazzze
它沒有工作,所以我刪除它,但我想把它放在charManagement函數,檢查每一幀,並做類似:if(mc.hitTestObject(tile)== true && leftDown){} else if(leftDown && mc.hitTestObject(tile)== false){mc.x - = mcSpeed; } 這與每一個箭頭,但它不起作用=( – user2993550
好吧,事實上,我意識到,當我打擊測試「瓷磚」它只測試最後一個創建任何想法如何打擊測試所有這些? – user2993550