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

2012/5/26

Adding Actor Button

BadatCoding BadatCoding

2012/5/26

#
I have an Actor which when clicked is meant to: 1) Show a preview of the 'shooter' which follows the mouse coordinates 2) When anywhere else on the world is clicked the shooter is to be added 3) The preview is then meant to be removed from the world. My issue is that when I add in code to remove the preview the preview never shows up in the first place. Here is the Preview Actor where the preview image is stored:
public class Preview extends Actor
{
 public Preview(String imageFile)
    {
        setImage(imageFile);
    }
    /**
     * Act - do whatever the Preview wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        MouseInfo mouseInfo = Greenfoot.getMouseInfo();
        
        if( mouseInfo == null )
            return;
        
        setLocation(mouseInfo.getX(), mouseInfo.getY());
    }    
}
Then I have the 'BuyShooter' Actor, which is meant to put those steps mentioned above into practice:
public class BuyShooter extends Actor
{
    shooter Shooter = new shooter();
    int mouseY, mouseX = 0;
    MouseInfo mouse = Greenfoot.getMouseInfo();
     boolean test = false;
     Preview preview;
    public void act() 
    {
        buy();
    } 
public void buy() {
     if(Greenfoot.mouseClicked(this)){
     preview();
}
}
 if(test == true){
            if(Greenfoot.mouseClicked(null)){
                if(notOn == false){
                MouseInfo mouseInfo = Greenfoot.getMouseInfo();
                mouseX = mouseInfo.getX();
                mouseY = mouseInfo.getY();
                getWorld().addObject(Shooter, mouseX, mouseY);
        // removePreview();
}
 public void preview(){
        preview = new Preview ("shooter1.png");
        getWorld().addObject(preview, mouseX, mouseY);
        test = true;
    }
ttamasu ttamasu

2012/5/26

#
hmm.... well if I were to write this I would change lines 13 to 18 in the act () method of Preview. I found out the hard way that if you don't have any new mouse event, MouseInfo will be null (which is most of the time) (eg you can click , move, drag, ect.. and it will create a MouseInfo event but if you leave the cursor at the current position it will be null) One easy fix is to just use a mouse click on the Preview window to put it away replace 13 to 18 with: if (Greenfoot.mouseClicked(this)) getWorld().removeObject(this); You don't need to setLocation on line 18 sice you are placing the preview window at the location on line 29 of the BuyShooter class
BadatCoding BadatCoding

2012/5/26

#
Thanks for the help ttamasu, I tried what you wrote but unfortuantly it doesn't work. I click on the BuyShooter button it adds the Preview image in the top corner (0,0) no longer following the users mouse like I wish it too. I also accidentally left out some code for the BuyShooter Actor last time:
public void removePreview(){
        getWorld().removeObject(preview);
    }
    public void tryIt(){
        // Remove the placed Shooter actor if it is placed on the BuyShooter button.
        Actor shooter1 = getOneIntersectingObject(shooter.class);
        if(shooter1 != null){
        notOn = true;
        getWorld().removeObject(Shooter);
    }
        else{   
            notOn = false;
        }
    }
danpost danpost

2012/5/26

#
It would be easier if the BuyShooter button only added the preview to the world; and the Preview class programmed to follow the mouse, and add the shooter when mouseClicked (and remove itself). It can also check for intersecting Buyshooter button before adding the shooter. It would be something like this:
// In the BuyShooter class

public void act()
{
    if (getWorld().getObjects(Preview.class).isEmpty() && Greenfoot.mouseClicked(this)
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        getWorld().addObject(new Preview(), mouse.getX(), mouse.getY());
    }
}

// In the Preview class

public void act()
{
    if (Greenfoot.mouseMoved(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        setLocation(mouse.getX(), mouse.getY());
    }
    if (Greenfoot.mouseClicked(null))
    {
        if (getIntersectingObjects(BuyShooter.class).isEmpty())
        {
            getWorld().addObject(new shooter(), getX(), getY());
        }
        getWorld().removeObject(this);
    }
}
BadatCoding BadatCoding

2012/5/27

#
Thanks danpost, I put in your code but got this error:
java.lang.NullPointerException
	at greenfoot.World.addObject(World.java:392)
	at BuyShooter.act(BuyShooter.java:31)
	at greenfoot.core.Simulation.actActor(Simulation.java:507)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:470)
	at greenfoot.core.Simulation.runContent(Simulation.java:204)
	at greenfoot.core.Simulation.run(Simulation.java:194)
danpost danpost

2012/5/27

#
Yeah, the mouse checks did that, change the Preview class act() method to:
public void act()
{
    if (Greenfoot.mouseClicked(null))
    {
        if (getIntersectingObjects(BuyShooter.class).isEmpty())
        {
            getWorld().addObject(new shooter(), getX(), getY());
        }
        getWorld().removeObject(this);
        return;
    }
    if (Greenfoot.mouseMoved(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        setLocation(mouse.getX(), mouse.getY());
    }
}
I will double-check and make sure it works.
BadatCoding BadatCoding

2012/5/27

#
It's ok it works, just had to add in the image name with the new Preview() so it's new Preview("shooter1.png") . Another thing, how can I check if the right click has been pressed to also removes the preview?
danpost danpost

2012/5/27

#
Worked OK for me! Hope you have success with it!
danpost danpost

2012/5/27

#
The 'Greenfoot.mouseClicked' method returns 'true' when either button is released (clicked).
BadatCoding BadatCoding

2012/5/27

#
Thanks dan, you've been a big help!
danpost danpost

2012/5/27

#
danpost wrote...
Yeah, the mouse checks did that, change the Preview class act() method to:
public void act()
{
    if (Greenfoot.mouseClicked(null))
    {
        if (getIntersectingObjects(BuyShooter.class).isEmpty())
        {
            getWorld().addObject(new shooter(), getX(), getY());
        }
        getWorld().removeObject(this);
        return;
    }
    if (Greenfoot.mouseMoved(null))
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        setLocation(mouse.getX(), mouse.getY());
    }
}
I will double-check and make sure it works.
Wanted to make sure others know, that it was NOT the mouse checks that caused the particular error you were having; and that the code I supplied works (both ways), at least as far as the mouse is concerned.
You need to login to post a reply.