你现在知道如何制作各种各样的图形对象,但你能用他们做什么?一个有趣的事情是构建一个简单的碰撞检测系统。你可以使用一个自定义函数 hitTestRectangle来检查任何两个矩形Pixi sprites是否接触。
如果它们重叠,hitTestRectangle将返回true。您可以使用hitTestRectangle与if语句来检查两个精灵这样的之间的冲突:
正如你将看到的,hitTestRectangle是进入庞大宇宙的游戏设计的前门。
在collisionDetection.html文件examples夹中运行该文件,获取如何使用的工作示例hitTestRectangle。使用箭头键移动猫。如果猫碰到盒子,盒子变红,“打!由文本对象显示。
[img]http://dl2.iteye.com/upload/attachment/0123/3480/e593d90c-7e24-35a8-ba58-4bd4f82a0369.png[/img]
你已经看到了创建所有这些元素的所有代码,以及使猫移动的键盘控制系统。唯一新的东西是在函数hitTestRectangle内部使用的方式play来检查冲突。
hitTestRectangle(spriteOne, spriteTwo)
如果它们重叠,hitTestRectangle将返回true。您可以使用hitTestRectangle与if语句来检查两个精灵这样的之间的冲突:
if (hitTestRectangle(cat, box)) {
//There's a collision
} else {
//There's no collision
}
正如你将看到的,hitTestRectangle是进入庞大宇宙的游戏设计的前门。
在collisionDetection.html文件examples夹中运行该文件,获取如何使用的工作示例hitTestRectangle。使用箭头键移动猫。如果猫碰到盒子,盒子变红,“打!由文本对象显示。
[img]http://dl2.iteye.com/upload/attachment/0123/3480/e593d90c-7e24-35a8-ba58-4bd4f82a0369.png[/img]
你已经看到了创建所有这些元素的所有代码,以及使猫移动的键盘控制系统。唯一新的东西是在函数hitTestRectangle内部使用的方式play来检查冲突。