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

2017/3/18

ANIMATION- Please help

Jimmywow Jimmywow

2017/3/18

#
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(); } }
danpost danpost

2017/3/18

#
Without delving too deeply into your code, it appears that the 'moveToGround' method will set your character on the ground. If it is not appearing on the ground when it should be, check your images to see if there is any excess transparencies below the sprite that appears on the image that would make the image larger (taller) and produce a gap between the ground and the sprite itself.
Jimmywow Jimmywow

2017/3/19

#
Ok ill try
Jimmywow Jimmywow

2017/3/19

#
Can i list more problems here, or should i make a new Discussion?
Jimmywow Jimmywow

2017/3/19

#
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
Jimmywow Jimmywow

2017/3/19

#
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
You need to login to post a reply.