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

2014/1/26

Referencing an instance of an object through a list

thebrownkid23 thebrownkid23

2014/1/26

#
    public void enemyCheck(){
        Actor actor = getOneIntersectingObject(enemy.class);
        if(actor != null){
            actor.setHealth((actor.getHealth() - power));
            exists = false;
            getWorld().removeObject(this);
            
        }
 }
So I have this code for a laser that is shot from a spaceship. In the last method, enemyCheck, it checks to see whether it has collided with an enemy.If it has, I want it to set the enemy that it collided with's health to whatever it was less the power of the laser. I have methods in the enemy class to get and set health and power is properly set. My question is, this turns up saying that it cannot find the method getHealth(). I tried putting it on the actor as you can see above but it isn't working. How would I go about doing this?
Zamoht Zamoht

2014/1/26

#
You have to keep the reference as an enemy class: enemy actor = (enemy)getOneIntersectingObject(enemy.class); And make class names start with a capital letter.
thebrownkid23 thebrownkid23

2014/1/27

#
Okay, thank you. And, I'll change it thanks
You need to login to post a reply.