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

2023/4/16

How to switch images slowly?

UAT123 UAT123

2023/4/16

#
I'm creating a snake game where there are a variety of healthy food to score points from eating. I want the image of the food the change once the food is touched by the snake. Here's my code currently and it's not doing the work. Can somebody help, please? import greenfoot.*; public class healthy extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; static int score = 10; static int time; public healthy() { image1 = new GreenfootImage ("fish.png"); image2 = new GreenfootImage ("broccoli.png"); image3 = new GreenfootImage ("tomato.png"); setImage(image1); getImage().scale(86,43); } public void act() { if (isTouching(snake.class)) { MyWorld w = (MyWorld) getWorld(); getWorld().removeObject(this); w.addPoint(); w.newHealthy(); switchImage(); } } public void switchImage() { if (getImage() == image1) { setImage(image2); getImage().scale(67,66); MyWorld w = (MyWorld) getWorld(); } else if(getImage() == image2) { setImage(image3); getImage().scale(76,78); MyWorld w = (MyWorld) getWorld(); } else if(getImage() == image3) { setImage(image1); getImage().scale(86,43); MyWorld w = (MyWorld) getWorld(); } } }
danpost danpost

2023/4/16

#
UAT123 wrote...
I'm creating a snake game where there are a variety of healthy food to score points from eating. I want the image of the food the change once the food is touched by the snake. Here's my code currently and it's not doing the work. Can somebody help, please? << Code Omitted >>
Seems to me some arrays might be in order. Throw in an index retainer for the images and we get something like this:
private GreenfootImage[] images;
private int imgNum = -1;

public healthy()
{
    images = new GreenfootImage[] {
        new GreenfootImage("fish.png"),
        new GreenfootImage("broccoli.png"),
        new GreenfootImage("tomato.png")
    };
    int[][] size = new int[][] { { 86, 43 }, { 67, 66 }, { 76, 78 } };
    for (int i=0; i<size.length; i++) {
        images[i].scale(size[i][0], size[i][1]);
    }
    switchImage();
}

private void switchImage() {
    imgNum = (imgNum+1)%images.length;
    setImage(images[imgNum]);
}
The act method has no issues pertaining to this.
UAT123 UAT123

2023/4/16

#
Thanks for replying but the image doesn't switch when the food is eaten. I wonder if it has anything to do with this bit of the code:
images = new GreenfootImage[] {
            new GreenfootImage("fish.png"),
            new GreenfootImage("broccoli.png"),
            new GreenfootImage("tomato.png")
        };
danpost danpost

2023/4/17

#
UAT123 wrote...
Thanks for replying but the image doesn't switch when the food is eaten. I wonder if it has anything to do with this bit of the code: << Code Omitted >>
No. That code should be fine. Please present your current class codes in full.
You need to login to post a reply.