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

2018/3/8

Problems with animation integers

Aura_ Aura_

2018/3/8

#
(entire class code using animation, there's other stuff but it doesn't use animation whatsoever)
import greenfoot.*;

/**
 * Write a description of class Player here.
 * 
 * Hunter L.
 * Version 0.0000001 (considering I completely rushed this)
 * If it seems like I copied code at times, I didn't, just used "Greenfoot Lessons 26-28" on YouTube (Mr. Stewart's lessons)
 */
public class Player extends Actor
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage image9;
    private GreenfootImage image10;
    private GreenfootImage image11;
    private GreenfootImage image12;
    private int jumpHeight = 8;
    private double fallSpeed = 0.4;
    private int speed = 3;
    private int cooldown = 0;
    private boolean inTheAir = false;
    private double deltaX = 0;
    private double deltaY = 0;
    private int groundHeight = getImage().getHeight()/2;
    private int sideWidth = getImage().getWidth()/2;
    private World myWorld;
    private int time = 0;
    private int time2 = 0;
    private int time3 = 0;
    int worldHeight;
    int worldWidth;
    public void addedToWorld(World myWorld)
    {
        this.myWorld = myWorld;
        this.worldHeight = myWorld.getHeight();
        this.worldWidth = myWorld.getWidth();
    }

    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Player()
    {
        image1 = new GreenfootImage("annaidle1.png");
        image2 = new GreenfootImage("annaidle2.png");
        image3 = new GreenfootImage("annaidle3.png");
        image4 = new GreenfootImage("annaidle4.png");
        image5 = new GreenfootImage("annaleft1.png");
        image6 = new GreenfootImage("annaleft2.png");
        image7 = new GreenfootImage("annaleft3.png");
        image8 = new GreenfootImage("annaleft4.png");
        image9 = new GreenfootImage("annaright1.png");
        image10 = new GreenfootImage("annaright2.png");
        image11 = new GreenfootImage("annaright3.png");
        image12 = new GreenfootImage("annaright4.png");
        setImage(image1);
    }

    public void act() 
    {
        if (!Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
        {
            !time++;
        }
        if (getImage() == image1 && time > 12 && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
        {
            setImage(image2);
        } else if (getImage() == image2 && time > 24 && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
        {
            setImage(image3);
        } else if (getImage() == image3 && time > 36 && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
        {
            setImage(image4);
        } else if (getImage() == image4 && time > 48 && !Greenfoot.isKeyDown("a") && !Greenfoot.isKeyDown("d"))
        {
            setImage(image1);
            time = 0;
        }
        if (Greenfoot.isKeyDown("a"))
        {
            setImage(image5);
            time2++;
        } else if (Greenfoot.isKeyDown("a") && time2 > 8 && getImage() == image5)
        {
            setImage(image6);
        } else if (Greenfoot.isKeyDown("a") && time2 > 16 && getImage() == image6)
        {
            setImage(image7);
        } else if (Greenfoot.isKeyDown("a") && time2 > 24 && getImage() == image7)
        {
            setImage(image8);
        } else if (Greenfoot.isKeyDown("a") && time2 > 32 && getImage() == image8)
        {
            setImage(image5);
            time2 = 0;
        }
I need to make it so that a time integer is going when I'm not holding A or D, and when I am, the time integer stops and time2 or time3 respectively start. It says that "!time++;" is not a statement. I don't have much time left, so any help is appreciated! Thank you! (If I try to change !time++ to just time++, it freezes the animation as soon as I hit A or D, and when I let go, it keeps it frozen.)
Super_Hippo Super_Hippo

2018/3/8

#
What do you think !time++ could do? ! is used to change (a boolean, ) true to false or false to true, ++ is used to increase a variable (for example int) by one. It is not clever to check for keypresses so often. Check it once and then nest the ifs. In the second part, it can't work at all. The conditions in lines 91, 94, 97 and 100 will never be met because they are only checked when the condition in 87 is not met which is part of the condition in the other lines. Animations are easier to handle when you have the images in arrays and simply increase a variable used to hold the current index.
You need to login to post a reply.