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

2024/4/29

Im trying to make a platformer and when i press start my chracter flies to the top of the screen pls help

Pls_help Pls_help

2024/4/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RobinsonStill here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RobinsonStill extends Actor
{
    public int vSpeed;
    public int gravity = 2;
    
    public boolean jumping;
    public int jumpStregth = 20;
    
    public int speed = 5;
    
    
    /**
     * Act - do whatever the RobinsonStill wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         checkFall();
         checkKeys();
    }
    
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("Up") && jumping == false)
        {
        jump();
    }
    if(Greenfoot.isKeyDown("right"))
    {
        setLocation(getX() + speed, getY());
    }
    /**
    
    if(Greenfoot.isKeyDown("left"))
    {
        setLocation(getY() + speed, getX());
    }
    */
}
    public RobinsonStill()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/6;
        int myNewWidth = (int)myImage.getWidth()/6;
        myImage.scale(myNewWidth, myNewHeight);
    }
    public void checkFall()
    {
        if(onGround() == true)
            vSpeed = 0;
        else
            fall();
    }
    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        
        if(vSpeed <= 12)
        vSpeed = vSpeed + gravity;
        
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = spriteHeight/2;
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Floor.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())/2;
        
        setLocation(getX(), newY);
        jumping = false;
    }
    public void jump()
    {
        vSpeed = vSpeed - jumpStregth;
        jumping = true;
        fall();
    }
}
danpost danpost

2024/4/29

#
Pls_help wrote...
Im trying to make a platformer and when i press start my chracter flies to the top of the screen pls help << Code Omitted >>
Might be that line 98 is making the character fly up. Make line 98 as follows:
vSpeed = -jumpStregth;
Pls_help Pls_help

2024/4/29

#
I just changed the code on line 98 and nothing had changed the character is still flying up to the top of the screen
danpost danpost

2024/4/30

#
Pls_help wrote...
I just changed the code on line 98 and nothing had changed the character is still flying up to the top of the screen
I did not see anything apparent in your code that would cause the actor to fly straight up upon starting the scenario. Maybe its the image. Try inserting the following as the last line in the constructor (after line 53):
setImage(myImage);
Try lowering the jump strength and see if that doesn't show any differences. I doubt it will, but good to double check. Other possibilities might be that some other object, either the world or some other actor, is moving it or I totally missed it.
Pls_help Pls_help

2024/5/7

#
I finally found the problem. Thanks.
You need to login to post a reply.