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

2015/3/24

Accessing from a World, a variable/method from an Actor class

akilino akilino

2015/3/24

#
public memoryGame(){
        super(1015,766,1);
        addObject(new memoryGameTitle(), getWidth()/2, 100);
        addObject(new startMemoryButton(), 810,660);
        startMemoryButton accessVar = getWorld()getObjects(startMemoryButton.class).get(0);
        if(accessVar.clicked == true){
            removeObject(startMemoryButton.class);
        }
    }
That's the code from the world. I would to know if the variable "clicked" is true. If it is, then it should remove the object from that world.
public void act() 
    {
        GreenfootImage changeImage = new GreenfootImage(261,88);
        if(Greenfoot.mouseMoved(this)){
            changeImage.drawImage(new GreenfootImage("startMemoryClicked.png"),0,0);
            setImage(changeImage);
        }else if(Greenfoot.mouseMoved(null) && !(Greenfoot.mouseMoved(this))){
            changeImage.drawImage(new GreenfootImage("startMemoryButton.png"),0,0);
            setImage(changeImage);
        }
        
        if(Greenfoot.mouseClicked(this)){
            setClicked(true);
        }
    }
    
    public void setClicked(boolean c){
        clicked = c;
    }
This is the code from the Actor "startMemoryButton". When the button "Start Game" is clicked, it is set for "true".
danpost danpost

2015/3/24

#
First off, lines 5 through 7 in your world constructor are useless there. The world constructor is only executed one time -- when the world itself is created. The act method of the button will not execute until after the world is created and the scenario is started. So, the button can never be clicked on when the constructor executes Second, instead of setting the boolean, why not just have the button remove itself using:
getWorld().removeObject(this);
The act method in the world can always use 'if ( getObjects(startMemoryButton.class).isEmpty() )' to see if has been removed (clicked on) or not.
You need to login to post a reply.