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

2014/7/16

Help with bug?

miku622 miku622

2014/7/16

#
I wanted to create a platform game. So I have an Actor ladybug, and my goal is to get it to jump through this platform maze. However, I have two problems. 1. the ladybug twitches up and down a little even when it isn't moving 2. even though i developed the ceiling methods to stop the ladybug when it hits the ceiling, it can go through the ceilings. Here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ladybug3 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ladybug3 extends Actor
{
    private int vSpeed=0;
    private int acceleration=2;
    private boolean jumping;
    private int jumpStrength=17;
    private int speed=5;
    public void act() 
    {
        checkFall();
        checkKey();
        setRotation(-90);
        platformAbove();
   
        // Add your action code here.
    }    
    public void checkKey() {
        if (Greenfoot.isKeyDown("space")&&jumping==false) {
            jump();
        }
        if (Greenfoot.isKeyDown("right")){
           { setRotation(0);}
            move(5);
        }
        if(Greenfoot.isKeyDown("left")){
            {setRotation(180);}
            move(5);
        }
    }
   
    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/2)+5;
        Actor ground=getOneObjectAtOffset(0, getImage().getHeight()/2, Bar2.class);
        if(ground==null)
        {
            jumping=true;
            return false;
        }
        else {
            moveToGround(ground);
            return true;
        }
    }
   public boolean platformAbove()
    {
        int spriteHeight=getImage().getHeight();
        int yDistance=(int)(spriteHeight/-2);
        Actor ceiling=getOneObjectAtOffset(0, yDistance, Bar2.class);
        if(ceiling !=null)
        {
            vSpeed=1;
            bopHead(ceiling);
            return true;
        }
        else {
           
            return false;
        }
    }
      public void bopHead(Actor ceiling) {
        int ceilingHeight=ceiling.getImage().getHeight();
        int newY=ceiling.getY()+(ceilingHeight+getImage().getHeight())/2;
        setLocation(getX(), newY);
    }
    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 checkFall()
    {
        if(onGround()) {
            vSpeed=0;
    }
    else {
        fall();
    }
}
public void jump() {
    vSpeed=vSpeed-jumpStrength;
    jumping=true;
    fall();

}
 
}
miku622 miku622

2014/7/16

#
i fixed number 2 but i still need help with number 1!
danpost danpost

2014/7/16

#
I do not get that behavior (twitching up and down) when using this class alone. Do you have code in another class that might cause a re-positioning of the Ladybug3 actor (your Bar2 class would be a prime suspect)?
miku622 miku622

2014/7/16

#
My Bar2 class is empty:
import greenfoot.*; 

public class Bar2 extends Actor
{
  
    public void act() 
    {
       
    }    
}
danpost danpost

2014/7/16

#
You World sub-class would be an alternative suspect.
miku622 miku622

2014/7/17

#
my world class just has all the objects that i added to the background.
danpost danpost

2014/7/17

#
Like I wrote before, the Ladybug3 class works fine (with no twitching) by itself. Post the scenario on this site (make sure to check the 'Publish source code' checkbox) and I will check it out.
miku622 miku622

2014/7/17

#
ok thanks i will after i finish the last level
You need to login to post a reply.