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

2013/4/27

Could someone help me?

bob10123 bob10123

2013/4/27

#
I am trying to make a simple platformer but I can not seem to get the gravity code right. Everything works, but the player falls halfway through the ground and walls, depending on how hard they collide with each other. This is what I want it to look like: and this is what sometimes happens: The code for the player class is here:
import greenfoot.*;

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public int JPower = 10;
    public int VSpeed = 0;
    public int Drag = VSpeed - 2;
    public int keyPressed = 0;
    public int bounce = 0;
    public int eighthWidth = getImage().getWidth()/8;
    public int quarterWidth = getImage().getWidth()/4;
    public int halfWidth = getImage().getWidth()/2;
    public double nextLevel;
    public int shootdelay=0;
    public int mindelay=5;
    //set JPower
    public void JPower(int Power)
    {
        JPower = Power;
    }
    //checks if chacracter is on the ground
    public boolean touchingWallL()
    {
        Actor actor = getOneObjectAtOffset(0-quarterWidth, 0, Wall.class);
        return actor != null;
    }
    
    public boolean touchingWallR()
    {
        Actor actor = getOneObjectAtOffset(quarterWidth, 0, Wall.class);
        return actor != null;
    }
    
    
    public boolean onWall()
    {
        Actor actor = getOneObjectAtOffset(0, halfWidth, Wall.class);
        return actor != null;
    }
    
    public boolean belowWall()
    {
        Actor actor = getOneObjectAtOffset(0, 0-halfWidth, Wall.class);
        return actor != null;
    }
    
   
    
    
    /**Listed actions so far are:jump/fall, moveL, moveR, boxPhysics
     */
    public void Action(String Action, String Char, String Key, String Key2, int AnimationFrames, int Speed)
    {
        if(Action == "jump/fall")
        {
            if(Greenfoot.isKeyDown(Key))
            {
                keyPressed = 1;
            }
            if( JPower > 0 && keyPressed == 1 && !belowWall())
            {
                setLocation(getX(), getY()-JPower);
                JPower--;
            }else if (onWall()){
                JPower = 10;
                VSpeed = 0;
                keyPressed = 0;
            }else{
                setLocation(getX(), getY()-VSpeed);
                VSpeed--;
            }
            
            if(belowWall())
            {
                JPower = 0;
            }
        }
        if(Action == "moveL")
        {
            if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallL())
            {
                setLocation(getX()-Speed*2, getY());
            }else if(Greenfoot.isKeyDown(Key) && !touchingWallL())
            {
                setLocation(getX()-Speed, getY());
            }
            
        }
        if(Action == "moveR")
        {
            if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallR())
            {
                setLocation(getX()+Speed*2, getY());
            }else if(Greenfoot.isKeyDown(Key) && !touchingWallR())
            {
                setLocation(getX()+Speed, getY());
            }
            
        }
    }
        
    
    }
