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

2015/4/5

incrementing the scoreboard

sammyyyyyy sammyyyyyy

2015/4/5

#
I'm trying to get the scoreboard to increment every time Patrick intersects with a Patty, but he just removes it without incrementing. Any ideas? public class PatP extends Patrick {
public void act() 
        {
            setImage("patrickp.png");
            add(); //for scoreboard
            wrong(); //ends game if runs into slop
            checkKeys1(); //to move

        }    
...
Super_Hippo Super_Hippo

2015/4/5

#
Show the 'increment' method in the PattyCount class.
sammyyyyyy sammyyyyyy

2015/4/5

#
public void makeImage()
    {
        GreenfootImage img = new GreenfootImage(WIDTH,HEIGHT);
        img.setColor(bckg);
        img.fill();
        img.setColor(ink);
        Font f = img.getFont();
        f = f.deriveFont(FONT_SIZE);
        img.setFont(f);
        String s = label + value + " " + units;
        img.drawString(s,10,30);        
        setImage(img);
    }
    
    public void increment()
    {
        value++;
        makeImage();
    }
sammyyyyyy sammyyyyyy

2015/4/5

#
oh, and here's the "add" method
public void add()
    {
       //increment score
       if (isTouching(Patties.class))
       {
           PatPatty p = ((PatPatty)getWorld());
           PattyCount pa = p.getPattyCount();
           pa.increment();
           removeTouching(Patties.class);
       }
    }
Super_Hippo Super_Hippo

2015/4/5

#
Does 'getPattyCount' return the PattyCount which is in the world and showing the score?
sammyyyyyy sammyyyyyy

2015/4/5

#
yes
sammyyyyyy sammyyyyyy

2015/4/5

#
this is the world code:
public class PatPat extends World
{
   
    public PattyCount thePattyCount;
    public int rand;
    public int randX;
    public int timer = 0;
    
    public PatPat()
    { 
        super(1000, 800, 1);
        setBackground("patpatbackground.jpg");
        addObject(new PatP(), 87, 602);
        addObject(new Patties(), 500, 100);
        setPaintOrder((PatP.class), (Patties.class), (Slops.class));
        thePattyCount = new PattyCount();
        addObject(new PattyCount(), 132,186);
    }
    public PattyCount getPattyCount()
    {
        return thePattyCount;
    }
danpost danpost

2015/4/5

#
Line 16 assigns 'thePattyCount' field one PattyCount object and line 17 puts a different one in the world. The one in the world is not the one being incremented and you will not see the incrementing of the one that is. Restrict your code to only one 'new PattyCount()' and you should be fine.
winnerpig winnerpig

2015/4/5

#
danpost wrote...
Line 16 assigns 'thePattyCount' field one PattyCount object and line 17 puts a different one in the world. The one in the world is not the one being incremented and you will not see the incrementing of the one that is. Restrict your code to only one 'new PattyCount()' and you should be fine.
All right! You should just change the code in Line17 like this:
        addObject(thePattyCount, 132,186);
You need to login to post a reply.