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

2017/3/20

Characters Vibrate ( I NEED ADVICE ) Also why do i get this Error Message- IOException writing debug log

Jimmywow Jimmywow

2017/3/20

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

2017/3/20

#
It would be great if you could use code tags in the future so it is easier for us to tell you "you have to change line xy". It is more difficult to copy, but easier to read (unless you have to scroll to the side). In NPC, you use %0.5 instead of %4 for the animation. It will always be true, so it changes every act cycle. Change it to a 4 to get the same animation speed as the Jimmy. (You can remove the other discussion.) I sometimes also receive the 'IOException writing debug log' error. Restarting Greenfoot usually fixes that. You create a lot of variables for images, but you use none of them. You can either remove all of them or you can use them instead of creating new images when setting the images. All the animation methods can also be simplified: You can either use:
//remove all GreenfootImages variables

public void animateRight()
{
    setImage("Jimmywr"+frame+".png");
    frame = frame%8+1; //Note that this does not skip the first image as done in your code. Not sure if this was wanted
}
Or:
private GreenfootImage[] runRightImages = 
{
    new GreenfootImage("Jimmywr1.png"),
    new GreenfootImage("Jimmywr2.png"),
    new GreenfootImage("Jimmywr3.png"),
    new GreenfootImage("Jimmywr4.png"),
    new GreenfootImage("Jimmywr5.png"),
    new GreenfootImage("Jimmywr6.png"),
    new GreenfootImage("Jimmywr7.png"),
    new GreenfootImage("Jimmywr8.png")
};

public void animateRight()
{
    //if you change frame=1 to frame=0, you can remove -1 and +1 from the following lines
    setImage(runRightImages[frame-1]);
    frame = frame%8+1;
}
Other than that, I don't see a reason for vibration. Not sure what you mean with 1&2 and 4&6. Are you sure that the 8 images are different and in the correct order? For Jimmy to set back to idle image, you have to check if it is not moving.
public void checkKeys()
{
    if(Greenfoot.isKeyDown("space") && jumping == false)
    {
        jump();
    }
    int dx = 0;
    if(Greenfoot.isKeyDown("d")) dx++;
    if(Greenfoot.isKeyDown("a")) dx--;
    if (dx != 0) //moves
    {
        if (dx==1) moveRight();
        else if (dx==-1) moveLeft();
    }
    else //does not move = either no a or d were pressed or both
    {
        //set first image
    }
}
I hope I didn't miss a question.
Jimmywow Jimmywow

2017/3/20

#
THANK YOU SOO MUCH!!! And.. I have spent most the night trying to add a Scrolling world by watching Jim Stewarts Tutorials but he doesnt have exactly what i want, which is for it just to move left and right when my character (Jimmy) Go's Near where i have to drag it at the bottom. My world is 2000-720 in one world and 4000-720 in the other
Jimmywow Jimmywow

2017/3/20

#
Also, im sorry but I don't quite understand what you mean by "It would be great if you could use code tags in the future so it is easier for us to tell you "you have to change line xy". It is more difficult to copy, but easier to read (unless you have to scroll to the side"
Super_Hippo Super_Hippo

2017/3/20

#
Follow that link: https://www.greenfoot.org/doc/code-tags For scrolling worlds, try to use the search option above. There are a few around here on the website which you can download.
You need to login to post a reply.