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

2013/10/5

Switching Images

David.Budnick David.Budnick

2013/10/5

#
I am trying to make a game for a class in school but I can't seem to get one part. I have a simple score board that goes up if one animal is eaten. What I am trying to do is switch the image of the animal that is eaten when the score equals a certain number. Can anyone help???
Gevater_Tod4711 Gevater_Tod4711

2013/10/5

#
Therefore you need to get a reference to all the animals and change their images to the images you want. So in your counter you need to add some code to the act method:
//in your counter class;

//you need to import some classes to use this code:
import java.util.List;

//also you need another global variable;
private boolean imagesSet = false;

//in this example counter is the current number of the counter that is displayed and imageSetNumber is the number that has to be reached to change the images of the animals;
//imagesSet is a boolean to check whether the images already been set so you don't set them every act;
public void act() {
    if (!imagesSet && counter >= imageSetNumber) {
        imagesSet = true;
        //instead of Animal you have to use the classname of the objects you want to set the image of;
        List<Animal> animalsInWorld = getWorld().getObjects(Animal.class);
        for (Animal animal : animalsInWorld) {
            animal.setImage(new GreenfootImage("filename.png"));//here you have to add the right filename of your new image;
        }
    }
}
danpost danpost

2013/10/5

#
@David.Budnick, you will need to show your spawning code and related code around it for the animal that is eaten (probably once in the world class constructor (or a method it calls) and another time after an animal is eaten). Also show the constructor for the class of that animal.
David.Budnick David.Budnick

2013/10/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Mover

{
  private int score = 0;
  private GreenfootImage image2 = new GreenfootImage("Snake2.png");
  private GreenfootImage image3 = new GreenfootImage("Snake3.png");
  
  public void act()
  {
 score = score + 1;
if( score == 5 ) {
   setImage( image2 ); 
} 

}
}
This is what I have so far.. the problem is that it automatically switches the image at the start of the game
danpost danpost

2013/10/5

#
NMV, there is a bigger problem than what I had originally saw. Will post again with ideas.
danpost danpost

2013/10/5

#
(1) are you removing the snake when eaten and introducing a 'new' snake into the world (if not, how exactly are you dealing with the eating (removing) and introduction (adding) of new animal objects ) (2) what is eating the snake (what is the name of its class and what class does that class extend)
David.Budnick David.Budnick

2013/10/5

#
I am removing the snakes when they are eaten there are 3 levels I want the size of the snake to change when the score hit 5 and 15. A hedgehog is eating the snake "Hedgehog"
danpost danpost

2013/10/5

#
Then, the size of the snake needs to be determined before the snake is placed into the world. This means that it will need a value passed to it, to let it know what size to be. You need a Snake class constructor like what follows and you need to move the score field to the Hedgehog class with a 'get' method that returns the score.
public Snake(int score)
{
    if (score < 5) ; // use default image (do nothing)
    else if (score < 15) setImage(image2);
    else setImage(image3);
}
In your world constructor, or a method it calls, create the snake object with 'new Snake(0)'. In the act method of the Hedgehog class, you should have something like this:
if (canSee(Snake.class))
{
    eat(Snake.class);
    score++;
    getWorld().addObject(new Snake(score), someX, someY);
}
The 'canSee' and 'eat' methods are methods you will find in the Animal class which can be imported via the menubar of the Greenfoot application using "Edit>Import class...".
You need to login to post a reply.