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

2019/1/28

Help with this code.

WhiteSorcerer03 WhiteSorcerer03

2019/1/28

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

/**
 * Write a description of class Ditto here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Boy extends Actor
{   
    private GreenfootImage image1;
    private GreenfootImage image2;
    private GreenfootImage image3;
    private GreenfootImage image4;
    private GreenfootImage image5;
    private GreenfootImage image6;
    private GreenfootImage image7;
    private GreenfootImage image8;
    private GreenfootImage idle;
    private int vSpeed = 0;
    private int acceleration = 1;
    private int frame;
    private boolean isKeyPressed;
    private boolean walking;
    private boolean facingRight;
    private boolean onGround;
    public void Boy()
    {  
      image1 = new GreenfootImage("Walk Right 1.png");
      image2 = new GreenfootImage("Walk Right 2.png");
      image3 = new GreenfootImage("Walk Right 3.png");
      image4 = new GreenfootImage("Walk Right 4.png");
      image5 = new GreenfootImage("Walk Left 1.png");
      image6 = new GreenfootImage("Walk Left 2.png");
      image7 = new GreenfootImage("Walk Left 3.png");
      image8 = new GreenfootImage("Walk Left 4.png");
      setImage(idle);
      walking = false;
      facingRight = true;
     
      
    
    }
    public void walkLeft()
    {
        int delay = 4;
        walking = true;
        facingRight = false;
        frame ++;
        if(frame < 1 * delay)
        {
            setImage(image5);
        }
        else if(frame < 3 * delay)
        {
            setImage(image6);
        }
        else if(frame < 5 * delay)
        {
            setImage(image7);
        }
        else if (frame < 7 * delay)
        {
            setImage(image8);
            frame = 1;
            return;
        }
    }
    public void walkRight()
    {
        int delay = 4;
        walking = true;
        facingRight = true;
        frame ++;
        if(frame < 1 * delay)
        {
            setImage(image1);
        }
        else if(frame < 3 * delay)
        {
            setImage(image2);
        }
        else if(frame < 5 * delay)
        {
            setImage(image3);
        }
        else if (frame < 7 * delay)
        {
            setImage(image4);
            frame = 1;
            return;
        }
    }
    public void checkKeys() { //checks different keys and performs the called actions
        isKeyPressed = false;
        if (Greenfoot.isKeyDown("d") && Greenfoot.isKeyDown("a"))
        {
            stopWalking();
            isKeyPressed = true;
        }
        else if (Greenfoot.isKeyDown("d") && getOneObjectAtOffset( -25, 0, Ground.class ) == null) {
            walkRight();
            setLocation (getX()-8, getY());
            isKeyPressed = true;
        }
        else if (Greenfoot.isKeyDown("a") && getOneObjectAtOffset( 25, 0, Ground.class ) == null) {
            walkLeft();
             setLocation (getX()+8, getY());
            isKeyPressed = true;
        }
        else if (!(isKeyPressed))
        {
            stopWalking();
        }
    }
    public void stopWalking()
    {
        walking = false;
        if (facingRight)
         setImage (idle);
        else
         setImage (idle);
    }
    public void act() 
    {
      movement();
      checkKeys();
    }
      public void movement(){
        // vertical movement
        vSpeed += acceleration; // gravity
        setLocation(getX(), getY()+vSpeed); // move (fall or jump)
        boolean onGround = false; // for current on ground state
        Actor ground = getOneIntersectingObject(Ground.class); // get ground intersector
        
        if (ground != null){ //if actor is inside ground
            int direction = (int)Math.signum(vSpeed); // get vertical direction
            if (direction == 1) 
            onGround = true; // actor is on ground
            vSpeed = 0; // vertical movement is stopped
            while (isTouching(Ground.class)) setLocation(getX(), getY()-direction); // move off of ground
        }
        if (onGround && Greenfoot.isKeyDown("w")) {
            vSpeed -= 18; // jump
        }
    
    }
}
WhiteSorcerer03 WhiteSorcerer03

2019/1/28

#
My problem is that when I run it. He jumps but doesn't move to the left or the right. I was wondering if you guys could help me out.
danpost danpost

2019/1/28

#
WhiteSorcerer03 wrote...
My problem is that when I run it. He jumps but doesn't move to the left or the right. I was wondering if you guys could help me out.
Right click on the boy and select 'Inspect'. Do the values appear to be okay, or is something off?
WhiteSorcerer03 WhiteSorcerer03

2019/1/29

#
No it seems to be fine. When I run the code. It comes up that line 134 has an error.
danpost danpost

2019/1/29

#
WhiteSorcerer03 wrote...
No it seems to be fine. When I run the code. It comes up that line 134 has an error.
Line 19 declares a variable called idle whose initial (default) value is null. I do not see where you assign an image to it.
WhiteSorcerer03 WhiteSorcerer03

2019/1/30

#
Even if I assign an image to idle it doesn't change anything it still tells me something is wrong with Line 134.
danpost danpost

2019/1/30

#
WhiteSorcerer03 wrote...
Even if I assign an image to idle it doesn't change anything it still tells me something is wrong with Line 134.
Please show the terminal error output you are getting.
WhiteSorcerer03 WhiteSorcerer03

2019/1/30

#
java.lang.NullPointerException* at greenfoot.collision.ibsp.IBSPColChecker.getOneIntersectingObject(IBSPColChecker.java:848) at greenfoot.collision.ColManager.getOneIntersectingObject(ColManager.java:187) at greenfoot.World.getOneIntersectingObject(World.java:775) at greenfoot.Actor.getOneIntersectingObject(Actor.java:959) at Boy.movement(Boy.java:135)* at Boy.act(Boy.java:127)* at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) I've stared the code that is highlighted in red
WhiteSorcerer03 WhiteSorcerer03

2019/1/30

#
Line 135 is Line 134
danpost danpost

2019/1/30

#
Do you have a Ground class?
WhiteSorcerer03 WhiteSorcerer03

2019/1/30

#
yes I do
danpost danpost

2019/1/30

#
WhiteSorcerer03 wrote...
yes I do
I guess that was a silly question. It would not compile if you did not. Inspect the boy after the error occurs. See what the World field has in it. As you might be able to tell, I am running out of ideas (looking more like a bug in greenfoot).
danpost danpost

2019/1/30

#
When I said:
Right click on the boy and select 'Inspect'. Do the values appear to be okay, or is something off?
you responded with
No it seems to be fine.
Did you not see a bunch of null values (for the images)? Would that not seem off?
You need to login to post a reply.