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

2017/1/19

Counter

1
2
3
jacquelinereilly jacquelinereilly

2017/1/19

#
the score isn't adding , nothing is happening
jacquelinereilly jacquelinereilly

2017/1/19

#
    if (isTouching (KrabbyPatty.class))
        {
           removeTouching (KrabbyPatty.class);
           ((Level1) getWorld()).counter.setScore();
        }
}}
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
the score isn't adding , nothing is happening
Show the entire class code. What is shown above looks okay.
jacquelinereilly jacquelinereilly

2017/1/19

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

/**
 * This class MrKrabs allows the user to move Mr. Krabs through the level.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class MrKrabs extends Actor
{
    public MrKrabs()
    {
        GreenfootImage image = getImage ();
        image.scale (image.getWidth ()-675, image.getHeight () -780);
        setImage (image);
    }
    private int vSpeed =0;
    private int acceleration =1;
    private boolean jumping;
    private int jumpStrength =16;
    private int speed =4;
    
    /**
     * Act - do whatever the MrKrabs wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Checkiing the platform is secure
        checkKey ();
        checkFall ();
        platformAbove ();
    }    
    
    public void checkKey ()
    {
        if (Greenfoot.isKeyDown ("right")) 
        {
            //direction =1;
            setLocation (getX()+speed, getY());
        }
        if (Greenfoot.isKeyDown ("left"))
        {
            //direction =-1l
            setLocation (getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("up") && jumping == false)
        {
            jump ();
        }
}

public boolean platformAbove ()
{
    int spriteHeight = getImage ().getHeight ();
    int yDistance = (int) (spriteHeight/-2);
    Actor ceiling = getOneObjectAtOffset (0, yDistance, Platform.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 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, Platform.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 checkFall()
{
    if (onGround ())
    {
        vSpeed = 0;
    }
    else
    {
        fall ();
    }
}

public void jump ()
{
    vSpeed = vSpeed - jumpStrength;
    jumping = true;
    fall ();
}

//(Colin. J helped with this)
public void kill(){
        if(isTouching(KrabbyPatty.class)){
            
            
            eat(KrabbyPatty.class);
             
            

}}

//Collecting the krabby patty's
         public void eat(Class clss)
    {
        Actor KrabbyPatty = getOneIntersectingObject(KrabbyPatty.class);
       
        if(KrabbyPatty != null) {
            World w = getWorld();
            w.removeObject(KrabbyPatty);  
            // (Altobelli) create an instance of the BACKROUND class (casting the world
            // as the BACKROUND class
            Level1 b = (Level1)w;
            
            // creates an instance of the counter by getting it from the BACKROUND class
            // and then call the setScore method in the BACKROUND class to increment score
            Counter c = b.getCounter();
            c.setScore();          
        }

       if (isTouching (KrabbyPatty.class))
        {
           removeTouching (KrabbyPatty.class);
           ((Level1) getWorld()).counter.setScore();
        }
}}
danpost danpost

2017/1/19

#
You have triplicate code here. The 'kill' method has one and the eat method, which is not written very well, has two sets of code that do basically the same thing. On top of all that, neither of these methods are linked in any way to the 'act' method of the class (methods will not execute unless they are called to; writing them does not, in itself, make them work). Move line 163 to line 137 and remove the entire 'eat' method (lines 143 through the first character in line 165). Then add 'kill();' to the act method.
jacquelinereilly jacquelinereilly

2017/1/19

#
thank you. then add kill() to where? Sorry
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
thank you. then add kill() to where? Sorry
danpost wrote...
Then add 'kill();' to the act method.
jacquelinereilly jacquelinereilly

2017/1/19

#
   if(isTouching(KrabbyPatty.class)){
            
            
            eat(KrabbyPatty.class);
jacquelinereilly jacquelinereilly

2017/1/19

#
now that is in the act method, and i dont have eat anymore ?
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
now that is in the act method, and i dont have eat anymore ?
You have 'kill', which was doing the same job.
jacquelinereilly jacquelinereilly

2017/1/19

#
do i keep public void kill () when putting it in the act method
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
do i keep public void kill () when putting it in the act method
You are not changing the method anymore than I suggested (adding that one line to it). You need to add the following line into the act method:
kill();
so that the code in the 'kill' method can execute.
jacquelinereilly jacquelinereilly

2017/1/19

#
OH! okay here it is still have an error
jacquelinereilly jacquelinereilly

2017/1/19

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

/**
 * This class MrKrabs allows the user to move Mr. Krabs through the level.
 * 
 * Jacqueline Reilly
 * January 18, 2017
 */
public class MrKrabs extends Actor
{
    public MrKrabs()
    {
        GreenfootImage image = getImage ();
        image.scale (image.getWidth ()-675, image.getHeight () -780);
        setImage (image);
    }
    private int vSpeed =0;
    private int acceleration =1;
    private boolean jumping;
    private int jumpStrength =16;
    private int speed =4;
    
    /**
     * Act - do whatever the MrKrabs wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Checkiing the platform is secure
        checkKey ();
        checkFall ();
        platformAbove ();
        kill ();
           
}
    
    public void checkKey ()
    {
        if (Greenfoot.isKeyDown ("right")) 
        {
            //direction =1;
            setLocation (getX()+speed, getY());
        }
        if (Greenfoot.isKeyDown ("left"))
        {
            //direction =-1l
            setLocation (getX() - speed, getY());
        }
        if (Greenfoot.isKeyDown("up") && jumping == false)
        {
            jump ();
        }
}

public boolean platformAbove ()
{
    int spriteHeight = getImage ().getHeight ();
    int yDistance = (int) (spriteHeight/-2);
    Actor ceiling = getOneObjectAtOffset (0, yDistance, Platform.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 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, Platform.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 checkFall()
{
    if (onGround ())
    {
        vSpeed = 0;
    }
    else
    {
        fall ();
    }
}

public void jump ()
{
    vSpeed = vSpeed - jumpStrength;
    jumping = true;
    fall ();
}

//(Colin. J helped with this)
public void kill(){
        if(isTouching(KrabbyPatty.class)){
            
           eat(KrabbyPatty.class);
 
   }
}}
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
OH! okay here it is still have an error
It would help to know what error message you were getting. Also, I suggested the following (from above):
danpost wrote...
Move line 163 to line 137
Oh, and I see the dilemma. Change 'eat' in new line 137 to 'removeTouching'.
There are more replies on the next page.
1
2
3