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

2016/9/16

score problem

1
2
Dayuyu Dayuyu

2016/9/16

#
world coding
    score score = new score();

    public score getScore()
    {
        return score;
    }
score coding
import greenfoot.*;
import java.awt.Color;

public class score extends Actor
{
    int score = 0;
    public void act()   
    {    
        setImage(new GreenfootImage("Score : "+ score, 24, Color.GREEN,  Color.BLACK));
    }
    
    public void addscore()
    {
        score+=2;
    }
}
button coding
    public void act() 
    {
        // Add your action code here
         if (Greenfoot.mouseClicked(this)) {
         win alert_win = new win();
         getWorld().addObject(alert_win,250, 300); 
         Greenfoot.delay(200);
         Q2 q2World = (Q2)getWorld(); // get reference to Q world object
         Greenfoot.setWorld(q2World.getLastWorld());
         score.addScore();
        }
    }
Dayuyu Dayuyu

2016/9/16

#
my problem is the score.addscore(); non-static method cannot be referenced from a static context .. to get the score when the main actor get correct answer at button coding.
Super_Hippo Super_Hippo

2016/9/16

#
Change line 10 in button to this:
q2World.getScore().addScore();
To make this work, the method should be changed to 'addScore' in the score class (case sensitive) By the way: Class names should start with an uppercase letter. You only have to change the image of the score object when it actually changes and not every time.
Dayuyu Dayuyu

2016/9/17

#
i change the coding like u say use uppercase and the getScore() cannot find the symbol. world
    Score score = new Score();

    public Score getScore()
    {
        return score;
    }
score
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    int score = 0;
    public void act()   
    {    
        setImage(new GreenfootImage("Score : "+ score, 24, Color.GREEN,  Color.BLACK));
    }
    
    public void addScore()
    {
        score+=2;
    }
}
button
    public void act() 
    {
        
        // Add your action code here
         if (Greenfoot.mouseClicked(this)) {
         win alert_win = new win();
         getWorld().addObject(alert_win,250, 300); 
         Greenfoot.delay(200);
         Q2 q2World = (Q2)getWorld(); // get reference to Q world object
         Greenfoot.setWorld(q2World.getLastWorld());
         q2World.getScore().addScore();
        }
    }  
danpost danpost

2016/9/17

#
Please show your entire Q2 world class code. I believe the problem will be found there. However, I find it hard to believe that you would get a symbol not found on 'getScore' when you say that the 'getScore' method shown is in your world class (unless you do not have it in your Q2 world class).
Dayuyu Dayuyu

2016/9/19

