This site requires JavaScript, please enable it in your browser!
Greenfoot back
ImAndrew
ImAndrew wrote ...

2018/1/5

Damage the enemy

ImAndrew ImAndrew

2018/1/5

#
Hello, I'm kinda new to game programming and OOP and I have a little problem in my game.I don't know how to modify de enemy health from the hero attack method.I get a error at "enemy.health -= damage;" and it says "cannot find symbol - var health" (The enemy class has that variable). I tried to lower the enemy health when the player is near it and uses space every 50 frames(attackSpeed variable).
void attack(int _damage)
    {
        if(Greenfoot.isKeyDown("space"))
        {
            Actor enemy = getOneIntersectingObject(Enemy.class);
            if(enemy != null && frameCounter >= attackSpeed)
            {
                enemy.health -= damage;
                frameCounter = 0;
            }
        }
    }
Super_Hippo Super_Hippo

2018/1/5

#
Change line 5 to
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
ImAndrew ImAndrew

2018/1/5

#
Super_Hippo wrote...
Change line 5 to
Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
Thanks, it's working now
You need to login to post a reply.