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

2018/5/14

Variable issue, in desperate need of help :l

Javalicious Javalicious

2018/5/14

#
Okay, so i'm trying to make a little dude that walks around with a 2 picture walking animation, but i cant seem to get it to do the second frame, i think it has something to do with the frame variable, I need a short easy to explain answer, not anything over the top or unnecessary, just something enough to fix this :l here's all the code for the actor, ill place the two parts for frame detection and stuff under it.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bread here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bread extends Actor
{
    /**
     * Act - do whatever the Bread wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public int frame = 0;
    
    public void act() 
    {
        frame += 1;
        
        GreenfootImage IdleDown = new GreenfootImage("sprite_00.png");
        GreenfootImage IdleRight = new GreenfootImage("sprite_04.png");
        GreenfootImage IdleUp = new GreenfootImage("sprite_05.png");
        GreenfootImage IdleLeft = new GreenfootImage("sprite_08.png");
        
        GreenfootImage WalkingDown1 = new GreenfootImage("sprite_01.png");
        GreenfootImage WalkingRight1 = new GreenfootImage("sprite_03.png");
        GreenfootImage WalkingUp1 = new GreenfootImage("sprite_06.png");
        GreenfootImage WalkingLeft1 = new GreenfootImage("sprite_09.png");
        
        GreenfootImage WalkingDown2 = new GreenfootImage("sprite_02.png");
        GreenfootImage WalkingRight2 = new GreenfootImage("sprite_04.png");
        GreenfootImage WalkingUp2 = new GreenfootImage("sprite_07.png");
        GreenfootImage WalkingLeft2 = new GreenfootImage("sprite_08.png");
        
        
        
        int XMotion = 0;
        int YMotion = 0;
        int motion = 0;
        String direction = "down";
        boolean moving = false;
        moving = false;
        
        if (frame > 10)
        {
           frame = 1;
        }
       if (Greenfoot.isKeyDown("w"))
       {
          YMotion = -3;
          motion = 1;
          direction = "up";
          moving = true;
        }
               if (Greenfoot.isKeyDown("s"))
       {
           YMotion = 3;
           motion = 1;
           direction = "down";
           moving = true;
        }
               if (Greenfoot.isKeyDown("d"))
       {
           XMotion = 3;
           motion = 1;
           direction = "right";
           moving = true;
        }
               if (Greenfoot.isKeyDown("a"))
       {
           XMotion = -3;
           motion = 1;
          direction = "left";
           moving = true;
        }
        
        
               if (Greenfoot.isKeyDown("up"))
       {
          //setRotation(-90);
          direction = "up";
        }
               if (Greenfoot.isKeyDown("right"))
       {
           //setRotation(0);
           direction = "right";
        }
               if (Greenfoot.isKeyDown("down"))
       {
           //setRotation(90);
           direction = "down";
        }
               if (Greenfoot.isKeyDown("left"))
       {
           //setRotation(180);
           direction = "left";
        }
        
        if (motion == 0)
        {
        if (direction == "up")
        {
            this.setImage(IdleUp);
        }
        else if(direction == "right")
        {
           this.setImage(IdleRight);
        }
        else if (direction == "down")
        {
            this.setImage(IdleDown);
        }
        else if (direction == "left")
        {
            this.setImage(IdleLeft);
        }
        else
        {
            this.setImage(IdleDown);
        }
        //frame = 0;
    
        }
        
        
                if (motion != 0)
        {
            if (frame < 5)
            {
        if (direction == "up")
        {
            this.setImage(WalkingUp1);
        }
        else if (direction == "right")
        {
           this.setImage(WalkingRight1);
        }
        else if (direction == "down")
        {
            this.setImage(WalkingDown1);
        }
        else if (direction == "left")
        {
            this.setImage(WalkingLeft1);
        }
        else
        {
            //this.setImage(IdleDown);
        }
    }
        //frame = 0;
        }
        
                        if (motion != 0)
        {
            if (frame > 5)
            {
        if (direction == "up")
        {
            this.setImage(WalkingUp2);
        }
        else if (direction == "right")
        {
           this.setImage(WalkingRight2);
        }
        else if (direction == "down")
        {
            this.setImage(WalkingDown2);
        }
        else if (direction == "left")
        {
            this.setImage(WalkingLeft2);
        }
        else
        {
            //this.setImage(IdleDown);
        }
    }
        //frame = 0;
        }
        
        setLocation(getX() + XMotion,getY() + YMotion);
        ((MyWorld)getWorld()).shareX(getX());
        ((MyWorld)getWorld()).shareY(getY());
       Actor item;
       item = getOneObjectAtOffset(0,0,thing.class);
       if (item != null)
       {
         getWorld().removeObject(item);  
        }
       // frame =+ 1;
    }    
}
Frame 1 stuff:
                if (motion != 0)
        {
            if (frame < 5)
            {
        if (direction == "up")
        {
            this.setImage(WalkingUp1);
        }
        else if (direction == "right")
        {
           this.setImage(WalkingRight1);
        }
        else if (direction == "down")
        {
            this.setImage(WalkingDown1);
        }
        else if (direction == "left")
        {
            this.setImage(WalkingLeft1);
        }
        else
        {
            //this.setImage(IdleDown);
        }
    }
frame 2 stuff:
                        if (motion != 0)
        {
            if (frame > 5)
            {
        if (direction == "up")
        {
            this.setImage(WalkingUp2);
        }
        else if (direction == "right")
        {
           this.setImage(WalkingRight2);
        }
        else if (direction == "down")
        {
            this.setImage(WalkingDown2);
        }
        else if (direction == "left")
        {
            this.setImage(WalkingLeft2);
        }
        else
        {
            //this.setImage(IdleDown);
        }
    }
        //frame = 0;
        }
Javalicious Javalicious

2018/5/14

#
NEVERMIND, just started working out of nowhere
danpost danpost

2018/5/14

#
Javalicious wrote...
i'm trying to make a little dude that walks around with a 2 picture walking animation, but i cant seem to get it to do the second frame
You are creating new images every act cycle. So, first thing you should do is move, at minimum, lines 22 through 35 to outside the act method. Next thing I noticed was that you have 6 different variable for movement, which is way overboard; but, I won't get into that right now (as you just want a quick fix). Line 42 should probably be moved outside the act method, so if no keys are currently pressed, the last movement direction will be available (while inside the act method, it is re-declared every act step and initialized to "down"). Hopefully, that will help.
You need to login to post a reply.