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

2017/5/2

mouseClicked() not working

janslem janslem

2017/5/2

#
Below you will find my code for the world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mainMenu extends World
{
    GreenfootImage bg;
    GreenfootImage bgImage;
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public mainMenu()
    {
        super(1280, 800, 1);
        colorOver();
        addObjects();
    }
    
    public void colorOver(){
        GreenfootImage bg = getBackground();
        Color green = new Color(0, 175, 0);
        for(int x = 320; x < 960; x++){
            for(int y = 300; y < bg.getHeight(); y++){
                bg.setColorAt(x, y, green);
            }
        }
    }
    
    public void isClicked(){
        if(Greenfoot.mouseClicked(creditsButton.class)){
            colorOver();
        }
    }
    
    public void addObjects(){
        bg = getBackground();
        fire fire1 = new fire();
        addObject(fire1, bg.getWidth()/2, 325);
        fire fire2 = new fire();
        addObject(fire2, fire1.getX() - 200, 325);
        fire fire3 = new fire();
        addObject(fire3, fire1.getX() + 200, 325);   
        
        startGameButton stBt = new startGameButton();
        addObject(stBt, bg.getWidth()/2, 430);
        
        upgradesButton ugBt = new upgradesButton();
        addObject(ugBt, bg.getWidth()/2, 510);
        
        optionsButton opBt = new optionsButton();
        addObject(opBt, bg.getWidth()/2, 590);
        
        creditsButton cdBt = new creditsButton();
        addObject(cdBt, bg.getWidth()/2, 670);
    }
}
And the method isClicked() doesn't seem it be working whatsoever. I click on the creditsButton, no error pops up, but colorOver() doesn't... you know... color over. In case you want me to move isClicked() to my creditsButton actor, the reason I can't, is because I can't access 'this' from a non-static method (isClicked()), and if I make it a static, colorOver() can't be accessed because apparently getBackground() can't be used in a static method.
Yehuda Yehuda

2017/5/2

#
janslem wrote...
And the method isClicked() doesn't seem it be working whatsoever. I click on the creditsButton, no error pops up, but colorOver() doesn't... you know... color over.
In the future (or starting right now) it would help if you would mention that you have a syntax error and where you have it. You said that no error pops up, but in your code you have a syntax error. Instead of saying your scenario isn't acting properly you should ask how to fix the error. On line 35 the parameter is an actor (instance) and you gave a class. Also that method is never called. In creditsButton which should be called CreditsButton place the following in the act method (or wherever you want the clicked to be checked for):
public void act() {
    if (Greenfoot.mouseClicked(this)) {
        ((mainMenu)getWorld()).colorOver(); // should be called MainMenu
    }
}
If you would move line 58 to 13 then you can just change line 35 to:
if (Greenfoot.mouseClicked(cdBt)) {
But you will also have to call the isClicked method somewhere.
danpost danpost

2017/5/2

#
janslem wrote...
Below you will find my code for the world < Code Omitted > And the method isClicked() doesn't seem it be working whatsoever. I click on the creditsButton, no error pops up, but colorOver() doesn't... you know... color over.
Main issue here is that you cannot click on a class, which is the object you are passing to the 'mouseClicked' method. You click on an instance of a World or of an Actor subclass. You could ask
if (Greenfoot.mouseeClicked(null) && (Greenfoot.getMouseInfo().getActor() instanceof creditsButton))
or you can ask
if (Greenfoot.mouseClicked((Actor)getObjects(creditsButton.class).get(0))))
or you can move line 58 to outside the 'addObjects' method (maybe insert it at line 13) and ask:
if (Greenfoot.mouseClicked(cdBt))
In case you want me to move isClicked() to my creditsButton actor, the reason I can't, is because I can't access 'this' from a non-static method (isClicked()), and if I make it a static, colorOver() can't be accessed because apparently getBackground() can't be used in a static method.
The 'this' reference is ALWAYS accessible from a non-static method. If you moved the 'isClicked' method to the creditsButton class, it should be modified to look like this (and should be called from the act method of the creditsButton class):
public void isClicked()
{
    if (Greenfoot.mouseClicked(this)) ((mainMenu)getWorld()).colorOver();
}
However, since you color over the area during the construction of the world (see line 20), coloring over it again with the same color will not produce any visible change.
danpost danpost