#
i bit confuse here the score not change.. My score is at Background1... When actor touch the monyet at Background1 the question while pop up at q2world, user while choose button for the answer and when true the score while increase... background1
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background1 extends World
{

    /**
     * Constructor for objects of class Background1.
     * 
     */
    public Background1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1);
        prepare();
    }
    
    private void prepare()
    {
        Rahmat rahmat = new Rahmat();
        addObject(rahmat,42,260);
        rahmat.setLocation(35,256);
        addObject(new Score(),740, 40);
        addObject(new Pisang(),100,270);
        addObject(new Pisang(),140,270);
        addObject(new Pisang(),180,270);
        addObject(new Pisang(),220,270);
        addObject(new Pisang(),70,140);
        addObject(new Pisang(),110,140);
        addObject(new Pisang(),150,140);
        addObject(new Pisang(),150,100);
        addObject(new Pisang(),30,570);
        addObject(new Pisang(),30,510);
        addObject(new Pisang(),30,450);
        addObject(new Pisang(),30,390);
        addObject(new Pisang(),680,160);
        addObject(new Pisang(),760,160);
        addObject(new Pisang(),680,230);
        addObject(new Pisang(),760,230);
        addObject(new Pisang(),760,290);
        addObject(new Pisang(),760,345);
        addObject(new Pisang(),700,345);
        addObject(new Pisang(),700,460);
        addObject(new Pisang(),750,460);
        addObject(new Pisang(),700,510);
        addObject(new Pisang(),750,510);
        addObject(new Pisang(),700,560);
        addObject(new Pisang(),750,560);
        addObject(new Pisang(),580,430);
        addObject(new Pisang(),530,430);
        addObject(new Pisang(),530,390);
        addObject(new Pisang(),530,350);
        addObject(new Pisang(),530,310);
        addObject(new Pisang(),530,270);
        addObject(new Pisang(),340,450);
        addObject(new Pisang(),420,450);
        addObject(new Pisang(),340,380);
        addObject(new Pisang(),420,380);
        addObject(new Pisang(),340,220);
        addObject(new Pisang(),420,220);
        addObject(new Pisang(),340,270);
        addObject(new Pisang(),420,270);
        addObject(new tree(),22,80);
        addObject(new tree(),60,80);
        addObject(new tree(),135,45);
        addObject(new tree(),100,80);
        addObject(new tree(),22,120);
        addObject(new tree(),22,160);
        addObject(new tree(),22,200);
        addObject(new tree(),160,45);
        addObject(new tree(),200,45);
        addObject(new tree(),240,45);
        addObject(new tree(),280,45);
        addObject(new tree(),320,45);
        addObject(new tree(),360,45);
        addObject(new tree(),400,45);
        addObject(new tree(),440,45);
        addObject(new tree(),480,45);
        addObject(new tree(),520,45);
        addObject(new tree(),560,45);
        addObject(new tree(),600,45);
        addObject(new tree(),640,45);
        addObject(new tree(),660,90);
        addObject(new tree(),700,90);
        addObject(new tree(),740,90);
        addObject(new tree(),780,90);
        addObject(new tree(),600,160);
        addObject(new tree(),600,200);
        addObject(new tree(),600,240);
        addObject(new tree(),600,280);
        addObject(new tree(),640,280);
        addObject(new tree(),680,280);
        addObject(new tree(),720,280);
        addObject(new tree(),600,320);
        addObject(new tree(),600,360);
        addObject(new tree(),620,400);
        addObject(new tree(),660,400);
        addObject(new tree(),700,400);
        addObject(new tree(),740,400);
        addObject(new tree(),780,400);
        addObject(new tree(),480,240);
        addObject(new tree(),480,280);
        addObject(new tree(),480,320);
        addObject(new tree(),480,360);
        addObject(new tree(),480,400);
        addObject(new tree(),480,440);
        addObject(new tree(),500,480);
        addObject(new tree(),540,480);
        addObject(new tree(),580,480);
        addObject(new tree(),620,440);
        addObject(new tree(),620,480);
        addObject(new tree(),280,160);
        addObject(new tree(),280,200);
        addObject(new tree(),280,240);
        addObject(new tree(),280,280);
        addObject(new tree(),440,320);
        addObject(new tree(),400,320);
        addObject(new tree(),22,320);
        addObject(new tree(),60,320);
        addObject(new tree(),100,320);
        addObject(new tree(),140,320);
        addObject(new tree(),180,320);
        addObject(new tree(),220,320);
        addObject(new tree(),260,320);
        addObject(new tree(),300,320);
        addObject(new tree(),560,160);
        addObject(new tree(),520,160);
        addObject(new tree(),280,430);
        addObject(new tree(),240,430);
        addObject(new tree(),200,430);
        addObject(new tree(),160,430);
        addObject(new tree(),120,430);
        addObject(new tree(),80,430);
        addObject(new tree(),80,460);
        addObject(new tree(),80,500);
        addObject(new tree(),80,540);
        addObject(new tree(),80,580);
        addObject(new tree(),280,460);
        addObject(new tree(),280,500);
        addObject(new tree(),320,500);
        addObject(new tree(),280,500);
        addObject(new tree(),360,500);
        addObject(new tree(),400,500);
        addObject(new tree(),120,210);
        addObject(new tree(),160,210);
        addObject(new tree(),200,210);
        addObject(new tree(),240,210);
        addObject(new tree(),320,160);
        addObject(new tree(),360,160);
        addObject(new tree(),400,160);
        addObject(new tree(),440,160);
        addObject(new Monyet(),70,210);
        addObject(new Monyet1(),480,160);
        addObject(new Monyet2(),630,115);
        addObject(new Monyet3(),350,320);
        addObject(new Monyet4(),450,490);
        addObject(new Monyet5(),280,560);
        addObject(new key(),650,345);
        addObject(new door(),130,480);
    }
    
    public void nextLevel()
    {
        Greenfoot.setWorld(new level2());
    }
    
    Score score = new Score();

    public Score getScore()
    {
        return score;
    }
}
q2
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Q2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Q2 extends World
{

    /**
     * Constructor for objects of class Q2.
     * 
     */
    private World lastWorld;
    public Q2(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 550, 1); 
        lastWorld = world;
        prepare();
        getScore();
    }
    
    public World getLastWorld()
    {
        return lastWorld;
    }
    
    Score score = new Score();
    public Score getScore()
    {
        return score;
    }
    
    private void prepare()
    {
      s2 s2 = new s2();
      addObject(s2,300,200);
      s2.setLocation(250,300);
      
      a1 a1 = new a1();
      addObject(a1,300,200);
      a1.setLocation(80,260);
      
      b1 b1 = new b1();
      addObject(b1,300,200);
      b1.setLocation(80,360);
      
      c1 c1 = new c1();
      addObject(c1,300,200);
      c1.setLocation(320,260);
      
      d1 d1 = new d1();
      addObject(d1,300,200);
      d1.setLocation(320,360);
    }
}
score
import greenfoot.*;
import java.awt.Color;

