本文介绍了瓷砖AS3的迷宫碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,所以我用这段代码创建了一个迷宫,我想对墙进行击打测试,但是当我尝试按if(mc.hitTestObject(tile))进行操作时,它什么也没做.我不确定为什么,这是代码:
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);
}
}
}
}
}
}
推荐答案
您可以使用图块对播放器进行测试这是通过使用tile类中的更新功能而不是当前使用的更新功能来完成的.这样就完成了mc.hittestobject(this);
You could use the tile to hit test for the playerThis is done by using an update function in the tile class, instead of the one you are currently using. This is done like somc.hittestobject(this);
这篇关于瓷砖AS3的迷宫碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!