hitTestObject 碰撞检测在 as3 中不起作用!
我正在尝试创建一个平台游戏,并且尝试让“player1”在遇到“平台”时停止。到目前为止,这是我的代码,
gotoAndStop("gameStart");
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
import flash.ui.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
player1.gotoAndStop("nothing");
//private var speed:Number = 0;
//private var maxspeed:Number = 4;
var myTimer:Timer = new Timer(10,0);
stage.focus = this;
player1.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
/*
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
player1.y += 2;
}
*/
function setup() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);
}
setup();
function reactToArrowKeys(keyEvent:KeyboardEvent) {
if (keyEvent.keyCode == 37) {
if (player1.x > 0) {
player1.x -= 5;
}
} else if (keyEvent.keyCode == 39) {
if (player1.x < 700) {
player1.x += 5;
}
}
}
function enterFrameHandler(e:Event):void {
if (player1.hitTestObject(platform)) {
trace("hitting");
} else {
player1.y += 4;
}
}
但是 hitTestObject 函数(enterFrameHandler)无法正常工作,并且将始终采用“else”路线。
请帮忙!
I am trying to create a platformer game and i am trying to make "player1" stop when it hits a "platform". here is my code so far,
gotoAndStop("gameStart");
import flash.display.MovieClip;
import flash.events.*;
import flash.ui.Keyboard;
import flash.ui.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
player1.gotoAndStop("nothing");
//private var speed:Number = 0;
//private var maxspeed:Number = 4;
var myTimer:Timer = new Timer(10,0);
stage.focus = this;
player1.addEventListener(Event.ENTER_FRAME,enterFrameHandler);
/*
myTimer.addEventListener(TimerEvent.TIMER,someFunction);
myTimer.start();
function someFunction(event:TimerEvent) {
player1.y += 2;
}
*/
function setup() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, reactToArrowKeys);
}
setup();
function reactToArrowKeys(keyEvent:KeyboardEvent) {
if (keyEvent.keyCode == 37) {
if (player1.x > 0) {
player1.x -= 5;
}
} else if (keyEvent.keyCode == 39) {
if (player1.x < 700) {
player1.x += 5;
}
}
}
function enterFrameHandler(e:Event):void {
if (player1.hitTestObject(platform)) {
trace("hitting");
} else {
player1.y += 4;
}
}
however the hitTestObject function (enterFrameHandler) does not work properly and will always take the "else" route.
please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发布的代码对我来说效果很好。我会寻找其他一些愚蠢的错误 - 例如,如果您复制并粘贴影片剪辑,则舞台上可能有多个名为“平台”的剪辑,在这种情况下,您的引用可能无法解析为您的引用打算。或者类似的其他东西。
要追踪它,请尝试调用:
这将告诉您 Flash 认为这些剪辑的边界框在哪里。我的猜测是,代码将返回与您预期不同的内容,解决该差异将显示错误所在。
The code as posted works fine for me. I'd look for some other kind of silly mistake - for example, if you copied and pasted movie clips, you might have more than one clip on the stage named "platform", in which case your reference may not resolve to the one you intend. Or something else along those lines.
To track it down, try calling:
which will tell you where Flash thinks the bounding boxes of those clips are. My guess is that code will return something other than what you'd expect, and resolving that discrepancy will show where the bug is.