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

2017/1/29

Please help me to counter the score.

1
2
biem biem

2017/1/29

#
Please help me to counter the score. For class Urs i use code : public int NumarMere; public void MarIntersecting() { //adunarea merelor Actor x = getOneIntersectingObject(Mar.class); if (x != null) { getWorld().removeObject(x); NumarMere++; } For class MyWorld i use: public Counter getNumarMere() {return NumarMere; } For class Counter i use : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 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("Scor : " + 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("Scor : " + score, 24, Color.GREEN, Color.BLACK)); } public void setScore() { score++; } } Thank you
danpost danpost

2017/1/29

#
I think you just need to call 'setScore' on the Counter object (the MyWorld 'NumarMere' field which the MyWorld 'getNumarMere' method returns) when you increment the int 'NumarMere' field in the Urs class. Immediately after 'NumarMere++;' line in the Urs class, add the following line:
((MyWorld)getWorld()).getNumarMere().setScore();
biem biem

2017/1/29

#
I've done the modification: In MyWorld : public Counter setScore() {return getNumarMere; }
biem biem

2017/1/29

#
In class Urs : public Counter setScore() {return getNumarMere; } But is not work. Is a probleme with getNumarMere.
danpost danpost

2017/1/29

#
There should not have been any modifications in the MyWorld class. The only thing you should have done was to add the line I gave you immediately after the line I said to place it after in the Urs class.
biem biem

2017/1/29

#
Unfortunately program gives me the error message: cannot find symbol -method getNumarMere(). Maybe it needs to import resources java?
danpost danpost

2017/1/29

#
You originally had shown this:
biem wrote...
]For class MyWorld i use:
public Counter getNumarMere()
    {return NumarMere;
    }
which was fine. But, then you modified it (for some unknown reason) and came back with this:
I've done the modification: In MyWorld : public Counter setScore() {return getNumarMere; }
Change it back.
biem biem

2017/1/29

#
Now, erore seem to be in MyWorld:mesaj for Numar Mere: incompatible types :int cannot be converted to Counter public Counter getNumarMere() {return NumarMere; }
danpost danpost

2017/1/29

#
biem wrote...
Now, erore seem to be in MyWorld:mesaj for Numar Mere: incompatible types :int cannot be converted to Counter public Counter getNumarMere() {return NumarMere; }
Show the line that declares the NumarMere field in the MyWorld class and also show the line that assigns an object to the field. It might also help if you show anything related to the Counter object in your MyWorld class.
biem biem

2017/1/29

#
in MyWorld import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class MyWorld extends World { public int NumarMere; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(800, 600, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Copac copac11 = new Copac(); addObject(copac11,553,512); Copac copac12 = new Copac(); addObject(copac12,647,517); Copac copac16 = new Copac(); addObject(copac16,438,210); Mar mar2 = new Mar(); addObject(mar2,169,375); Mar mar3 = new Mar(); addObject(mar3,132,267); Mar mar4 = new Mar(); addObject(mar4,108,115); Urs urs = new Urs(); addObject(urs,68,571); Counter counter = new Counter(); addObject(counter,716,36); } public Counter getNumarMere() {return NumarMere; } }
biem biem

2017/1/29

#
in class Counter import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 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("Scor : " + 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("Scor : " + score, 24, Color.GREEN, Color.BLACK)); } public void setScore() { score++; } }
biem biem

2017/1/29

#
in class Urs import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Cos here. * * @author (your name) * @version (a version number or a date) */ public class Urs extends Actor { /** * Act - do whatever the Cos wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public static int NumarMere; //variabila numar intreg pentru numaryl de intersectii cu merele public void act() { keyboardControls(); MarIntersecting(); CopacIntersecting(); } public void MarIntersecting() { //adunarea merelor Actor x = getOneIntersectingObject(Mar.class); if (x != null) { getWorld().removeObject(x); NumarMere++; ((MyWorld)getWorld()).getNumarMere().setScore(); } }
danpost danpost

2017/1/29

#
In your MyWorld class, this line:
public Counter getNumarMere()
declares a method that returns a Counter object reference -- not an int value. This line in your MyWorld class declares the NumarMere field to be of type int -- not Counter:
public int NumarMere;
The method tries to return the value of the field -- it cannot be both ways.
biem biem

2017/1/29

#
What can I do ?
danpost danpost

2017/1/29

#
biem wrote...
What can I do ?
Well, change the field to hold a Counter reference and assign your Counter object to it.
There are more replies on the next page.
1
2