And the code for the subclass of the player, the P1 is here:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class P1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class P1 extends Player
{
    private int level;
    public int shootdelay = 0;
    public int mindelay = 5;
    private Bullet bullet;
    /**
     * Act - do whatever the P1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    
    public void act() 
    {
        Action("jump/fall", " ", "up", " ", 10, 0);
        Action("moveL", " ", "left", "shift", 10, 3);
        Action("moveR", " ", "right", "shift", 10, 3);
        Gun gun = new Gun();
        
        if (Greenfoot.mouseClicked(null))            
        shoot();
        Actor P1 = getOneIntersectingObject(Enemy.class); 
        if (P1 != null)
        {
            Level5 level5 = new Level5();
            Greenfoot.setWorld(level5);
        }
        Actor actor = getOneIntersectingObject(Enemy6.class);
        if (P1 != null)
        {
            Level6p4 level6p4 = new Level6p4();
            Greenfoot.setWorld(level6p4);
        }
    }
    public void shoot()
    {
        
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int rot = getRotation()-10;
        Bullet b = new Bullet();
        getWorld().addObject(b, getX(), getY());
        b.turnTowards(mouse.getX(),mouse.getY());    
            
        
    }
    
    
}
    
    
Thank you so much for your advice!
The problem is you used half the width of the image for detecting the wall, instead of half the height. Make a variable called "halfHeight = getImage().getHeight()/2" and then replace your aboveWall method to have halfHeight instead of halfWidth.
Also, another thing, if you don't want other classes being able to see your instant variables, you can declare them protected, instead of public. Protected variable/methods basically mean they are private to all classes except the subclasses of the class they were declared in. If you do that, none of the other classes will be able to access/edit your Player instant variables, but subclasses of Player can.
bob10123 bob10123

2013/4/27

#
Yeah that does not seem to change anything... Here is my updated code with your suggestion:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    public int JPower = 10;
    public int VSpeed = 0;
    public int Drag = VSpeed - 2;
    public int keyPressed = 0;
    public int bounce = 0;
    public int eighthWidth = getImage().getWidth()/8;
    public int quarterWidth = getImage().getWidth()/4;
    public int halfWidth = getImage().getWidth()/2;
    public int halfHeight = getImage().getHeight()/2;
    public double nextLevel;
    public int shootdelay=0;
    public int mindelay=5;
    //set JPower
    public void JPower(int Power)
    {
        JPower = Power;
    }
    //checks if chacracter is on the ground
    public boolean touchingWallL()
    {
        Actor actor = getOneObjectAtOffset(0-quarterWidth, 0, Wall.class);
        return actor != null;
    }
    
    public boolean touchingWallR()
    {
        Actor actor = getOneObjectAtOffset(quarterWidth, 0, Wall.class);
        return actor != null;
    }
    
    
    public boolean onWall()
    {
        Actor actor = getOneObjectAtOffset(0, halfHeight, Wall.class);
        return actor != null;
    }
    
    public boolean belowWall()
    {
        Actor actor = getOneObjectAtOffset(0, 0-halfWidth, Wall.class);
        return actor != null;
    }
    
    public boolean onGround()
    {
        Actor actor = getOneObjectAtOffset(0, halfWidth, Ground.class);
        return actor != null;
    }
    
    
    
    
    
    /**Listed actions so far are:jump/fall, moveL, moveR, boxPhysics
     */
    public void Action(String Action, String Char, String Key, String Key2, int AnimationFrames, int Speed)
    {
        if(Action == "jump/fall")
        {
            if(Greenfoot.isKeyDown(Key))
            {
                keyPressed = 1;
            }
            if( JPower > 0 && keyPressed == 1 && !belowWall())
            {
                setLocation(getX(), getY()-JPower);
                JPower--;
            }else if (onGround() || onWall()){
                JPower = 10;
                VSpeed = 0;
                keyPressed = 0;
            }else{
                setLocation(getX(), getY()-VSpeed);
                VSpeed--;
            }
            
            if(belowWall())
            {
                JPower = 0;
            }
        }
        if(Action == "moveL")
        {
            if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallL())
            {
                setLocation(getX()-Speed*2, getY());
            }else if(Greenfoot.isKeyDown(Key) && !touchingWallL())
            {
                setLocation(getX()-Speed, getY());
            }
            
        }
        if(Action == "moveR")
        {
            if(Greenfoot.isKeyDown(Key) && Greenfoot.isKeyDown(Key2) && !touchingWallR())
            {
                setLocation(getX()+Speed*2, getY());
            }else if(Greenfoot.isKeyDown(Key) && !touchingWallR())
            {
                setLocation(getX()+Speed, getY());
            }
            
        }
    }
        
    
    }

        
    
Whoops, my bad, I meant onGround(), not on wall... but if your onWall() method works the same as your onGround method, just looking for walls. So you need to replace halfWidth in onWall, onGround, and belowWall with halfHeight
OxiClean OxiClean

2013/4/27

#
"Still doesn't work" - Bob
You need to login to post a reply.