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

2019/6/23

replay button not working

AniSusi AniSusi

2019/6/23

#
so, if i place the replay button in my world manually, it works just fine, however if it spawns in for example if my player dies, it wont work. heres the code for the replay button :
import greenfoot.*;
import java.util.List;
public class replay extends Actor
{
    private GreenfootImage replay1 = new GreenfootImage("replay1.png");
    private GreenfootImage replay2 = new GreenfootImage("replay2.png");
    public void act() 
    {
        GreenfootSound buttonMusic = new GreenfootSound("button.mp3");
        Actor checkpoint = getOneIntersectingObject(ckeckpoint.class);
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if(mouse != null)
        {
            setImage(replay1);
            List objects = getWorld().getObjectsAt(mouse.getX(),mouse.getY(),replay.class);
            for (Object object : objects)
            {
                if(object == this)
                {
                    setImage(replay2);
                }
                if(Greenfoot.mouseClicked(this))
                {
                    buttonMusic.setVolume(25);
                    buttonMusic.play();
                    Greenfoot.setWorld(new level1());
                }
            }
        }
    }    
}
and heres the code for when its called :
public void act()
    {
        GreenfootSound hitMusic = new GreenfootSound("hit.mp3");
        if(health == 0)
        {
            if(getWorld().getObjects(gameover.class).isEmpty())
            {
                getWorld().addObject(new gameover(),300,200);
            }
            if(getWorld().getObjects(replay.class).isEmpty())
            {
                getWorld().addObject(new replay(),300,350);
            }
            hitMusic.setVolume(50);
            hitMusic.play();
            animateDeath();
            return;
        }
        fall();
        jump();
        move();
        stand();
        shoot();
        hit();
        health();
        counter++;
        Actor portal = getOneIntersectingObject(portal.class);
        if(getY() == getWorld().getHeight()-1)
        {
            if(getWorld().getObjects(gameover.class).isEmpty())
            {
                getWorld().addObject(new gameover(),300,200);
            }
            if(getWorld().getObjects(replay.class).isEmpty())
            {
                getWorld().addObject(new replay(),300,350);
            }
            hitMusic.setVolume(50);
            hitMusic.play();
            getWorld().removeObject(this);
        }
        if(portal != null)
        {
            Greenfoot.setWorld(new level2());
        }
    }
danpost danpost

2019/6/23

#
The code in your replay class can be much simplified. You can make use of the method in the Greenfoot class to determine mouse state. A simple act method could be structured as follows:
public void act()
{
    // gain hover
    if (Greenfoot.mouseMoved(this)) { ... } // set image
    // lose hover
    if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this)) { ... } // set Image
    // clicked
    if (Greenfoot.mouseClicked(this)) { ... } // play button sound and start game
}
No MouseInfo objects; no for loops, no List objects. Create the GreenfootSound object within the if block for when clicked so you do not unnecessarily create one every act step.
AniSusi AniSusi

2019/6/23

#
this behaves just like the other code too tho
danpost danpost

2019/6/23

#
AniSusi wrote...
this behaves just like the other code too tho
Show revised codes.
AniSusi AniSusi

2019/6/23

#
import greenfoot.*;
import java.util.List;
public class replay extends Actor
{
    private GreenfootImage replay1 = new GreenfootImage("replay1.png");
    private GreenfootImage replay2 = new GreenfootImage("replay2.png");
    GreenfootSound buttonMusic = new GreenfootSound("button.mp3");
    public void act()
    {
    if (Greenfoot.mouseMoved(this)) 
    { 
        setImage(replay2);
    }
    if (Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
    {
        setImage(replay1);
    }
    if (Greenfoot.mouseClicked(this)) 
    { 
        buttonMusic.setVolume(25);
        buttonMusic.play();
        Greenfoot.setWorld(new level1());
    }
    }
}
Super_Hippo Super_Hippo

2019/6/23

#
Make sure that no object is on top of the replay object (including transparent image parts). So that the mouse actually clicks/moves on the replay object and not on something else.
AniSusi AniSusi

2019/6/23

#
that doesnt seem to be the problem
danpost danpost

2019/6/23

#
AniSusi wrote...
that doesnt seem to be the problem
Code looks okay; however, there is no need for line 2.
You need to login to post a reply.