2017/5/2

#
Yehuda wrote...
In the future (or starting right now) it would help if you would mention that you have a syntax error and where you have it. You said that no error pops up, but in your code you have a syntax error. Instead of saying your scenario isn't acting properly you should ask how to fix the error. On line 35 the parameter is an actor (instance) and you gave a class.
Placing a Class object in for the parameter of the 'mouseClicked' method will not produce any errors as the parameter is of type Object, which means it will accept any type of object. It needs to be of type Object to accommodate both World and Actor types (which are the only types that can be clicked). Placing any other type object just causes the method to return a false value (that object was not clicked on).
janslem janslem

2017/5/2

#
danpost wrote...
However, since you color over the area during the construction of the world (see line 20), coloring over it again with the same color will not produce any visible change.
But 'addObject' is called after 'colorOver' in line 20 and 21, so if I was to call 'isClicked' after 'addObject', shouldn't it "color over" the objects added? If so, your code has managed to get rid of errors saying I wasn't allowed to access getBackground() from a static method, however 'isClicked' still does not appear to work/respond.
danpost danpost

2017/5/2

#
janslem wrote...
'addObject' is called after 'colorOver' in line 20 and 21, so if I was to call 'isClicked' after 'addObject', shouldn't it "color over" the objects added?
Your colorOver method works with the background image of the world. Actor images are ALWAYS drawn on top of the background image. This is why:
'isClicked' still does not appear to work/respond.
If you want to cover actors in your world, you need another actor to do that. You may need to add a 'setPaintOrder' method call to your world constructor so that the "cover" is always painted over any other actors in your world.
janslem janslem

2017/5/2

#
danpost wrote...
janslem wrote...
'addObject' is called after 'colorOver' in line 20 and 21, so if I was to call 'isClicked' after 'addObject', shouldn't it "color over" the objects added?
Your colorOver method works with the background image of the world. Actor images are ALWAYS drawn on top of the background image. This is why:
'isClicked' still does not appear to work/respond.
If you want to cover actors in your world, you need another actor to do that. You may need to add a 'setPaintOrder' method call to your world constructor so that the "cover" is always painted over any other actors in your world.
I'm guessing I could just do 'removeObject' then?
janslem janslem

2017/5/2

#
danpost wrote...
If you want to cover actors in your world, you need another actor to do that. You may need to add a 'setPaintOrder' method call to your world constructor so that the "cover" is always painted over any other actors in your world.
My new code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class mainMenu extends World
{
    GreenfootImage bg;
    GreenfootImage bgImage;
    creditsButton cdBt = new creditsButton();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public mainMenu()
    {
        super(1280, 800, 1);
        colorOver();
        addObjects();
        isClicked();
    }
    
    public void colorOver(){
        GreenfootImage bg = getBackground();
        Color green = new Color(0, 175, 0);
        for(int x = 320; x < 960; x++){
            for(int y = 300; y < bg.getHeight(); y++){
                bg.setColorAt(x, y, green);
            }
        }
    }
    
    public void isClicked(){
        if(Greenfoot.mouseClicked(cdBt)){
            removeObject(cdBt);
        }
    }
    
    public void addObjects(){
        bg = getBackground();
        fire fire1 = new fire();
        addObject(fire1, bg.getWidth()/2, 325);
        fire fire2 = new fire();
        addObject(fire2, fire1.getX() - 200, 325);
        fire fire3 = new fire();
        addObject(fire3, fire1.getX() + 200, 325);   
        
        startGameButton stBt = new startGameButton();
        addObject(stBt, bg.getWidth()/2, 430);
        
        upgradesButton ugBt = new upgradesButton();
        addObject(ugBt, bg.getWidth()/2, 510);
        
        optionsButton opBt = new optionsButton();
        addObject(opBt, bg.getWidth()/2, 590);
        
        addObject(cdBt, bg.getWidth()/2, 670);
    }
}
Doesn't get rid of cdBt :(
Super_Hippo Super_Hippo

2017/5/2

#
You need to place line 23 in the act method, not in the constructor.
janslem janslem

2017/5/3

#
Super_Hippo wrote...
You need to place line 23 in the act method, not in the constructor.
I'm disappointed that I didn't realize I could make an act method for world haha. Thank you very much!
You need to login to post a reply.