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

2020/11/23

Gif image question

1
2
cjz19931031 cjz19931031

2020/11/23

#
I have a question about Gif images. I'm trying to make game, there is a thing that when I click the mouse, then the Gif image starts moving. Here is my code, when i click mouse, the image isn't moving. public void act() { if(Greenfoot.mouseClicked(this)) { setImage(img.getCurrentImage()); } }
danpost danpost

2020/11/23

#
With that code, the image will update at the time of the click. That is, mouseClicked is only true when a mouse button is released. You will need a way to determine whether a mouse button has (past tense) been released or not. One of your fields needs to be appointed that duty. You already have an img field. It could be set to null until the mouse is clicked, at which time you can assign the GifImage object to it. With that, a separate if statement can say "if img is not null, set current gif image".
cjz19931031 cjz19931031

2020/11/25

#
I updated my code, but the image doesn't move, are there any error of my codes?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lose1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lose1 extends Actor
{
    /**
     * Act - do whatever the Lose1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GifImage img;
    public static GreenfootSound bkg;
    PauseButton p = new PauseButton();

    public void act() 
    {
       
        //img.pause();
        if(Greenfoot.mouseClicked(this))
        {   img = new GifImage("lose1.gif");
            setImage(img.getCurrentImage()); 
            
        }

        /*else if(Greenfoot.mouseClicked(PauseButton.class))
        {
        PauseImage();
        }*/
    }


}
danpost danpost

2020/11/25

#
cjz19931031 wrote...
I updated my code, but the image doesn't move, are there any error of my codes? << Code Omitted >>
danpost wrote...
a separate if statement can say "if img is not null, set current gif image".
if (img != null) img.setCurrentImage();
else if (Greenfoot.mouseClicked(this)) img = new GifImaage("lose1.gif");
cjz19931031 cjz19931031

2020/11/25

#
For line1, is it (img.getCurrentImage())? I got error if i code (img.setCurrentImage())
danpost danpost

2020/11/25

#
cjz19931031 wrote...
For line1, is it (img.getCurrentImage())? I got error if i code (img.setCurrentImage())
Oh, I guess it is:
if (img != null) setImage(img.getCurrentImage());
cjz19931031 cjz19931031

2020/11/25

#
But it still doesn't work.
danpost danpost

2020/11/25

#
cjz19931031 wrote...
But it still doesn't work.
Do it run when you click the mouse if you change "this" on line 2 to "null"?
cjz19931031 cjz19931031

2020/11/25

#
You mean like this? It still doesn't work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lose1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lose1 extends Actor
{
    /**
     * Act - do whatever the Lose1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GifImage img;
    public static GreenfootSound bkg;
    PauseButton p = new PauseButton();

    public void act() 
    {
       
        if(img != null)
        {
            img.getCurrentImage();
        }
        
        else if(Greenfoot.mouseClicked(null))
        {   img = new GifImage("lose1.gif");
             
            
        }

        /*else if(Greenfoot.mouseClicked(PauseButton.class))
        {
        PauseImage();
        }*/
    }


}
danpost danpost

2020/11/25

#
danpost wrote...
setImage(img.getCurrentImage());
Line 24 was not properly changed.
cjz19931031 cjz19931031

2020/11/25

#
It works. Thank you.I have an other question. When I click pause button, why it won't stop moving and stop playing music?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lose1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lose1 extends Actor
{
    /**
     * Act - do whatever the Lose1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GifImage img;
    public static GreenfootSound bkg;
    PauseButton p = new PauseButton();
    public Lose1()
    {
        bkg = new GreenfootSound("ufo.mp3");  
    }

    public void act() 
    {
        Actor pb = getOneIntersectingObject(PlayButton.class);
        Actor pause = getOneIntersectingObject(PauseButton.class);
        if(img != null)
        {
            setImage(img.getCurrentImage());
        }

        else if(Greenfoot.mouseClicked(pb))
        {   img = new GifImage("lose1.gif");
            bkg.play();

        }

        else if(Greenfoot.mouseClicked(pause))
        {
            img.pause();
            bkg.stop();
        }
    }

}
danpost danpost

2020/11/25

#
Remove "else" from line 32. Place the following condition on line 33:
if (img == null)
cjz19931031 cjz19931031

2020/11/25

#
It seems like doesn't work. And if Click the image, it will start moving and playing music. What i want to do is when I click the play button, then start moving and playing, otherwise nothing happens.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Lose1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Lose1 extends Actor
{
    /**
     * Act - do whatever the Lose1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    private GifImage img;
    public static GreenfootSound bkg;
    PauseButton p = new PauseButton();
    public Lose1()
    {
        bkg = new GreenfootSound("ufo.mp3");  
    }

    public void act() 
    {
        Actor pb = getOneIntersectingObject(PlayButton.class);
        Actor pause = getOneIntersectingObject(PauseButton.class);
        if(img != null)
        {
            setImage(img.getCurrentImage());
        }

        if(Greenfoot.mouseClicked(pb))
        {   
            if(img == null)
            {
                img = new GifImage("lose1.gif");
                bkg.play();
            }
        }

        else if(Greenfoot.mouseClicked(pause))
        {

            img.pause();
            bkg.stop();
        }
    }

}
danpost danpost

2020/11/25

#
Remove lines 35 and 38. The following is a simplified version of the class:
import greenfoot.*;

public class Lose1 extends Actor
{
    private GifImage img;
    public static GreenfootSound bkg;
    
    public Lose1()
    {
        bkg = new GreenfootSound("ufo.mp3");
        img = new GifImage("lose1.gif");
        img.pause();
    }
    
    public void act()
    {
        setImage(img.getCurrentImage());
        if (Greenfoot.mouseClicked(null))
        {
            Actor mouseOn = Greenfoot.getMouseInfo().getActor();
            if (mouseOn == null) return;
            if (mouseOn instanceof PlayButton) bkg.play();
            if (mouseOn instanceof PauseButton) bkg.pause();
        }
    }
}
cjz19931031 cjz19931031

2020/11/25

#
Thank you so much for your help! Now I've done this part. I appreciate it.
There are more replies on the next page.
1
2