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

2016/9/20

Animating a Button

Yakman3 Yakman3

2016/9/20

#
I'm trying to create a standard "clicker" game. I've gotten most of it done, with a scorecounter, autoclickers for in-game money, etc. But I'm having some problems with animating the button to depress when you click it. It sometimes works, but the animation is very quick and it only works every few clicks. Here's the code I have currently in the "button" class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Button extends Actor
{
    public static int clicks;
    public int imageNumber;
    public int imageNumber2;
    public int imageNumber3;
    private Counter counter;
    public static int x = 1;
    public static int y = 0;
    public static int z = 0;
    public static int a = 0;
    public Button(Counter pointCounter){
    counter = pointCounter;
    
    
    }
    /**
     * Act - do whatever the Button wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mousePressed(this)){
        clicks = clicks + x;
        Greenfoot.playSound("Click2-Sebastian-759472264.wav");
        counter.add(x);
        updateImage2();
       
        
        }
        if(clicks == 50){
        updateImage();
        x = 5;
        clicks = 55;
        }
        if(clicks == 500){
        updateImage();
        x = 25;
        clicks = 525;
        }
        if(clicks == 5000){
        updateImage();
        x = 125;
        clicks = 5125;
        }
        if(clicks == 50000){
        updateImage();
        x = 625;
        clicks = 381;
        }
        if(clicks == 500000){
        updateImage();
        x = 3125;
        clicks = 783;
        }
        if(y >= 0){
        if(z != 2000000){
        z++;
        }
        if(z == 2000000){
        counter.add(y);
        z = 0;
        }
        if(y == 5){
        y = 0;
        }
        }
         if(a != 30){
        a++;
        
        }
        if(a == 30){
        setImage("button" + imageNumber + ".png");
        a = 0;
        }
        
    }    
    public void updateImage(){
    imageNumber++;
    if(imageNumber == 7){
    imageNumber = 0;
    }
    setImage("button" + imageNumber + ".png");
    }
    public void updateImage2(){
    imageNumber2++;
    if(imageNumber2 == 7){
    imageNumber2 = 0;
    }
    setImage("button" + imageNumber + " - Copy.png");
    }
  
}
Also, if it changes anything, the greenfoot "speed" slider is always set to the max, to help the counter catch up when you are adding a lot of points per second. Thanks in advance!
danpost danpost

2016/9/21

#
The problem is that 'Greenfoot.mousePressed' only returns true on the act cycles where the mouse button changes from an "up" state to a "down" state. Using the 'Greenfoot.mouseDragged' method will not help either, as it only returns true when the location of the mouse changes after pressing the mouse on some object. What you need to do is to track the state of the button:
// instance field
private boolean mouseDown;
Then, in the act method (before anything else), check for updates on the value of the field:
if (!mouseDown && Greenfoot.mousePressed(this)) mouseDown = true;
if (mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
With the tracking now in place, you can control your animation by using the value of the 'mouseDown' field.
Yakman3 Yakman3

2016/9/22

#
Okay, I put that in, but it doesn't animate. Here's the code I added, at the top of the act() method. (I also added the mouseDown boolean variable.)
if(!mouseDown && Greenfoot.mousePressed(this)) mouseDown = true;
        if(mouseDown && Greenfoot.mouseClicked(null)) mouseDown = false;
        if(mouseDown = true){
        setImage("button" + imageNumber + ".png"); 
        }
        if(mouseDown = false){
        setImage("button" + imageNumber + " - Copy.png");    
        }
danpost danpost

2016/9/22

#
Lines 3 and 6 are not comparing the left and right sides of the 'if' "condition" -- you have assignment statements for conditions. They are being excepted as a condition because the resultant value is still a boolean (true/false) one. You need to use double equal signs to compare the two sides:
// line 3
if (mouseDown == true)
// or simply
if (mouseDown)

// and line 6
if (mouseDown == false)
// or simply
if (!mouseDown)
Yakman3 Yakman3

2016/9/22

#
That worked perfectly! Thanks so much!
You need to login to post a reply.