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

4 days ago

Please help :)

LoafofBrad LoafofBrad

4 days ago

#
i was looking over my code and for the life of my i cannot get the coin to animate
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Coin here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Coin extends Actor
{
    /**
     * Act - do whatever the Orang wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    GreenfootImage image1 = new GreenfootImage("Coin00.png");
    GreenfootImage image2 = new GreenfootImage("Coin01.png");
    GreenfootImage image3 = new GreenfootImage("Coin02.png");
    GreenfootImage image4 = new GreenfootImage("Coin03.png");
    GreenfootImage image5 = new GreenfootImage("Coin04.png");
    GreenfootImage image6 = new GreenfootImage("Coin05.png");
 
     
    public int frame = 1;
    public int waktu = 0;
    public void act() 
    {
      animasi();
    }    
    public void animasi()
    {
         if (frame == 1)
            {
            if(waktu == 5)
            {
            setImage(image1);
            frame = 2;
            waktu = 0;
            }
        }
         else if (frame == 2)
            {
            if(waktu == 5)
            {
            setImage(image2);
            frame = 3;
            waktu = 0;
            }
        }
         else if (frame == 3)
            {
            if(waktu == 5)
            {
            setImage(image3);
            frame = 1;
            waktu = 0;
            }
        }
            else if (frame == 2)
            {
            if(waktu == 5)
            {
            setImage(image4);
            frame = 3;
            waktu = 0;
            }
        }
            
         else if (frame == 3)
            {
            if(waktu == 5)
            {
            setImage(image5);
            frame = 1;
            waktu = 0;
            }
        }
            else if (frame == 2)
            {
            if(waktu == 5)
            {
            setImage(image6);
            frame = 3;
            waktu = 0;
            }
        }            
    }
    
}
danpost danpost

4 days ago

#
I do not see where the waktu field is being incremented. Two things to help reduce code and make things easier to read: (1) ask about the waktu counter before the frame number; and (2) use an array to hold and control the images; For (1), using modulus operation to loop counter. For (2), use index of array to control frame number. It would look like this:
import greenfoot.*;

public class Coin extends Actor
{
    private GreenfootImage[] images = new GreenfootImage[6];
    private int waktu;
    
    public Coin()
    {
        for (int i=0; i<images.length; i++)
        {
            images[i] = new GreenfootImage("Coin0"+i+".png");
        |
    }
    
    public void act()
    {
        animasi();
    }
    
    public void animasi()
    {
        waktu = (waktu+1)%(images.length*5);
        if (waktu%5 == 0) setImage(images[waktu/5);
    }
}
danpost danpost

4 days ago

#
Even better might be to make the images array static (and public) and load the array from your world constructor. That way the loading of images is done only once and done at the start of the program (not multiple times throughout by way of the Coin constructor).
danpost danpost

4 days ago

#
Oops -- line 24 above should be:
if (waktu%5 == 0) setImage(images[waktu/5]);
(missed the closing square bracket for the index of the array)
You need to login to post a reply.