Question

What I have done is made an array and after doing so I've pushed two objects(enemies) in to that array.

Now I'm checking if any of the enemies from that array touch my player.

    private function collision():void 
    {

    goblin1 = new Goblin;
    goblin2 = new Goblin;

    goblinArray.push(goblin1);
    goblinArray.push(goblin2);  

    //get all enemies in to the loop    
    for (var i:int = 0; i < goblinArray.length; i++)
        {
            var goblin:Goblin = goblinArray[i]; //datatype goblin to the goblin class 


            if (goblin.hitTestObject(player)) //if anything from that array hits the player then do this
            {
                //make that individual goblin stop

            }
            else
            {
                //make the goblin and other goblins move

            }
        }
    }

This should technically work, but I can't think of a solution, I would appreciate hints very much.

Thank you for taking your time.

Ps I've just remembered: what ever goblin the player hits, I want him to do what's in the if statement, not all the goblins, i want to target that goblin that's been hit, and hit only.

Sorry If I could not explain it at best.

Edit; I'm trying to accomplish a hit test where the hit test tests to see if the player is hitting the goblins in the array.

If it does hit that goblin, in that array, then only that goblin in the array will stop, whilst the other goblins in the array move.

Was it helpful?

Solution

what ever goblin the player hits, I want him to do what's in the if statement, not all the goblins, i want to target that goblin that's been hit, and hit only.

But your statement is correct, you will have that needed goblin... If you want to stop your collision algorithm after first collision, you could use break or return;

if (goblin.hitTestObject(player)) //if anything from that array hits the player then do this
        {
            //Do whatever you need
            //Decide what flag you need canMove *or* cantMove ;) 
            goblin.canMove = false;
            break;
        }

OTHER TIPS

It depends what the goblin.cantMove and goblin.canMove setters do. If they're just Booleans, you will only need one - canMove can be set to true or false.
I assume you have a game loop somewhere else in your code. And when you update your goblins, if canMove is set to false they're prevented from moving.
If that's what you intend, then in your game loop code, I'd recommendand looping through your goblins and calling their update() method. Then, in your Goblin update() method check the goblin's canMove property and moving it only if canMove is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top