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

2014/2/23

Image not setting straight away

Trxnce Trxnce

2014/2/23

#
hi guys me again, i have recently made a button that when moused over changes texture, the only problem is when i compile the button image is the greenfoot foot symbol until i mouse over the button then it goes back to normal. how can i make it so when i reset it keeps the 1st image. heres my code:
import greenfoot.*;  
  
public class StartButton extends Actor  
{  
    boolean mouseOver = false;  
  
    public void act()  
    {  
       if (!mouseOver && Greenfoot.mouseMoved(this))    
        {    
            setImage("Button2.png");    
            mouseOver = true;    
         }    
         if (mouseOver && Greenfoot.mouseMoved(null) && ! Greenfoot.mouseMoved(this))    
         {    
             setImage("Button1.png");    
             mouseOver = false;    
         }  
        if (Greenfoot.mouseClicked(this))    
        {    
            Greenfoot.setWorld(new Marioland());  
        }    
          
    }     
}  
danpost danpost

2014/2/23

#
There are a couple of ways to do that. (1) manually set the image for the actor by right clicking on the icon of the class and selecting 'Set image...'; then select the image and click OK. That will make that image the default image for any instances created from that class. (2) add a constructor ('public StartButton()') method to set the image programmatically to the "Button1.png" image.
danpost danpost

2014/2/23

#
There is another way also (this is not the preferred way however; since your button class is specific to a start button): (3) get a reference to the actor when you create it and set its image programmatically then. For example
Actor button = new StartButton();
button.setImage("Button1.png");
This way might be preferred if you had a generic button class that you create multiple buttons from. Then you would give each one its own image when created (like above), except you would probably not have the reference be a local one to the method, but an instance field to the world class. That way the world could check to see if any button was clicked and determine the proper code to execute depending on which one was clicked.
Trxnce Trxnce

2014/2/23

#
the first one worked cheers mate :) i just had to manually set the image and bang it was done, cheers
sobia sobia

2014/2/24

#
Danpost, I have actually tried the third approach that you mentioned in you reply ; in my cars game That I'm trying to make, the first time. However, I faced the exact problem as the OP stated. The greenfoot symbol. I just want to double check, when using setImage() the default path is the images folder correct? I will try posting a snippet possible later. Not on my machine right now. Thanks!
danpost danpost

2014/2/24

#
sobia wrote...
Danpost, I have actually tried the third approach that you mentioned in you reply ; in my cars game That I'm trying to make, the first time. However, I faced the exact problem as the OP stated. The greenfoot symbol.
Not possible if you created the actor and assigned it a reference in the first line and then set its image in the second before adding that object into the world (maybe you are adding a different instance into the world).
I just want to double check, when using setImage() the default path is the images folder correct?
Correct. Greenfoot uses the 'images' folder as the current directory path for images.
You need to login to post a reply.