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

2016/6/9

Avoider game-help me fix problems

donatela201 donatela201

2016/6/9

#
I finished chapter 2 in creative greenfoot book, so twink doesn´t work. method in Avatar class: addedToWorld isn´t called. Where should I put that method? I want to see skull eyes. It is written corectly, but it is never called - it doesn´t work.
protected void addedToWorld(World w){
        leftEye = new Eye();
        rightEye = new Eye();
        w.addObject(leftEye, getX()-10, getY()-8);
        w.addObject(rightEye, getX()+10,getY()-8);
        
    }
When I catch a clover, it not gives me health. Skull is the same, not changing.
public void addHealth(){
        if(health < 3){
            health++;
            if(--nextImage == 0){
                setImage("images/skull.png");
           }else{
               setImage("images/skull" + nextImage + ".png");
            }
  }
}
danpost danpost

2016/6/9

#
donatela201 wrote...
I finished chapter 2 in creative greenfoot book, so twink doesn´t work.
What exactly does "creative greenfoot book" refer to? and what version?
method in Avatar class: addedToWorld isn´t called. Where should I put that method? I want to see skull eyes. It is written corectly, but it is never called - it doesn´t work.
The 'addedToWorld' method is called by greenfoot (like the 'act' method is); only it is called when an object of the Actor subclass is added into a world. So, yes ... it is being called if you are adding an Avatar actor into a world.
When I catch a clover, it not gives me health. Skull is the same, not changing.
The image would have to change when losing health first. Then the 'addHealth' method would need to be called from somewhere. You might be able to just use the value of 'health' to set the appropriate image and bypass the use of the 'nextImage' field. I am quite sure that there is a one-to-one correspondence between their values -- maybe something like: nextImage = 3-health So, you can replace any occurrence of 'nextImage' with '3-health' and remove the 'nextImage' field altogether.
donatela201 donatela201

2016/6/9

#
So when I run the program, eyes aren´t there, there is just skull without eyes. AddedToWorld doesn´t start, but why? If I call that method somewhere, eyes are there, but they are in the center -I understand why, that´s mean that the methom addedToWorld works, but it isn´t called by greenfoot.
danpost danpost

2016/6/9

#
Please show the code for the class this 'addedToWorld' method is location within. Also, show what code you are using to add the Skull actor into the world.
You need to login to post a reply.