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.
Frame 1 stuff:
frame 2 stuff:
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;
}
}
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);
}
} 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;
}
