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

2024/6/10

Image is only scaling once but never again

toemilio toemilio

2024/6/10

#
So I'm making a flappy bird project for my computer science class and one of our extensions is to create a powerup that makes you smaller. My problem is that the code will run perfectly once, it scales my image and makes it smaller, but every other time I run it the image stays the same. Every solution I try it will work once but never again. Here is my code...
public class FlappyBird extends Actor
{
 public void FlappyBird()
    {
        setImage("flappyBird1.png");
    } 
public void act()
    {
        if (getOneIntersectingObject(Powerup.class) != null)
        {
             switchImage();
        }    
    }
/**
     *
     */
    public void switchImage()
    {
        int size = getImage().getWidth() / 2 ;
        int size2 = getImage().getHeight() / 2 ;
        GreenfootImage image = getImage();
        image.scale(size, size2 );
        setImage(image);
        
    }
}

public class Powerup extends Actor
{
    int POWER_SPEED = -2;
    
    /**
     * Act - do whatever the Powerup wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation( getX() + POWER_SPEED , getY());
        if (getOneIntersectingObject (FlappyBird.class) != null)
        {
            getWorld().removeObject(this);
        }
danpost danpost

2024/6/11

#
toemilio wrote...
So I'm making a flappy bird project for my computer science class and one of our extensions is to create a powerup that makes you smaller. My problem is that the code will run perfectly once, it scales my image and makes it smaller, but every other time I run it the image stays the same. Every solution I try it will work once but never again. << Code Omitted >>
If line 3 is to be the beginning of the constructor of the class, then the keyword void should be omitted from the line.
You need to login to post a reply.