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

2014/11/17

Help with image

emilpro01 emilpro01

2014/11/17

#
Hi, I'm new to greenfoot and i have no clue how to make my lady bug change image when it hits the apple. I have been trying to figure out the solution by my self and some forum search but so far no luck. Any ways here is what my ladybug code looks like.
public class Ladybug extends Actor
{
    public int applecounter;
    GreenfootSound eating = new GreenfootSound("eating.mp3");
    //GreenfootSound GamveOver = new GreenfootSound("GamveOver.mp3");
    private GreenfootImage image1;
    private GreenfootImage image2;
    /**
     * Act - do whatever the Ladybug wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("w")) {  
            MouseInfo m = Greenfoot.getMouseInfo();  
            if (m != null) {  
                turnTowards(m.getX(), m.getY()); 
                move(4);
                Actor Apple = getOneObjectAtOffset(0, 0, Apple.class);
                if(Apple !=null) {
                    getWorld() .removeObject(Apple); 
                    Greenfoot.playSound("eating.mp3");
                    applecounter++;
                    if (applecounter ==7) {
                        //Greenfoot.stop();
                        World world = getWorld();
                        if (world !=null) 
                        {
                            world.removeObjects(world.getObjects(null));
                            world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
                        }
                    }
                }  
            }
        }
       
    }    
    public void image()
    {
        image1 = new GreenfootImage("ladybug1.png");
        image2 = new GreenfootImage("ladybug2.png");
    }
}
danpost danpost

2014/11/17

#
The 'images' method does not appear to be called from anywhere. You should probably have a constructor in the class where that method is called from.
ejgod46 ejgod46

2014/11/17

#
danpost wrote...
The 'images' method does not appear to be called from anywhere. You should probably have a constructor in the class where that method is called from.
it did but i took out because i have no idea what to do with it or how to use it. I have put this in my if apple !=null command but when my lady hit the apple it disappears and stay invincible. I have to make 2 new different account because of the spam thing, any ways im the same guy and this is the code that i tried to use
 public void act()
    {
        if (getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
        
    }
danpost danpost

2014/11/17

#
@emilpro01, the code in the constructor of a class is executed when you use the 'new' keyword on the name of the class, 'new Ladybug()'. In your case, that would be a good place to set the initial image of the ladybug. The image fields can also be set in the constructor before setting the image of the ladybug (you could call the 'images' method there); or, those field could be set when declared (in lines 6 and 7) and the 'images' method can be removed entirely. @ejgod46, your 'act' method will only have your ladybug franticly changing colors back and forth. However, if you changed the name of the method to something like 'changeImage' and call it when an apple is detected, you would be on your way.
ejgod46 ejgod46

2014/11/17

#
yo i think i got it, any ways this is what i did,
{
    public int applecounter;
    GreenfootSound eating = new GreenfootSound("eating.mp3");
    //GreenfootSound GamveOver = new GreenfootSound("GamveOver.mp3");
    private GreenfootImage image1 = new GreenfootImage("ladybug1.png");
    private GreenfootImage image2 = new GreenfootImage("ladybug2.png");
    /**
     * Act - do whatever the Ladybug wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("w")) {  
            MouseInfo m = Greenfoot.getMouseInfo();  
            if (m != null) {  
                turnTowards(m.getX(), m.getY()); 
                move(4);
                Actor Apple = getOneObjectAtOffset(0, 0, Apple.class);
                if(Apple !=null) {
                    getWorld() .removeObject(Apple); 
                    changeImage();
                    Greenfoot.playSound("eating.mp3");
                    applecounter++;
                    if (applecounter ==7) {
                        //Greenfoot.stop();
                        World world = getWorld();
                        if (world !=null) 
                        {
                            world.removeObjects(world.getObjects(null));
                            world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2);
                        }
                    }
                }  
            }
        }
        
    }    

    public void changeImage()
    {
        if (getImage() == image1)
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }

    }

   
}
emilpro01 emilpro01

2014/11/25

#
danpost wrote...
Where is the rest of the code in the class that deals with the images? you should have some field declaration statements and a constructor. Are the two images saved in the fields in the 'images' method the only images of the ladybug in two different colors? Which image does a new ladybug initially have?
I was trying to under this , but i have no idea what to do or where to put this, so i put it in my if apple != null but when my lady bug hits an apple,it disappears and never goes back to its original image.
tkurban tkurban

2014/11/25

#
danpost wrote...
The 'images' method does not appear to be called from anywhere. You should probably have a constructor in the class where that method is called from.
It did but I took it because i have no idea how to use it since im new and still learning.But what i did is this i put this on my if apple !=null command but when it hit my apple my lady bug disappears and it just stay invincible
danpost danpost

2014/11/25

#
Maybe you do not understand the reason behind my last comment. If you do not set anything to the image fields ('image1' and 'image2') then what do you think would happen if you tried to set one of them as the image for your actor?
You need to login to post a reply.