public class Score extends Actor
{
    int score = 0;
    public void act()   
    {    
        setImage(new GreenfootImage("Score : "+ score, 24, Color.GREEN,  Color.BLACK));
    }
    
    public void addScore()
    {
        score+=2;
    }
}
button
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class b1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class b1 extends s2
{
    /**
     * Act - do whatever the b1 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
         if (Greenfoot.mouseClicked(this)) {
         win alert_win = new win();
         getWorld().addObject(alert_win,250, 300); 
         Greenfoot.delay(200);
         Q2 q2World = (Q2)getWorld(); 
         Greenfoot.setWorld(q2World.getLastWorld());
         q2World.getScore().addScore();
        }
    }   
}
danpost danpost

2016/9/19

#
You are creating a second Score object in the Q2 class. It is the one being added to and it is not the same object as the one in your Background1 class. In your Q2 class, change line 31 to
Score score;
and change line 23 to:
if (world instanceof Background1) score = ((Background1)world).getScore();
You may be able to drop the 'if' part and just execute the assignment; but I am not totally sure about the structure of your classes where multiple questions are concerned.
danpost danpost

2016/9/19

#
After further review, I see that line 28 of the Background1 class is creating even another Score instance and placing that one in the world. This one is not the same one as that referred to by the 'score' field. Change that line to:
addObject(score, 740, 40);
so that the one referenced in the 'score' field is placed into the world.
Dayuyu Dayuyu

2016/9/19

#
can i ask how to do the decrease code.. because i want to decrease the life of actor when answer wrong... life
import greenfoot.*; 

import java.awt.Color;
import java.applet.*;
import java.awt.*;

public class Life extends Actor

{
  Font font1=new Font("Helvetica",Font.BOLD,25);
  public Life(String text)
  {
      GreenfootImage img_field = new GreenfootImage(140,50);
      img_field.setFont(font1);
      img_field.setColor(Color.black);
      img_field.drawString(text, 0, 45);
      setImage(img_field);
   }
      
  public void setText(String text)
   {
       GreenfootImage img_field = getImage();
       img_field.clear();
       img_field.drawString(text, 0, 45);
       
   }
}
actor - collect Pisang for life
    public void makan()
    {
        //Actor Score;
        Actor Pisang;
        Pisang = getOneObjectAtOffset(4, 4, Pisang.class);
        getWorld().addObject(life_field, 90, 30);
        
        if (Pisang != null)
        {
            World world;
            world = getWorld() ;
            world.removeObject(Pisang);
            totalPisangEat++;
            
            life_field.setText("Life: " +totalPisangEat);
        }
    }
danpost danpost

2016/9/19

#
Dayuyu wrote...
can i ask how to do the decrease code.. because i want to decrease the life of actor when answer wrong
Well, I presume that this would also occur while the Q2 world is active. So, (1) get the last world from the Q2 world and cast is appropriately (2) get the player from that world and (3) decrease the value of that player's 'totalPisangEat' field.
Dayuyu Dayuyu

2016/9/24

#
correct me if I'm wrong....i try to make life decrease use coding score like i make before but totalPisangEat cannot find symbol actor coding
    public void makan()
    {
        //Actor Score;
        Actor Pisang;
        Pisang = getOneObjectAtOffset(4, 4, Pisang.class);
        getWorld().addObject(life_field, 90, 30);
        
        if (Pisang != null)
        {
            World world;
            world = getWorld() ;
            world.removeObject(Pisang);
            totalPisangEat++;
            
            life_field.setText("Life: " +totalPisangEat);
        }
    }
    
     public void addTolak()
    {
       totalPisangEat-=2;
    }
Q2 coding
    private World lastWorld;
    public Q2(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 550, 1); 
        lastWorld = world;
        prepare();
        if (world instanceof Background1) score = ((Background1)world).getScore();
    }
    
    public World getLastWorld()
    {
        return lastWorld;
    }
    
    Score score;
    public Score getScore()
    {
        return score;
    }
    
    public Score getTolak()
    {
       return totalPisangEat;
    }
level 1 / background1
    public void nextLevel()
    {
        Greenfoot.setWorld(new level2());
    }
    
    Score score = new Score();

    public Score getScore()
    {
        return score;
    }
    
    public Score getTolak()
    {
       return totalPisangEat;
    }
}
Super_Hippo Super_Hippo

2016/9/24

#
You are using 'totalPisangEat' in all classes. In the actor subclass, you seem to use it as an 'int', in the world subclasses you are using it as a Score object. I don't see the variable created anywhere though.
Dayuyu Dayuyu

2016/9/24

#
i make the change for the coding and no error but when i run the game and answer wrong for decrease life this error pop up java.lang.NullPointerException at c1.act(c1.java:24) TotalPisangEat
    int totalPisangEat;
    public void addTotalPisangEat()
    {
       totalPisangEat-=2;
    }
Q2
    TotalPisangEat totalPisangEat;
    public TotalPisangEat getTotalPisangEat()
    {
       return totalPisangEat;
    }
c1
    public void act() 
    {
        // Add your action code here.
        if (Greenfoot.mouseClicked(this)) {
         loose alert_loose = new loose();
         getWorld().addObject(alert_loose,250, 300); 
         Greenfoot.delay(200);
         Q2 q2World = (Q2)getWorld(); // get reference to Q world object
         Greenfoot.setWorld(q2World.getLastWorld());
         q2World.getTotalPisangEat().addTotalPisangEat();
        }
    }
background1
    TotalPisangEat totalPisangEat;
    public TotalPisangEat getTotalPisangEat()
    {
       return totalPisangEat;
    }
actor
public int totalPisangEat = 0;
    Life life_field = new Life("Life:" + totalPisangEat);
    
    private GreenfootImage kanan = new GreenfootImage ("kanan.png");
    private GreenfootImage kanan1 = new GreenfootImage ("kanan1.png");
    private GreenfootImage kanan2 = new GreenfootImage ("kanan2.png");
    
    private GreenfootImage kiri = new GreenfootImage ("kiri.png");
    private GreenfootImage kiri1 = new GreenfootImage ("kiri1.png");
    private GreenfootImage kiri2 = new GreenfootImage ("kiri2.png");
    
    private GreenfootImage dpn = new GreenfootImage ("dpn.png");
    private GreenfootImage dpn1 = new GreenfootImage ("dpn1.png");
    private GreenfootImage dpn2 = new GreenfootImage ("dpn2.png");
    
    private GreenfootImage blakang = new GreenfootImage ("blakang.png");
    private GreenfootImage belakang1 = new GreenfootImage ("belakang1.png");
    private GreenfootImage belakang2 = new GreenfootImage ("belakang2.png");
 
    private int frame = 1;
        
     /**
     * Act - do whatever the Rahmat 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.   
        checkKey();
        makan();
        grab();
        exit();
        soalan1();
        soalan2();
        soalan3();
        soalan4();
        soalan5();
        soalan6();
    }    
    
    public void checkKey()
    {
        /*Actor tree;
        tree = getOneObjectAtOffset(4, 4, tree.class);
        if (tree != null)
        {
            World world;
            world = getWorld();
            move(-6);
        }*/
        
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
            moveUp();
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
            moveDown();
        }
    }
    
    public void makan()
    {
        //Actor Score;
        Actor Pisang;
        Pisang = getOneObjectAtOffset(4, 4, Pisang.class);
        getWorld().addObject(life_field, 90, 30);
        
        if (Pisang != null)
        {
            World world;
            world = getWorld() ;
            world.removeObject(Pisang);
            totalPisangEat++;
            
            life_field.setText("Life: " +totalPisangEat);
        }
    }
