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

2017/4/18

Adding Score...

IanWasHere IanWasHere

2017/4/18

#
I am creating a game where you need to avoid obstacles. When the obstacles reach the bottom of the screen, a point is added. But, I am not able to add change the score. I would appreciate some help. Thanks - Ian **Obstacle (block)** public class Block extends Actor { public int addScore; private int actCounter; private int grn = 6; public int getTransparency; private int bttl = 300; private int mrn = 10; public Block() { setRotation(90); getImage().setColor(Color.BLUE); fillBlock(); } public void act() { moveDown(); } /** * Change the colour of the Block. */ private void fillBlock() { getImage().fill(); } /** * Move a random amount of steps. Call "atBottom". */ private void moveDown() { atBottom(); move(Greenfoot.getRandomNumber(mrn)); } /** * Check if we have touvhed the bottom, if so become invisible. */ private void atBottom() { if (getY() == 499) { getImage().setTransparency(0); backTop(); addScore(); } else { getImage().setTransparency(255); if (isTouching(Dot.class)) { if (getTransparency !=255) { World world = getWorld(); world.removeObjects(world.getObjects(null)); //removes all the objects in the world; world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world; Greenfoot.stop(); } } } } /** * If at bottom, go back up and come down again. */ private void backTop() { setLocation(Greenfoot.getRandomNumber(300), 22); moveDown(); } } **Counter** public class Counter extends Actor { private int score; public int done = 20; public int addScore; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Counter() { score = 0; setImage (new GreenfootImage(200, 30)); GreenfootImage image = getImage(); image.scale(image.getWidth() + 200, image.getHeight() + 30); setImage(image); update(); } public void addScore() { score++; update(); if (score == done) { Greenfoot.playSound("tada.wav"); Greenfoot.stop(); } } public void update() { GreenfootImage ing = getImage(); ing.clear(); ing.setColor(Color.BLACK); ing.drawString("S c o r e : " + score, 4, 20); } }
danpost danpost

2017/4/18

#
Make sure you use 'new Counter' only once in your World subclass. Also, you are calling 'addScore' in your Block class as if the method was located in the Block class. Since that method is in the Counter class, you need a reference to your Counter object to call the method on.
danpost danpost

2017/4/18

#
Your first four declared int fields do not make much sense -- 'int addScore', 'int actCounter' and 'int grn' are not used at all and I think you added 'int getTransparency' because you were getting an error on the following line:
 if (getTransparency !=255)
which has 'getTransparency' as a field instead of as a method. The line should probably be this:
 if (getImage().getTransparency() !=255)
IanWasHere IanWasHere

2017/4/19

#
danpost wrote...
Make sure you use 'new Counter' only once in your World subclass. Also, you are calling 'addScore' in your Block class as if the method was located in the Block class. Since that method is in the Counter class, you need a reference to your Counter object to call the method on.
Thanks! Now, how do I reference the counter object?
IanWasHere IanWasHere

2017/4/19

#
IanWasHere wrote...
danpost wrote...
Make sure you use 'new Counter' only once in your World subclass. Also, you are calling 'addScore' in your Block class as if the method was located in the Block class. Since that method is in the Counter class, you need a reference to your Counter object to call the method on.
Thanks! Now, how do I reference the counter object?
I keep getting the error: non-static method getCounter() cannot be referenced from static context import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Shape; /** * Write a description of class Block here. * * @author (Ian McMitchell) * @version (April 9/16) */ public class Block extends Actor { public int addScore; private int actCounter; private int grn = 6; public int getTransparency; private int bttl = 300; private int mrn = 10; public Block() { setRotation(90); getImage().setColor(Color.BLUE); fillBlock(); } public void act() { moveDown(); } /** * Change the colour of the Block. */ private void fillBlock() { getImage().fill(); } /** * Move a random amount of steps. Call "atBottom". */ private void moveDown() { atBottom(); move(Greenfoot.getRandomNumber(mrn)); } /** * Check if we have touvhed the bottom, if so become invisible. */ private void atBottom() { if (getY() == 499) { World myWorld = getWorld(); backTop(); score(); MyWorld myworld = (MyWorld)myWorld; Counter counter = MyWorld.getCounter(); Counter.addScore(); } else { getImage().setTransparency(255); if (isTouching(Dot.class)) { if (getImage().getTransparency() !=255) { World world = getWorld(); world.removeObjects(world.getObjects(null)); //removes all the objects in the world; world.addObject(new GameOver(), world.getWidth()/2, world.getHeight()/2); //adds the game over screen in the middle of the world; Greenfoot.stop(); } } } } /** * If at bottom, go back up and come down again. */ private void backTop() { setLocation(Greenfoot.getRandomNumber(300), 22); moveDown(); } public void score() { getWorld(); } }
Yehuda Yehuda

2017/4/19

#
In the atBottom() method there is no point in all your getting worlds if you use the name of the class instead of the variable. You should do 'myworld.getCounter().addScore()'. Now you have to make sure that in your MyWorld class there is only one instance of Counter being used meaning as danpost said "Make sure you use 'new Counter' only once in your World subclass.".
danpost danpost

2017/4/19

#
Change this line:
Counter.addScore();
to this:
counter.addScore();
IanWasHere IanWasHere

2017/4/20

#
Thanks, guys! I keep getting an error about static and non-static.... non-static method getCounter() cannot be referenced from static context
danpost danpost

2017/4/20

#
IanWasHere wrote...
Thanks, guys! I keep getting an error about static and non-static.... non-static method getCounter() cannot be referenced from static context
Did you make the change I suggested?
danpost wrote...
Change this line:
Counter.addScore();
to this:
counter.addScore();
IanWasHere IanWasHere

2017/4/20

#
yes, I did.
Yehuda Yehuda

2017/4/20

#
IanWasHere wrote...
yes, I did.
And you're getting the error on that line?
IanWasHere IanWasHere

2017/4/20

#
no.. the line above MyWorld.getCounter().addScore();
Yehuda Yehuda

2017/4/20

#
You don't need to do both of those. You're getting an error because you did MyWorld instead of the name of the instance you have of it. If you did what danpost said to do then you don't also need to do what I said. You shouldn't call the addScore method twice.
IanWasHere IanWasHere

2017/4/20

#
oh... I see.. this topic is new to me. But I'm getting a new error: cannot find symbol - variable counter
Yehuda Yehuda

2017/4/20

#
The if should look something like this:
        if (getY() == 499)        { 
            backTop();
            score();
            MyWorld myWorld = (MyWorld) getWorld();
            Counter counter = myWorld.getCounter();
            counter.addScore();
        }
You can combine the last two lines of the if into one technically.
You need to login to post a reply.