如何处理一个影片剪辑的多个实例的碰撞检测?

发布于 2024-08-31 04:36:01 字数 318 浏览 1 评论 0原文

我正在使用 Adob​​e Flash CS4。 语言是 Action Script 3

在我的库中,我有 2 个项目:

  • 玩家

玩家对象已经可以正确地移动他。 现在,当我将多个墙壁对象放置到舞台中(墙壁 = 32x32 像素)时,我想防止玩家在走进墙壁时移动。

我尝试为所有墙壁指定相同的实例名称,并仅检查与该对象的碰撞,但当我这样做时,碰撞仅适用于其中一堵墙壁。

我可以为墙的所有实例提供不同的碰撞脚本,但这非常耗时,是否有另一种方法可以将墙全局定义为玩家的实体?

提前致谢!

I'm using Adobe Flash CS4.
The language is Action Script 3

In my library I have 2 items:

  • player
  • wall

the player object is already functioning correctly with moving him around.
Now when I place multiple wall objects into the stage (wall = 32x32 px) I want to prevent the player from moving when he walks into a wall.

I've tried giving all the walls the same instance name and just check for a collision with that object but when I do that the collision only works for 1 of the walls.

I could give all the instances of wall a different collision script but this is way to time consuming, is there another way to globally define the wall as solid for the player?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

╄→承喏 2024-09-07 04:36:01

最好的选择是将所有墙实例放入一个父影片剪辑中,并检查播放器实例是否与其发生碰撞。

或者您可以循环遍历所有墙实例并单独检查。可能听起来很浪费,但这样您就可以为碰撞检测例程添加一些优化。例如,如果玩家位于屏幕的右侧部分,为什么要检查与左侧墙壁的碰撞。

或者您可以使用数学来检查冲突并避免 Flash 的内置例程。听起来可能需要更多工作,但看到它有多么有缺陷(受到帧速率等的限制),我会这样做——当然,如果游戏动态允许的话。

Your best bet would be to put all the wall instances in one parent movie clip and check the player instance for collision with it.

Or you can loop through all the wall instances and check separately. Might sound wasteful to you but that way you can add some optimizations to your collision detection routine. For example, why check for collision with the left wall if the player is in the right part of the screen.

Or you can use math to check for collisions and avoid Flash's built-in routine whatsoever. Might sound like even more work but seeing how flawed it is (limited by frame-rate, etc.), I would go that way -- if the game dynamics allows it, of course.

北陌 2024-09-07 04:36:01

请记住,任何影片剪辑都是 MovieClip 类的实例。通过为所有实例指定相同的名称,您可以有效地执行此操作:

var myClip:MovieClip = new MovieClip();
myClip = new MovieClip();

myClip <-- will always refer to the later movieclip since thats what it is now referenced to.

为了解决您的问题,您很可能会创建一个包含集合(很可能是数组)的类。您将对每个 MovieClip 的引用添加到此数组中(只需推入 MovieClip)。然后,您将拥有一个在每一帧或每次角色移动时调用的函数,该函数循环遍历此数组,检查墙壁是否击中玩家,如果确实发生,则返回 true,如果没有,则返回 false。

Remember that any movieclip is an instance of a MovieClip class. By giving all the instances the same name you are effectivly doing this:

var myClip:MovieClip = new MovieClip();
myClip = new MovieClip();

myClip <-- will always refer to the later movieclip since thats what it is now referenced to.

To solve your problem you would most likely create a class which contains a collection (most likely an Array). You will add a reference to each MovieClip into this Array (simply by pushing in the MovieClips). Then you would have a function that is called either each frame or each time the character moves that loops through this array checking to see if the wall hits the player and if it does returns true that this has occured or false if it has not.

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