嘿伙计们,所以我用这个代码创建了一个迷宫,我想要试试墙壁,但是当我尝试沿着if(mc.hitTestObject(tile))的行时,它什么也没做。我不知道为什么,这是代码:
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);
}
}
}
}
}
}
答案 0 :(得分: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。当你说它不工作时,这是否意味着它给你错误?或者它只是没有完成你想要的东西?
答案 1 :(得分:0)
您可以使用磁贴来测试播放器 这是通过在tile类中使用更新函数来完成的,而不是您当前使用的更新函数。这是这样做的 mc.hittestobject(本);