Ok, so all my animations work but when my sprite is on the Ground.class it sometime will just vibrate in place i have a suspect that its to do with some of the frames.. ( I am making a 2D Platformer) i also Really need to know how to make it so if i am not pressing a key it will set it back to a default image so that his feet are not in the air. Here is the Animation code
public void animateRight()
{
if(frame == 1)
{
setImage("Jimmywr1.png");
}
else if(frame == 2)
{
setImage("Jimmywr2.png");
}
else if(frame == 3)
{
setImage("Jimmywr3.png");
}
else if(frame == 4)
{
setImage("Jimmywr4.png");
}
else if(frame == 5)
{
setImage("Jimmywr5.png");
}
else if(frame == 6)
{
setImage("Jimmywr6.png");
}
else if(frame == 7)
{
setImage("Jimmywr7.png");
}
else if(frame == 8)
{
setImage("Jimmywr8.png");
frame= 1;
return;
}
frame ++;
}
That's the Right animation ( We will just focus on this set of images so that I can do the same on the rest)
Nvm here is just all the code maybe someone smart (Danpost) And others will be able to help me :D ( Note- I would put comments so that it would be easier to understand but i think it isnt too hard to understand now) PLEASE HELP! thanks...
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Jimmy extends Actor
{
private int vSpeed = 0;
private int acceleration = 1;
private boolean jumping;
private int jumpStrength = 15;
private int speed = 5;
private GreenfootImage run1 = new GreenfootImage("Jimmywr1.png");
private GreenfootImage run2 = new GreenfootImage("Jimmywr2.png");
private GreenfootImage run3 = new GreenfootImage("Jimmywr3.png");
private GreenfootImage run4 = new GreenfootImage("Jimmywr4.png");
private GreenfootImage run5 = new GreenfootImage("Jimmywr5.png");
private GreenfootImage run6 = new GreenfootImage("Jimmywr6.png");
private GreenfootImage run7 = new GreenfootImage("Jimmywr7.png");
private GreenfootImage run8 = new GreenfootImage("Jimmywr8.png");
private GreenfootImage lrun1 = new GreenfootImage("jimmywl1.png");
private GreenfootImage lrun2 = new GreenfootImage("jimmywl2.png");
private GreenfootImage lrun3 = new GreenfootImage("jimmywl3.png");
private GreenfootImage lrun4 = new GreenfootImage("jimmywl4.png");
private GreenfootImage lrun5 = new GreenfootImage("jimmywl5.png");
private GreenfootImage lrun6 = new GreenfootImage("jimmywl6.png");
private GreenfootImage lrun7 = new GreenfootImage("jimmywl7.png");
private GreenfootImage lrun8 = new GreenfootImage("jimmywl8.png");
private int frame = 1;
private int animationCounter = 0;
/**
* Act - do whatever the Jimmy wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkKeys();
checkFall();
animationCounter ++;
platformAbove();
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("space") && jumping == false)
{
jump();
}
if(Greenfoot.isKeyDown("d"))
{
moveRight();
}
if(Greenfoot.isKeyDown("a"))
{
moveLeft();
}
}
public void moveRight()
{
setLocation(getX()+speed, getY());
if(animationCounter % 4 == 0)
{
animateRight();
}
}
public void moveLeft()
{
setLocation(getX()-speed, getY());
if(animationCounter % 4 == 0)
{
animateLeft();
}
}
public void animateRight()
{
if(frame == 1)
{
setImage("Jimmywr1.png");
}
else if(frame == 2)
{
setImage("Jimmywr2.png");
}
else if(frame == 3)
{
setImage("Jimmywr3.png");
}
else if(frame == 4)
{
setImage("Jimmywr4.png");
}
else if(frame == 5)
{
setImage("Jimmywr5.png");
}
else if(frame == 6)
{
setImage("Jimmywr6.png");
}
else if(frame == 7)
{
setImage("Jimmywr7.png");
}
else if(frame == 8)
{
setImage("Jimmywr8.png");
frame= 1;
return;
}
frame ++;
}
public void animateLeft()
{
if(frame == 1)
{
setImage("jimmywl1.png");
}
else if(frame == 2)
{
setImage("jimmywl2.png");
}
else if(frame == 3)
{
setImage("jimmywl3.png");
}
else if(frame == 4)
{
setImage("jimmywl4.png");
}
else if(frame == 5)
{
setImage("jimmywl5.png");
}
else if(frame == 6)
{
setImage("jimmywl6.png");
}
else if(frame == 7)
{
setImage("jimmywl7.png");
}
else if(frame == 8)
{
setImage("jimmywl8.png");
frame= 1;
return;
}
frame ++;
}
// Falls 2 Pixels down (vSpeed = Falling Speed)
public void fall()
{
setLocation(getX(), getY() +vSpeed);
if(vSpeed <=9)
{
vSpeed = vSpeed + acceleration;
}
jumping = true;
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight/2) + 5;
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
if(ground == null)
{
jumping = true;
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public boolean platformAbove()
{
int spriteHeight = getImage().getHeight();
int lookForGround = (int)(spriteHeight/ -2);
Actor ceiling = getOneObjectAtOffset(0, lookForGround, Platform.class);
if(ceiling != null)
{
vSpeed =1;
bopHead(ceiling);
return true;
}
else
{
return false;
}
}
public void bopHead(Actor ceiling)
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
jumping = false;
}
public void checkFall()
{
if(onGround())
{
vSpeed =0;
}
else
{
fall();
}
}
public void jump()
{
vSpeed = vSpeed - jumpStrength;
jumping = true;
fall();
}
}
Ok so with my Previous Problem i cant see anything that would be wrong the images seem pretty spot on... here is an image of the transparency
Note - 1 & 2 Vibrate, 4&6 Don't and if you really need i can send a video displaying the problem
http://imgur.com/a/rvv45
Please help :D
I guess another way we could fix this is if the w key is released or not pressed it sets jimmy back to his 1st frame or something and that will also fix his legs in the air problem because when it go's through the animation frames it will just be stuck on a certain frame and i would rather go back to the beginning frame
ALSO Recent problem i have added an enemy and his animations are way too quick he doesn't vibrate either just the animations goes way to quickly... Please i need help :/ ( Sorry for reposting this again no one replied to my previous Discussion)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class NPC here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class NPC extends BadGuys
{
private int vSpeed = 0;
private int acceleration = 1;
private int speed = 1;
private int spriteHeight = getImage().getHeight();
private int spriteWidth = getImage().getWidth();
private int lookForGroundDistance = (int)spriteHeight /2;
private int lookForEdge = (int)spriteWidth/2;
private GreenfootImage lrun1 = new GreenfootImage("aibotwl1.png");
private GreenfootImage lrun2 = new GreenfootImage("aibotwl2.png");
private GreenfootImage lrun3 = new GreenfootImage("aibotwl3.png");
private GreenfootImage lrun4 = new GreenfootImage("aibotwl4.png");
private GreenfootImage lrun5 = new GreenfootImage("aibotwl5.png");
private GreenfootImage lrun6 = new GreenfootImage("aibotwl6.png");
private GreenfootImage lrun7 = new GreenfootImage("aibotwl7.png");
private GreenfootImage lrun8 = new GreenfootImage("aibotwl8.png");
private GreenfootImage run1 = new GreenfootImage("aibotwr1.png");
private GreenfootImage run2 = new GreenfootImage("aibotwr2.png");
private GreenfootImage run3 = new GreenfootImage("aibotwr3.png");
private GreenfootImage run4 = new GreenfootImage("aibotwr4.png");
private GreenfootImage run5 = new GreenfootImage("aibotwr5.png");
private GreenfootImage run6 = new GreenfootImage("aibotwr6.png");
private GreenfootImage run7 = new GreenfootImage("aibotwr7.png");
private GreenfootImage run8 = new GreenfootImage("aibotwr8.png");
private int frame = 1;
private int animationCount = 0;
/**
* Act - do whatever the NPC wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
checkFall();
killJimmy();
move();
if (speed<0)
{
animateLeft();
}
else
{
animateRight();
}
animationCount ++;
}
public void move()
{
Actor ground = getOneObjectAtOffset(lookForEdge, lookForGroundDistance, Ground.class);
if(ground == null)
{
speed *= -1;
lookForEdge *= -1;
}
else
{
move(speed);
}
}
public void animateLeft()
{
if(frame == 1)
{
setImage("aibotwl1.png");
}
else if(frame == 2)
{
setImage("aibotwl2.png");
}
else if(frame == 3)
{
setImage("aibotwl3.png");
}
else if(frame == 4)
{
setImage("aibotwl4.png");
}
else if(frame == 5)
{
setImage("aibotwl5.png");
}
else if(frame == 6)
{
setImage("aibotwl6.png");
}
else if(frame == 7)
{
setImage("aibotwl7.png");
}
else if(frame == 8)
{
setImage("aibotwl8.png");
frame= 1;
return;
}
frame ++;
}
public void animateRight()
{
if(frame == 1)
{
setImage("aibotwr1.png");
}
else if(frame == 2)
{
setImage("aibotwr2.png");
}
else if(frame == 3)
{
setImage("aibotwr3.png");
}
else if(frame == 4)
{
setImage("aibotwr4.png");
}
else if(frame == 5)
{
setImage("aibotwr5.png");
}
else if(frame == 6)
{
setImage("aibotwr6.png");
}
else if(frame == 7)
{
setImage("aibotwr7.png");
}
else if(frame == 8)
{
setImage("aibotwr8.png");
frame= 1;
return;
}
frame ++;
}
public void moveRight()
{
setLocation(getX()+speed, getY());
if(animationCount % 0.5 == 0)
{
animateRight();
}
}
public void moveLeft()
{
setLocation(getX()-speed, getY());
if(animationCount % 0.5 == 0)
{
animateLeft();
}
}
/**
* Check wither we can see lettuce. If we can, eat it.
*/
void killJimmy()
{
if (canSee(Jimmy.class))
{
eat(Jimmy.class);
}
}
public void fall()
{
setLocation(getX(), getY() +vSpeed);
if(vSpeed <=9)
{
vSpeed = vSpeed + acceleration;
}
}
public void checkFall()
{
if(onGround())
{
vSpeed =0;
}
else
{
fall();
}
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int)(spriteHeight/2) + 5;
Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class);
if(ground == null)
{
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
}