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

2013/4/30

I need help :/

bob10123 bob10123

2013/4/30

#
Well, once again, I am making a platformer and the character falls halfway through the blocks that he lands on, depending on the speed and height he lands from... any ideas? If the classes that I am using won't work, I am open to use another player class entirely... thanks
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
Code for the player class: 
/**
 * 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());
            }
            
        }
    }
        
    
    }

        
    
code for the P1 subclass of player:
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());    
            
        
    }
    
    
}
    
    
danpost danpost

2013/4/30

#
It appears you do not yet have any code detecting the moment when it falls onto the ground or a platform; which is needed to be able to 'set' your player on the platform (or ground) properly (so it is ON the ground or platform and not IN the ground or platform.
bob10123 bob10123

2013/4/30

#
So... would it just be adding a couple booleans? And where would I put them?
danpost danpost

2013/4/30

#
It would go right after the code that makes the object move vertically. If the object is moving downward and is now (after moving) intersecting a platform or the ground, then re-location the actor on top on the object.
bob10123 bob10123

2013/5/1

#
Ok. How would I write the location for reverting the character to the top of the blocks?
bob10123 bob10123

2013/5/1

#
Sorry, still learning java.
danpost danpost

2013/5/1

#
Use 'setLocation' using half the height of the actor's image, plus half the height of the object it is to be set on. Use that distance in relation to the y-location of the object to be set on.
bob10123 bob10123

2013/5/1

#
So I added this line of code to the player class, and now I slowly fall through every block.
}else if (onGround() && JPower > 0 && keyPressed == 1)
            {
                JPower = 10;
                VSpeed = 0;
                keyPressed = 0;
                setLocation(getX(), getY()+halfHeight +halfHeight);
            }
danpost danpost

2013/5/1

#
I realize it is probably wasteful, but you will need to again get a reference to the object that 'onGround' determined your actor was on. Then, you need to use its location and subtract half of its height and half of your actors height from the y value of its location to determine where to place your actor.
bob10123 bob10123

2013/5/1

#
Ok, I did what you said and am now getting this error
java.lang.NullPointerException
	at Player.Action(Player.java:89)
	at P1.act(P1.java:23)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
davmac davmac

2013/5/1

#
at Player.Action(Player.java:89)
This error usually means you have a null value in a variable and you are trying to call a method through that variable. If you need more help please show the code you have for at that line (Player class line 89) and the surrounding method.
bob10123 bob10123

2013/5/1

#
Ok well I am not getting that error anymore, but I am still getting the problem where my person falls halfway through the ground and wall depending on the speed.
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;
    //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;
    }
    
    public boolean onGround()
    {
        Actor actor = getOneObjectAtOffset(0, halfWidth, Ground.class);
        return actor != null;
    }
    
    public boolean inGround()
    {
        Actor actor = getOneObjectAtOffset(0, eighthWidth, Ground.class);
        return actor != null;
    }
    
    public boolean inWall()
    {
        Actor actor = getOneObjectAtOffset(0, 0, 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 (onGround() || onWall()){
                JPower = 10;
                VSpeed = 0;
                keyPressed = 0;
            }else{
                setLocation(getX(), getY()-VSpeed);
                VSpeed--;
            }
            if(inGround())
            {
                Actor ground = getOneObjectAtOffset(0, eighthWidth, Ground.class);
                setLocation(getX(), getY()-ground.getImage().getHeight());
            }
            if(inWall())
            {
                Actor wall = getOneObjectAtOffset(0, 0, Wall.class);
                setLocation(getX(), getY()-wall.getImage().getHeight());
            }
            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(inGround())
            {
                Actor ground = getOneObjectAtOffset(0, eighthWidth, Ground.class);
                setLocation(getX(), getY()-ground.getImage().getHeight());
            }
        }
        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());
            }
            if(inGround())
            {
                Actor ground = getOneObjectAtOffset(0, eighthWidth, Ground.class);
                setLocation(getX(), getY()-ground.getImage().getHeight());
            }
        }
    }
}
You need to login to post a reply.