danpost danpost

2016/9/24

#
What is the purpose of the TotalPisangEat class? Totally unnecessary. Remove the TotalPisangEat class. Remove all the codes given in your last post for the background1 and Q2 classes. Something needs done with line 10 of the c1 class code given -- but I am not sure what and do not have time to figure it out right now.
Dayuyu Dayuyu

2016/9/25

#
i done remove TotalPisangEat class and try to make another coding but the same thing happen..no error but has this pop up java.lang.NullPointerException at c1.act(c1.java:24) Q2
    private World lastWorld;
    public Q2(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(500, 550, 1); 
        lastWorld = world;
        prepare();
        if (world instanceof Background1) score = ((Background1)world).getScore();
        if (world instanceof Background1) totalPisangEat = ((Background1)world).getPisang();
    }
    
    public World getLastWorld()
    {
        return lastWorld;
    }
    
    Score score;
    public Score getScore()
    {
        return score;
    }
    
    Rahmat totalPisangEat;
    public Rahmat getPisang()
    {
        return totalPisangEat;
    }
    
Background 1
    public void nextLevel()
    {
        Greenfoot.setWorld(new level2());
    }
    
    Score score = new Score();

    public Score getScore()
    {
        return score;
    }
    
    Rahmat totalPisangEat;
    public Rahmat getPisang()
    {
        return totalPisangEat;
    }
