hitTestObject 碰撞检测在 as3 中不起作用!

发布于 2024-08-03 11:43:58 字数 1218 浏览 5 评论 0原文

我正在尝试创建一个平台游戏,并且尝试让“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追星践月 2024-08-10 11:43:58

发布的代码对我来说效果很好。我会寻找其他一些愚蠢的错误 - 例如,如果您复制并粘贴影片剪辑,则舞台上可能有多个名为“平台”的剪辑,在这种情况下,您的引用可能无法解析为您的引用打算。或者类似的其他东西。

要追踪它,请尝试调用:

trace( player1.getBounds(stage) );
trace( platform.getBounds(stage) );

这将告诉您 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:

trace( player1.getBounds(stage) );
trace( platform.getBounds(stage) );

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文