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

#
how would i get a counter to keep track of how many items are being collected and display it as they go through the level. the max items they can get is 2
Super_Hippo Super_Hippo

2017/1/19

#
Import the counter class, add a counter object to the world. Whenever an item is collected, call the 'add' method (or how it is called in the Counter class) on the counter object which is in the world.
jacquelinereilly jacquelinereilly

2017/1/19

#
import greenfoot.*;  
 

/**
 * This class Level1 displays the first level for the user to play.
 * 
 * Jacqueline Reilly 
 * Wednesday, January 18, 2017
 */
public class Level1 extends World
{
    Counter counter = new Counter ();
    /**
     * Constructor for objects of class Level1.
     * 
     */
    public Level1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(950, 700, 1); 
        
        //Making background fit the screen
        GreenfootImage Bg = new GreenfootImage ("background.jpg");
        Bg.scale (getWidth (), getHeight ());
        setBackground (Bg);
        
        //Adding the platforms 
        Platform platform = new Platform ();
        addObject (platform, 268, 653);
        Platform platform1 = new Platform ();
        addObject (platform1, 704, 652);
        
        //Adding characters
        addObject (new MrKrabs(), 93, 522);
        addObject (new Plankton(), 208, 490);
        
        //Adding object for Mr. Krabs to collect
        KrabbyPatty krabbyPatty = new KrabbyPatty ();
        addObject (krabbyPatty, 484, 457);
        addObject (counter, 100, 100);
        KrabbyPatty krabbyPatty1 = new KrabbyPatty ();
        addObject (krabbyPatty1, 842, 480);
        
        //Adding object for Plankton to collect
        FormulaBottle bottle = new FormulaBottle ();
        addObject (bottle, 329, 374);
        FormulaBottle bottle1 = new FormulaBottle ();
        addObject (bottle1, 691, 436);
        
        //
       
        
    }
}
jacquelinereilly jacquelinereilly

2017/1/19

#
that is my code. I think I imported it wrong
Super_Hippo Super_Hippo

2017/1/19

#
Well, your code is not showing the Counter class. You added the counter correctly.
jacquelinereilly jacquelinereilly

2017/1/19

#
So how do I do that? I'm new to this lol
danpost danpost

2017/1/19

#
You need a Counter class that extends the Actor class. You can create one or you can import the one greenfoot supplies using 'Edit>Import class...' on the menubar and selecting it.
jacquelinereilly jacquelinereilly

2017/1/19

#
I got it. thank you. Where should I add the counter object? Line 40?
danpost danpost

2017/1/19

#
jacquelinereilly wrote...
I got it. thank you. Where should I add the counter object? Line 40?
Line 40 does add the counter into your world (already).
jacquelinereilly jacquelinereilly

2017/1/19

#
Yes I got it. How do I add a point to the score once they touch the object? It has to disappear and then add 1 to the score Please & thank you
Super_Hippo Super_Hippo

2017/1/19

#
In the act method of the class which should remove objects from the other class: (Change ClassName to the name of the class which should be removed.)
if (isTouching(ClassName.class))
{
    removeTouching(ClassName.class);
    ((Level1) getWorld()).counter.add(1);
}
jacquelinereilly jacquelinereilly

2017/1/19

#
 if (isTouching (KrabbyPatty.class))
        {
            removeTouching (KrabbyPatty.class);
            ((Level1) getWorld()).counter.add(1);
        }
}}
it is saying they can't find add
Super_Hippo Super_Hippo

2017/1/19

#
Show the Counter class.
jacquelinereilly jacquelinereilly

2017/1/19

#
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * Jacqueline Reilly 
 * January 18, 2017
 */
public class Counter extends Actor
{
    int score = 0;
    
    public Counter()
    {
        setImage(new GreenfootImage("Krabby Patty's : " + score, 24, Color.GREEN, Color.BLACK));  
    }

    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Krabby Patty's  : " + score, 24, Color.GREEN, Color.BLACK));
    }    

    public void setScore()
    {
        score++;
    }
}

danpost danpost

2017/1/19

#
Replace '.add(1)' with '.setScore()'.
There are more replies on the next page.
1
2
3