什么是更有效的碰撞方式?
What's a more efficient way to do collisions?
这是我的困境
我的游戏舞台周围有 4 堵墙,当玩家撞到这些墙时,我不想为每一堵墙都做一个 if 语句来检查玩家是否撞到它,所以我有创建了一个数组来固定墙壁,然后检查玩家是否击中了它。现在,因为我正在这样做,所以如果玩家击中某物,我将不知道他实际击中了什么,如果他击中 [0], [1], [2]
等,我将无法检查我的数组,因为那时我会回来做检查他是否撞到了特定的墙壁。我不想这样做的原因是为了将来,当我添加更多障碍物、建筑物等时。
所以我的问题是,我如何进行碰撞检查,而不对特定对象进行硬编码检查,并提供某种可用于玩家响应的值,例如,如果您撞到顶墙你可以在不执行上述操作的情况下以某种方式解决这个问题,然后做到这一点,这样你就不能走过什么的,
if (main.playerPosKeeper_mc.hitTestObject(this[main.StageCollisions]))
{
trace("hit");
}
StageCollisions 是一个数组,其中包含我所有的障碍。
当玩家在 StageCollisions 中击中任何东西时,我不能简单地从他的 y 值或 x 值中减去,因为我不知道他击中了哪个对象,但我也不想硬对其进行编码,以便我检查我是否正在击中让我们说是最高障碍,因为如果我只是回到制作静态 if else 语句,那么为什么首先要做一个数组。
^^ 引用此主题
AS3 - How to Cycle States of Character Animations (moving & stopped)
这困扰了我一段时间,非常感谢您的帮助。这是一个很难形成的问题,因此如有必要,我可以澄清要点。
一些琐碎的伪代码供您研究:
private function collisionCheck(h:Sprite):Sprite{ // pass the hero Sprite into this function and it will return the wall that it hit
for each (b:Sprite in blockArray){ // if your array of hit-able objects is called "blockArray"
if (h.hitTtestObject(b)){ // check the hero Sprite against all objects in the array
return b;
}
}
return null;
}
然后,在您代码的其他地方(可能在您的 gameTick 函数或 gameLoop 函数中,或者您在每一帧重复游戏逻辑的任何地方:
private function gameTick():void{
var objectHit:Sprite = collisionCheck(_myHero); // this will run the collision check function, and return the sprite that the hero collides with;
if (objectHit != null){
objectHit.alpha = 0.5;
// this will give you a visible representation that your code is indeed working, or not.
}
}
在您的英雄没有与任何东西发生碰撞的那些时刻,此功能将 return 无效。这就是为什么我在尝试对其 alpha 值执行操作之前首先检查 objectHit 是否不为空。当然,除了在项目中更改其 alpha 值之外,您还会做一些其他事情,但这是我经常做的事情(使用 alpha)以快速获得视觉确认,即事物正在检测它们应该检测的内容。
So my question is, how can I do collision checks, without hard coding
checks on specific objects, and giving some sort of value that can be
used for the player to respond to, for example if your hitting the top
wall and you can figure that out somehow without doing the above, then
make it so you can't walk through or something
是的,所以您需要一种方法来执行通用碰撞响应。这可能是一个很大的话题。最简单的方法通常是在移动后检查碰撞,然后在发生碰撞时反转移动。
像这样:
function movePlayer(movementX:Number, movementY:Number):void {
var originalX:Number = player.x;
var originalY:Number = player.y;
player.x += movementX;
if (checkCollision()) {
player.x = originalX;
}
player.y += movementY;
if (checkCollision()) {
player.y = originalY;
}
}
function checkCollision():Boolean {
for each (var wall:MovieClip in walls) {
if (player.hitTestObject(wall)) {
return true;
}
}
return false;
}
这样你可以 checkCollision()
检查 4 面墙或 50 面墙,没关系。它不会让玩家进入其中。
这只是一个起点,还有很多方法可以分解或完善它。
这是我的困境
我的游戏舞台周围有 4 堵墙,当玩家撞到这些墙时,我不想为每一堵墙都做一个 if 语句来检查玩家是否撞到它,所以我有创建了一个数组来固定墙壁,然后检查玩家是否击中了它。现在,因为我正在这样做,所以如果玩家击中某物,我将不知道他实际击中了什么,如果他击中 [0], [1], [2]
等,我将无法检查我的数组,因为那时我会回来做检查他是否撞到了特定的墙壁。我不想这样做的原因是为了将来,当我添加更多障碍物、建筑物等时。
所以我的问题是,我如何进行碰撞检查,而不对特定对象进行硬编码检查,并提供某种可用于玩家响应的值,例如,如果您撞到顶墙你可以在不执行上述操作的情况下以某种方式解决这个问题,然后做到这一点,这样你就不能走过什么的,
if (main.playerPosKeeper_mc.hitTestObject(this[main.StageCollisions]))
{
trace("hit");
}
StageCollisions 是一个数组,其中包含我所有的障碍。
当玩家在 StageCollisions 中击中任何东西时,我不能简单地从他的 y 值或 x 值中减去,因为我不知道他击中了哪个对象,但我也不想硬对其进行编码,以便我检查我是否正在击中让我们说是最高障碍,因为如果我只是回到制作静态 if else 语句,那么为什么首先要做一个数组。
^^ 引用此主题
AS3 - How to Cycle States of Character Animations (moving & stopped)
这困扰了我一段时间,非常感谢您的帮助。这是一个很难形成的问题,因此如有必要,我可以澄清要点。
一些琐碎的伪代码供您研究:
private function collisionCheck(h:Sprite):Sprite{ // pass the hero Sprite into this function and it will return the wall that it hit
for each (b:Sprite in blockArray){ // if your array of hit-able objects is called "blockArray"
if (h.hitTtestObject(b)){ // check the hero Sprite against all objects in the array
return b;
}
}
return null;
}
然后,在您代码的其他地方(可能在您的 gameTick 函数或 gameLoop 函数中,或者您在每一帧重复游戏逻辑的任何地方:
private function gameTick():void{
var objectHit:Sprite = collisionCheck(_myHero); // this will run the collision check function, and return the sprite that the hero collides with;
if (objectHit != null){
objectHit.alpha = 0.5;
// this will give you a visible representation that your code is indeed working, or not.
}
}
在您的英雄没有与任何东西发生碰撞的那些时刻,此功能将 return 无效。这就是为什么我在尝试对其 alpha 值执行操作之前首先检查 objectHit 是否不为空。当然,除了在项目中更改其 alpha 值之外,您还会做一些其他事情,但这是我经常做的事情(使用 alpha)以快速获得视觉确认,即事物正在检测它们应该检测的内容。
So my question is, how can I do collision checks, without hard coding checks on specific objects, and giving some sort of value that can be used for the player to respond to, for example if your hitting the top wall and you can figure that out somehow without doing the above, then make it so you can't walk through or something
是的,所以您需要一种方法来执行通用碰撞响应。这可能是一个很大的话题。最简单的方法通常是在移动后检查碰撞,然后在发生碰撞时反转移动。
像这样:
function movePlayer(movementX:Number, movementY:Number):void {
var originalX:Number = player.x;
var originalY:Number = player.y;
player.x += movementX;
if (checkCollision()) {
player.x = originalX;
}
player.y += movementY;
if (checkCollision()) {
player.y = originalY;
}
}
function checkCollision():Boolean {
for each (var wall:MovieClip in walls) {
if (player.hitTestObject(wall)) {
return true;
}
}
return false;
}
这样你可以 checkCollision()
检查 4 面墙或 50 面墙,没关系。它不会让玩家进入其中。
这只是一个起点,还有很多方法可以分解或完善它。