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

2014/8/10

Unwanted bouncing

roeilthegreat roeilthegreat

2014/8/10

#
I finally got this actor to jump (actually, I copied a part of danpost's platformer scenario, sorry.), but there seems to be some problems. The running animation is too fast, and when I jump once, the character passes the ground. Also, most of the time, when I press "right", my actor either stops, or will do nothing. It rarely punches as it's supposed to. I believe it has something to do with the "frame" variable, but I'm not sure how to tweak it. All help would be appreciated. Thanks in advance, EDIT: I found the cause of the bouncing, the fast animation ground passing is still there though. Here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Start here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Start extends Player
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
    
    private GreenfootImage slide = new GreenfootImage("slide.png");    
    private GreenfootImage punch1 = new GreenfootImage ("punch0.png");
    private GreenfootImage punch2 = new GreenfootImage ("punch2.png");  
    private GreenfootImage punch3 = new GreenfootImage ("punch3.png");  
    private GreenfootImage jump = new GreenfootImage ("victoryjump.png");  
    private GreenfootImage walk1 = new GreenfootImage ("1.png");  
    private GreenfootImage walk2 = new GreenfootImage ("2.png");  
    private GreenfootImage walk3 = new GreenfootImage ("3.png");  
    private GreenfootImage walk4 = new GreenfootImage ("4.png");  
    private GreenfootImage walk5 = new GreenfootImage ("5.png");  
    private GreenfootImage walk6 = new GreenfootImage ("6.png");  
    private GreenfootImage walk7 = new GreenfootImage ("7.png");  
    private GreenfootImage walk8 = new GreenfootImage ("8.png");  
    private GreenfootImage walk9 = new GreenfootImage ("9.png");  
    private GreenfootImage walk10 = new GreenfootImage ("10.png");
    private int frame = 1;
    /**
     * Act - do whatever the Start wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */ 
    public void act()
    {   
        CheckKeys();
        CheckFall();
    }
    
    public void CheckKeys()//detects if player inputs a command.
    {
        if(Greenfoot.isKeyDown("up") && jumping == false)//prevents double jumping
        {
            jump();
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            slide();
        }
        else if(Greenfoot.isKeyDown("right"))
        {
            punch();
        }
        else //runs even when no action is eneterd
        {
           run();
        }
    }
    
    public void slide()
        {
          setImage (slide);
        }
        
    public void punch()
    {
        if(frame == 1)
        {
            setImage(punch1);
        }
        else if(frame == 2)
        {
            setImage(punch2);
        }
        else if(frame == 3)
        {
            setImage(punch3);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void run()
    {
        if(frame == 1)
        {
            setImage(walk1);
        }
        else if(frame == 2)
        {
            setImage(walk2);
        }
        else if(frame == 3)
        {
            setImage(walk3);
        }
         else if(frame == 4)
        {
            setImage(walk4);
        }
         else if(frame == 5)
        {
            setImage(walk5);
        }
         else if(frame == 6)
        {
            setImage(walk6);
        }
         else if(frame == 7)
        {
            setImage(walk7);
        }
         else if(frame == 8)
        {
            setImage(walk8);
        }
         else if(frame == 9)
        {
            setImage(walk9);
        }
        else if(frame == 10)
        {
            setImage(walk10);
            frame = 1;
            return;
        }
        frame++;
    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
    }
    
    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/4) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/4, Ground.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }

    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/4;
        setLocation(getX(), newY);
        jumping = false;
    }

    public void CheckFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    } 
}
        
danpost danpost

2014/8/10

#
I do not see where you copied my code. What part and which scenario did you get it from?
danpost danpost

2014/8/10

#
Change line 77 to
else if (frame > 2)
Your 'run' animation could have the value end up greater than 3 and the 'punch' animation was not written to account for values greater than 3.
roeilthegreat roeilthegreat

2014/8/11

#
My bad. The jumping and formula and animation was actually from askgriff's scenario: http://www.greenfoot.org/scenarios/10593. The worked great, and although I'm still trying to figure out how to stop my character from falling through the ground, I managed to fix the fast animation by increasing the frame numbers on lines: 69,73,88, and so on. Thanks for the help. :D
danpost danpost

2014/8/13

#
One thing that may be causing passage through ground objects is that when 'jump' is called 'fall' is executed; this is when you call 'checkKeys' from the 'act' method. However, you are calling 'fall' << again >> when you call 'checkFall' from the 'act' method. This will make your actor move double-speed when first leaving the ground.
You need to login to post a reply.