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

2015/4/21

keep the score even the world is resetted

listiliz listiliz

2015/4/21

#
Hi, I have a little problem about score over here. So I have been following steps for making score : https://www.youtube.com/watch?v=VnR6i7BXD-E but my score counter still doesn't incremented I think it's because the world is resetted so i can't keep the score, do any of you have solutions? Here's my code: "bg" is my world
import greenfoot.*;

/**
 * Write a description of class bg here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bg extends World
{
    Counter counter = new Counter();

    /**
     * Constructor for objects of class bg.
     * 
     */
    public bg()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        makanan.reset();

        prepare();
    }
    
    public Counter getCounter()
    {
         return counter;
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        addObject(counter, 100, 40);

        musuh musuh = new musuh();
        addObject(musuh, 137, 300);
        musuh musuh2 = new musuh();
        addObject(musuh2, 375, 363);
        musuh musuh3 = new musuh();
        addObject(musuh3, 350, 282);
        musuh3.turn(45);
        musuh musuh4 = new musuh();
        addObject(musuh4, 213, 367);

        ular ular = new ular();
        addObject(ular, 33, 360);
        removeObject(ular);
        ular ular2 = new ular();
        addObject(ular2, 67, 369);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(213, 367);
        musuh4.setLocation(279, 383);
        musuh.setLocation(45, 275);
        musuh4.setLocation(252, 253);
        musuh3.setLocation(191, 257);
        musuh2.setLocation(482, 276);
        musuh2.setLocation(482, 276);
        musuh2.setLocation(482, 276);
        musuh2.setLocation(482, 276);
        musuh2.setLocation(326, 384);
        musuh3.setLocation(190, 302);
        penjumlah1 penjumlah1 = new penjumlah1();
        addObject(penjumlah1, 69, 105);
        penjumlah2 penjumlah2 = new penjumlah2();
        addObject(penjumlah2, 345, 114);
        penjumlah2.setLocation(457, 117);
        penjumlah1.setLocation(150, 129);
        penjumlah2.setLocation(447, 133);

        plus plus = new plus();
        addObject(plus, 301, 146);
        plus.setLocation(303, 129);
        penjumlah2.setLocation(449, 131);
        hasil hasil = new hasil();
        addObject(hasil, 433, 339);
        angkaRand angkarand = new angkaRand();
        addObject(angkarand, 478, 289);
        angkaRand angkarand2 = new angkaRand();
        addObject(angkarand2, 490, 363);
        angkarand2.setLocation(349, 329);
        hasil.setLocation(438, 350);
        angkarand2.setLocation(345, 258);
        angkarand2.setLocation(338, 278);
    }
}
here's a code for "ular" who eats "hasil" and it supposed to increment the score
import greenfoot.*;
import java.awt.Color;

/**
 * Write a description of class ular here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ular extends Actor
{
   protected static int i=0;
    
    
    /**
     * Act - do whatever the ular wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        keyboard();
        makan();
        if(isTouching(hasil.class))
        {
          i=i+1;
          World myWorld = getWorld();
          bg bg = (bg)myWorld;//untuk pnggil method counter dari bg
          Counter counter= bg.getCounter(); //bikin variabel counter tipe counter diambil dr bg
          counter.addScore();
        }
        if(i==5)
        {
          Greenfoot.stop();
        }
    }    
    
      public void displayGameOver(){
       GameOver x=new GameOver();
         getWorld().addObject(x,getWorld().getWidth()/2 -40, getWorld().getHeight()/2);
        Greenfoot.stop();
      }
    
    public void makan(){
        if(isTouching(hasil.class))
        {
          
          removeTouching (hasil.class);
          Greenfoot.setWorld(new bg()); //world is resetted here
        }
          else if(isTouching(angkaRand.class) )
            {
              removeTouching (ular.class);
              Greenfoot.stop();
              displayGameOver();
            
            }
    }
    
    public void keyboard()
    {
       if(Greenfoot.isKeyDown("up")) {
           
           
           setLocation(getX(), getY()-1);
           move(3);
        }else if(Greenfoot.isKeyDown("down") ) {
          setLocation(getX(), getY()+1);
          move(3);
        }else if(Greenfoot.isKeyDown("left")) {
           
           move(-3);
        }else if(Greenfoot.isKeyDown("right") ) {
           setLocation(getX()+1, getY());
           move(3);
        }
    }
}
here's a code for Counter:
import greenfoot.*;
import java.awt.Color;

/**
 * Write a description of class skor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Counter extends Actor

{
    
    int score=0;
    /**
     * Act - do whatever the skor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.GREEN, Color.BLACK));
    }    
    
    public void addScore()
    {
      score++;
      
    }
}
danpost danpost

2015/4/21

#
You could reset makanan and remove all objects from the world at the beginning of the prepare method and then rebuild the current world instead of creating a new one (change the accessibility of 'prepare' to 'public' and call it instead of calling 'setWorld(new bg())').
listiliz listiliz

2015/4/27

#
danpost wrote...
You could reset makanan and remove all objects from the world at the beginning of the prepare method and then rebuild the current world instead of creating a new one (change the accessibility of 'prepare' to 'public' and call it instead of calling 'setWorld(new bg())').
i tried this, but it becomes -> i called the prepare method, or should i make a new method?
danpost danpost

2015/4/27

#
There is no problem with calling the prepare method programmatically. Is there a question or problem with what it became ( "i tried this, but it becomes>" )? Explain the issue (how it is not right and what right is).
You need to login to post a reply.