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

2019/6/24

score counter

ronald ronald

2019/6/24

#
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class MyWorld extends World
{
    Counter counter = new Counter();
    /**
     * Constructor for objects of class MyWorld.
     */
    public MyWorld()
    {
        super(900, 600, 1);
        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, 50, 50);
        Emoti1 emoti1 =  new  Emoti1();
        addObject(emoti1, 130, 114);
        Emoti2 emoti2 =  new  Emoti2();
        addObject(emoti2, 330, 115);
        Counter counter =  new  Counter();
        addObject(counter, 38, 49);
        counter.setLocation(345,47);

        counter.setLocation(135,46);
        removeObject(counter);
    }
}
import lang.stride.*;
import java.util.*;
import greenfoot.*;

/**
 * 
 */
public class Counter extends Actor
{

    /**
     * Act - do whatever the Counter wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment.
     */
    int score = 0;
    
    public void act()
    {
        setImage(new GreenfootImage("Score :" + score, 20, Color.WHITE, Color.BLACK));
    }
    
    public void addScore()
    {
        score++;
    }
}
import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Emoti1 extends Actor { /** * Act - do whatever the Emoti1 wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); } public void move() { int x = getX(); int y = getY(); if(Greenfoot.isKeyDown("right"))x++; if(Greenfoot.isKeyDown("left"))x--; if(Greenfoot.isKeyDown("up"))y--; if(Greenfoot.isKeyDown("down"))y++; setLocation(x,y); if(isTouching(Emoti2.class)); removeTouching(Emoti2.class); } } the score counter is displayed but does not scroll when the emoticon 1 touches the emoticon 2 help me please
Super_Hippo Super_Hippo

2019/6/24

#
ronald wrote...
but does not scroll
What do you mean with that and where are you trying to achieve it within your codes?
ronald ronald

2019/6/24

#
all i'm trying to do is just muddy the code and understand, for example, i try to score when an emoticon 1 touches the emoticon 2 with arrows, i'm not going out a game quite done , I miss it, three lines of code, I think, but I can not display 1 when the emoticon 1 touches the emoticon 2, it's all stupid as a trick, that's it
Super_Hippo Super_Hippo

2019/6/24

#
MyWorld: Change "GetCounter" in line 20 to "getCounter" Remove lines 36-41. Use the following in Emoti1:
if (isTouching(Emoti2.class))
{
    ((MyWorld) getWorld()).getCounter().addScore();
}
ronald ronald

2019/6/24

#
if I use your line of code, my emoti1 will not be able to touch the emoti2 anymore and will not display the score
danpost danpost

2019/6/24

#
What @Hippo suggested was accurate. The code, however, did not provide the line to "eat" the Emoti2 object:
if (isTouching(Emoti2.class))
{
    ((MyWorld)getWorld()).getCounter().addScore();
    removeTouching(Emoti2.class);
}
Make sure you do everything else:
Super_Hippo wrote...
MyWorld: Change "GetCounter" in line 20 to "getCounter" Remove lines 36-41.
danpost danpost

2019/6/24

#
It might help if you change the Counter class to the following:
public class Counter extends Actor
{
    int score = 0;
    
    public Counter()
    {
        updateImage(); // initial image
    }
    
    private void updateImage()
    {
        setImage(new GreenfootImage("Score :" + score, 20, Color.WHITE, Color.BLACK));
    }
    
    public void addScore()
    {
        score++;
        updateImage(); // new score image
    }
}
This creates a new image for the Counter object only when needed -- not every act step.
ronald ronald

2019/6/25

#
I realized that I put twice counter counter = new counter, normaly it was not displayed when you do delete from the right click, the line of code in world does not erase automatically and moreover i did not change the act
You need to login to post a reply.