actor
public class Rahmat extends Actor
{   
    private int vSpeed = 0;
    private int acceleration = 2;
    private int speed = 2;
    
    private boolean haskey = false;
    
    public int totalPisangEat = 0;
    Life life_field = new Life("Life:" + totalPisangEat);
    
    private GreenfootImage kanan = new GreenfootImage ("kanan.png");
    private GreenfootImage kanan1 = new GreenfootImage ("kanan1.png");
    private GreenfootImage kanan2 = new GreenfootImage ("kanan2.png");
    
    private GreenfootImage kiri = new GreenfootImage ("kiri.png");
    private GreenfootImage kiri1 = new GreenfootImage ("kiri1.png");
    private GreenfootImage kiri2 = new GreenfootImage ("kiri2.png");
    
    private GreenfootImage dpn = new GreenfootImage ("dpn.png");
    private GreenfootImage dpn1 = new GreenfootImage ("dpn1.png");
    private GreenfootImage dpn2 = new GreenfootImage ("dpn2.png");
    
    private GreenfootImage blakang = new GreenfootImage ("blakang.png");
    private GreenfootImage belakang1 = new GreenfootImage ("belakang1.png");
    private GreenfootImage belakang2 = new GreenfootImage ("belakang2.png");
 
    private int frame = 1;
        
     /**
     * Act - do whatever the Rahmat 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.   
        checkKey();
        makan();
        grab();
        exit();
        soalan1();
        soalan2();
        soalan3();
        soalan4();
        soalan5();
        soalan6();
    }    
    
    public void checkKey()
    {
        /*Actor tree;
        tree = getOneObjectAtOffset(4, 4, tree.class);
        if (tree != null)
        {
            World world;
            world = getWorld();
            move(-6);
        }*/
        
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
        
        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        
        if(Greenfoot.isKeyDown("up"))
        {
            moveUp();
        }
        
        if(Greenfoot.isKeyDown("down"))
        {
            moveDown();
        }
    }
    
    public void makan()
    {
        //Actor Score;
        Actor Pisang;
        Pisang = getOneObjectAtOffset(4, 4, Pisang.class);
        getWorld().addObject(life_field, 90, 30);
        
        if (Pisang != null)
        {
            World world;
            world = getWorld() ;
            world.removeObject(Pisang);
            totalPisangEat++;
            
            life_field.setText("Life: " +totalPisangEat);
        }
    }
    
     public void losePisang()
    {
        totalPisangEat-=2;
    }
    
C1
    public void act() 
    {
        // Add your action code here.
        if (Greenfoot.mouseClicked(this)) {
         loose alert_loose = new loose();
         getWorld().addObject(alert_loose,250, 300); 
         Greenfoot.delay(200);
         Q2 q2World = (Q2)getWorld(); // get reference to Q world object
         Greenfoot.setWorld(q2World.getLastWorld());
         q2World.getPisang().losePisang();
        }
    } 
There are more replies on the next page.
1
2