AS3:如何检测碰撞?
我正在尝试制作一个简单的游戏,当球落入某个方块时,你就赢了。
机制:球会穿过几个障碍物,最后有两个方块,如果球碰到左边的方块,你就赢了,下一关将包含更多的方块,并且它们之间的空间更小。
测试影片(点击屏幕让球落下): http://gabrielmeono.com/downloads/Lucky_Hit_Alpha.swf
这些是主要变量:
var winBox:QuickObject;//You win
var looseBox:QuickObject;//You loose
var gameBall:QuickObject;//Ball dropped
问题:
- 如何检测碰撞并在球击中 winBox 时创建一个函数? (获胜消息/下一级别)*
谢谢,这是完整的代码:
package {
import flash.display.MovieClip;
import com.actionsnippet.qbox.*;
import flash.events.MouseEvent;
[SWF(width = 600, height = 600, frameRate = 60)]
public class LuckyHit extends MovieClip {
public var sim:QuickBox2D;
var winBox:QuickObject;
var looseBox:QuickObject;
var gameBall:QuickObject;
/**
* Constructor
*/
public function LuckyHit()
{
sim = new QuickBox2D(this);
//sim.createStageWalls();
winBox = sim.addBox({x:5,y:600/30, width:300/30, height:10/30, density:0});
looseBox = sim.addBox({x:15,y:600/30, width:300/30, height:10/30, density:0});
// make obstacles
for (var i:int = 0; i<(stage.stageWidth/50); i++){
//End
sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});
//Mid End
sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});
//Middle Start
sim.addCircle({x:0 + i * 1.5, y:09, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:08, radius:0.1, density:0});
sim.addCircle({x:0 + i * 1.5, y:07, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:06, radius:0.1, density:0});
}
sim.start();
stage.addEventListener(MouseEvent.CLICK, _clicked);
}
/**
* ..
* @param e MouseEvent.CLICK
*/
private function _clicked(e:MouseEvent)
{
gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
}
}
}
I'm trying to make a simple game, when the ball falls into certain block, you win.
Mechanics: The ball falls through several obstacles, in the end there are two blocks, if the ball touches the left block you win, the next level will contain more blocks and less space between them.
Test the movie (click on the screen to drop the ball):
http://gabrielmeono.com/downloads/Lucky_Hit_Alpha.swf
These are the main variables:
var winBox:QuickObject;//You win
var looseBox:QuickObject;//You loose
var gameBall:QuickObject;//Ball dropped
Question:
- How do I detect a collision and make a function if the ball hits the winBox? (Win message/Next level)*
Thanks, here is the full code:
package {
import flash.display.MovieClip;
import com.actionsnippet.qbox.*;
import flash.events.MouseEvent;
[SWF(width = 600, height = 600, frameRate = 60)]
public class LuckyHit extends MovieClip {
public var sim:QuickBox2D;
var winBox:QuickObject;
var looseBox:QuickObject;
var gameBall:QuickObject;
/**
* Constructor
*/
public function LuckyHit()
{
sim = new QuickBox2D(this);
//sim.createStageWalls();
winBox = sim.addBox({x:5,y:600/30, width:300/30, height:10/30, density:0});
looseBox = sim.addBox({x:15,y:600/30, width:300/30, height:10/30, density:0});
// make obstacles
for (var i:int = 0; i<(stage.stageWidth/50); i++){
//End
sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});
//Mid End
sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});
//Middle Start
sim.addCircle({x:0 + i * 1.5, y:09, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:08, radius:0.1, density:0});
sim.addCircle({x:0 + i * 1.5, y:07, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:06, radius:0.1, density:0});
}
sim.start();
stage.addEventListener(MouseEvent.CLICK, _clicked);
}
/**
* ..
* @param e MouseEvent.CLICK
*/
private function _clicked(e:MouseEvent)
{
gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不熟悉 QuickBox2D,但请尝试一下。
QuickBox2D 文档中的相关类是QuickContacts。
I'm not familiar with QuickBox2D, but give this a shot.
The relevant class in the QuickBox2D documentation is QuickContacts.
有一些库可以处理碰撞检测。一个简单的谷歌搜索应该会返回很多。
同时,您可以随时查看这篇文章
There are a few libraries available that can handle collision detection. A simple Google search should return quite a few.
In the meantime, you can always check this article
如果两个对象的边界框重叠并且两个对象的边界圆重叠,则圆形和矩形发生碰撞。
但就像加布里埃尔所说,你最好使用一些现有的库,而不是编写自己的库,除非你真的想了解它背后的机制。
A circle and a rectangle is colliding if bounding boxes of both objects overlap and bounding circles of both objects overlap.
But like Gabriel said, you'll be better off using some existing library, rather than writing your own, unless you really want to understand the mechanics behind it.
Flixel 是一个很好的游戏入门库,碰撞检测是一个核心功能。
除非这是不允许外部图书馆的课堂作业?
Flixel is a good library for beginning game making, and collision detection is a core feature.
Unless this is a class assignment that doesn't allow outside libraries?