[英]Flash actionscript 3, collision detection upon keypress
I am trying to get my character to collide with the walls I have created but I am having difficulties doing so, I have tried using solutions in other threads but none have worked. 我试图让我的角色与我创建的墙发生碰撞,但是这样做有困难,我尝试在其他线程中使用解决方案,但没有一个起作用。 help please.
请帮助。 Essentially I got collision detection working, however it only activates upon clicking, with a mouse event.
本质上,我可以进行碰撞检测,但是只有在单击时,鼠标事件才会激活。
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e: MouseEvent): void {
e.target.startDrag();
}
function drop(): void {
head_mc.x -= velocity;
stopDrag();
if (head_mc.hitTestObject(square2_mc)) {
velocity = 0;
trace("Collision detected!");
} else {
trace("No collision.");
velocity = 5;
}
}
What I am trying to do is, make head_mc
, my player model, stop moving to the right upon colliding with square_mc
, my wall. 我要做的是,使我的玩家模型
head_mc
在与我的墙square_mc
碰撞后停止向右移动。 It only works when I click on the object, I need it to work by only moving the head_mc
to the block my using my arrow keys. 它仅在单击对象时起作用,我需要通过使用箭头键将
head_mc
移至块head_mc
使其head_mc
。
You can only detect collision upon clicking because you have put your hitTestObject
inside a function that is a MouseEvent
function. 您只能在单击时检测到碰撞,因为您已将
hitTestObject
放在了一个MouseEvent
函数中。 What you could do is that instead of testing collision on MOUSE_UP
and MOUSE_DOWN
, test the collision with a frequent interval of time by either using TimerEvent
or directly calling the function through Event.ENTER_FRAME
. 您可以做的是,不是使用
MOUSE_UP
和MOUSE_DOWN
来测试冲突,而是使用TimerEvent
或通过Event.ENTER_FRAME
直接调用函数,以频繁的时间间隔测试冲突。 That way, the program will keep checking for collision after a very short interval of time and hence, when collision is detected, the function could trace out trace("Collision detected!");
这样,程序将在很短的时间间隔后继续检查碰撞,因此,当检测到碰撞时,该函数可以跟踪
trace("Collision detected!");
and every second it is not colliding, it will keep tracing trace("No collision.");
并且每秒钟都不会发生冲突,它将继续跟踪
trace("No collision.");
and your velocity will be set to 5. 并将您的速度设置为5。
you could add a function this way: 您可以通过以下方式添加函数:
function checkCollision(e: Event): void {
if (head_mc.hitTestObject(square2_mc)) {
velocity = 0;
trace("Collision detected!");
} else {
trace("No collision.");
velocity = 5;
}
}
stage.addEventListener(Event.ENTER_FRAME, checkCollision);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.