0
votes

I am currently coding a game, and have encountered an annoying glitch. Occasionally, when you kill an enemy in game, it will drop extra currency, and another enemy onscreen will be removed from the enemylist. However, this second enemy, the one removed from the enemylist, will still be on screen, and will still shoot at the player. Below are code snippets from the collision formula, the enemies destruction sequence, and the bullet hitcheck sequence.

Collision formula:

    public function testCollision(enemy:Entity):Boolean{
        var eX:Number = enemy.collision.xPos
        var eY:Number = enemy.collision.yPos
        var eSL:Number = enemy.collision.SideLength/2
        if(eX-xPos<(SideLength/2)+eSL && eY-yPos<(SideLength/2)+eSL && eX-xPos>-(SideLength/2)-eSL && eY-yPos>-(SideLength/2)-eSL){
            return true
        }else{
            return false
        }
    }

Enemy destruction sequence:

        if(deathVar){
            view.transparency -= 1/20
            if(view.transparency<0.1){
                var cur = new PixelCurrency(2)
                cur.collision.xPos = collision.xPos
                cur.collision.yPos = collision.yPos
                entityCreated.dispatch(cur)
                destroy()
            }
        }

Bullet hitcheck:

        for each (var enemy:Entity in Game.entities){
            if(enemy.allies == Pixapocalypse.EnemyFaction){
                if(collision.testCollision(enemy)){
                    if(enemy.life){
                        enemy.life.changeHealth(-2)
                        this.sound.playSound(new basicHitSound())
                        this.destroy()
                        break
                    }
                    else{

                    }
                }
            }
        }

If you need any extra info, please tell me, I am greatly appreciative of your help.

1
How do you control contents in Game.entities? Please add code that performs management on this collection. Most likely the trouble is forward lookup plus splicing PLUS doing something with the handler along with splicing.Vesper
@Vesper: What do you mean by control contents, if you mean addition and subtraction of entities from the vector, then that is done by entities.push(entity), splice(indexOf(entity)), and in a special case, entities.length = 0, the ordering on entities in the vector isn't changed, entities are just added and removed. If you meant something else, just let me know!ddewerg
Yes, I've meant exactly this. Note that splice() has 2 mandatory arguments, if the second one is omitted, the entire array tail is dropped off the array! So, you might have worse bugs in your code than you've discovered. Please post this part of code.Vesper
@Vesper, The line of code which actually splices the entities is this: entities.splice(entities.indexOf(entity), 1), I always screw up the splice command when writing it, but at least Flash catches me! However, I am inclined to believe that the problem lies with the 'Pixapocalypse.EnemyFaction' vector, as currency should not be automatically created on destruction, and extra currency appears when the glitch occurs.ddewerg
See, if you are using a for (var i:int=0;i<entities.length;i++) to iterate through your array, and you splice within this cycle, the element that's right after the one being spliced is not processed. Another nitpick is that you don't check if indexOf() returns a valid value, which can too occur under special conditions with a flawed code.Vesper

1 Answers

0
votes

What happens when Enemy.life < 0 ?