For each loop and collision detection in Actionscript 3
I am in the progress of making a Flash game and currently I am doing the coding for collision detection. The collision detection will be for detecting if the player is running into obstacles, and if the player runs into the obstacle, then it will stop him from running through it.
Here is my code that I am using for collision detection:
A createLevel function
private function createLevel():void开发者_运维知识库
{
for (var i:int; i < numChildren; i++)
{
if (getChildAt(i) is Trees)
{
var j = getChildAt(i);
treesArray.push(j);
trace(treesArray.length);
}
}
}
And a collision detection function.
private function checkCollisions():void
{
for each (var trees:Trees in treesArray)
{
if (player.hitTestObject(trees))
{
switch (true)
{
case (player.x <= trees.x) :
player.setRightSpeed(0);
break;
case (player.x >= trees.x) :
player.setLeftSpeed(0);
break;
}
switch (true)
{
case (player.y <= trees.y) :
player.setDownSpeed(0);
break;
case (player.y >= trees.y) :
player.setUpSpeed(0);
break;
}
}
else
{
player.resetSpeeds();
}
}
I am putting all the Trees objects that I have dragged onto the Flash stage into an Array. Then in the game loop, I have it so that it checks collisions with the checkCollision()
function.
I have it so that if the player hits any of the trees at all then it will see which side he hit it on and stop the player from moving in that direction. And if the player is not hitting anything then reset speeds to normal so he can move in any direction.
But the code only works for the 1st Tree object that is in the treesArray. By that I mean that the player will only be stopped if he is moving into the 1st tree and not any other tree in the array. How do I make it so that it works for all the trees?
Also I am thinking it might be due to the position of where my player.resetSpeeds()
function is but I am not precisely sure.
It's a bit unclear what resetSpeeds() does exactly, but I can tell from the code you provided that it will always be executed - unless the player hits all trees in the game at the same time.
Suppose there are 4 trees in the game and the player hits the second on the left side then the following will be executed on the player object in that order:
- resetSpeeds()
- player.setRightSpeed(0)
- resetSpeeds()
- resetSpeeds()
You'll have to first find the tree that's being hit and only then change the player's speed once (as opposed to as many times as there are trees in the game).
/** Find the first Trees that was hit by the player.
* Note that the loop will quit early once a collision is found. */
private function getHitTrees():Trees {
for each (var trees:Trees in treesArray) {
if (player.hitTestObject(trees)) {
return trees;
}
}
return null;
}
private function checkCollisions():void {
var hitTrees:Trees = getHitTrees();
if (hitTrees) { stopThePlayer(hitTrees); }
else { letThePlayerMoveOn(); }
}
This will also make your code more readable by splitting it up into smaller chunks with function names that tell you exactly what is happening in there; as opposed to one big function with a lot of nesting.
BTW: whoever taught you to that trick with switch(true)
: please kick him in the nuts. That is completely unreadable.
精彩评论