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

2018/1/1

How to create a score counter in greenfoot?

brianfu brianfu

2018/1/1

#
I want to add 1 to the score counter when my character jumps over an obstacle. But I don't know what the condition will be. please help me!!! Here is the code for my obstacle: public class Obstable2 extends Actor { int moveSpeed=3; public void act() { setLocation (getX()-moveSpeed, getY()); remove(); spawn(); } private void remove() { Actor position= getOneIntersectingObject(Frog.class); if (position !=null) { World world; world=getWorld(); GameOver game = new GameOver(); world.addObject(game, world.getWidth()/2, world.getHeight()/2); world.removeObject(position); } } private void spawn() { int heightOfObstable2 =getImage().getHeight(); Actor theblock=getOneObjectAtOffset(599, 600, Obstable2.class); } } Here is the code for my world: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class brick extends World { Counter counter = new Counter(); public brick() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 599, 1, false); Frog position= new Frog(); addObject(position, 50,420); Stage pos = new Stage(); addObject(pos, 500,525); Obstable2 jump2= new Obstable2(); addObject(jump2, 1050, 420); addObject(counter, 100, 40); } private int spawnCounter = 0; public void act() { if (spawnCounter > 100) { spawnCounter = 0; addObject(new Obstable2(), Greenfoot.getRandomNumber(1050)+1050,420); } spawnCounter++; } public Counter getCounter() { return counter; } }
danpost danpost

2018/1/1

#
First, remove the 'spawn' method and the call to that method from the 'act' method of the Obstacle2 classs (the method, as is, does absolutely nothing). Then for the counter, add an 'else' clause to the code in the 'remove' method. Ask if the new x-coordinate is a bit lower than that of the frog. Only ask for a coordinate in the same range as what the obstacle moves each act cycle. When in the range, increment the counter:
1
((brick)getWorld()).getCounter().add(1);
As you did not show the Counter class, the way to increment might be different than that which is given here ( using 'add(1)' ).
mert2727 mert2727

2018/1/3

#
look at this :D https://www.youtube.com/watch?v=VnR6i7BXD-E
danpost danpost

2018/1/3

#
mert2727 wrote...
look at this :D https://www.youtube.com/watch?v=VnR6i7BXD-E
That link shows how to make a score counter; but, that is not the issue here (thanks for trying to help, anyway). The issue here was coming up with a condition that will satisfy the particular needs of this game for scoring:
brianfu wrote...
I want to add 1 to the score counter when my character jumps over an obstacle. But I don't know what the condition will be. please help me!!!
You need to login to post a reply.