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

2018/11/29

Score counter broken on level two, works fine on level one

1
2
3
jonah420123 jonah420123

2018/11/29

#
Hi, Im having a problem on the second level of my game. If a 'motorbike' can see a disk (coin) then the level should add one point to that bike's specific counter. There are two counters. There are two bikes, one red, one blue. Trying to get the counter to work on my second level has proven difficult, here is the code on level one;
 if (getWorld() instanceof Arena)
        {
        if(disk!=null)
    {
        Arena arena = (Arena)getWorld();
        arena.removeObject(disk);
        
        Counter2 counter = arena.getCounter(); <----- () Has the error "incompatible types: Counter cannot be converted to Counter2
        counter.bumpCount(1);   
    }
}
The code above is solely to increase the score of the red bike's counter by 1 each time it can see a counter. This code worked fine before i tried to add more code for the second level (ArenaTwo) which stopped any score on the red bike from being added at all, in both level one and two. Here is the code I am trying to get the counter to work with on level two;
        if (getWorld() instanceof ArenaTwo)
        {
        if(disk!=null)
    {
        ArenaTwo arenatwo = (ArenaTwo)getWorld();
        arenatwo.removeObject(disk);
        
        Counter2 counter = arenatwo.getCounter(); <------ Exact same error in with () as there was in my previous code
        counter.bumpCount(1);   
    }
}
The two sections of code are both in my actor called 'redbike' In the other bike, the other player, Ihave the exact same code but just with 'Counter', instead of 'Counter2' to differentiate the two.
danpost danpost

2018/11/29

#
In the level one code, remove the '2' from line 8. The same removal in the level two code should work. There should be no need for two different Counter classes as you can create multiple objects from the same class.
jonah420123 jonah420123

2018/11/29

#
    Actor disk;
    disk = getOneObjectAtOffset(0,0, disk.class);   
    if (getWorld() instanceof Arena)
        {
        if(disk!=null)
    {
        Arena arena = (Arena)getWorld();
        arena.removeObject(disk);
        
        Counter counter = arena.getCounter();
        counter.bumpCount(1);   
    }
}
        if (getWorld() instanceof ArenaTwo)
        {
        if(disk!=null)
    {
        ArenaTwo arenatwo = (ArenaTwo)getWorld();
        arenatwo.removeObject(disk);
        
        Counter thisCounter = arenatwo.getCounter();
        thisCounter.bumpCount(1);   
    }
}
No errors occur but on the second level, however, it does not work as intended. The 'bluebikes' counter goes up when the 'redbike' collects a coin (disk). This occurs on both levels. If the 'blue'bike' collects a coin, the 'bluebikes' counter still goes up correctly.
jonah420123 jonah420123

2018/11/29

#
There are ONLY two levels, Arena (1), and ArenaTwo (2). Here is some other information I believe might help:
public class Arena extends World
{
    private Actor timerDisplay = new timer();
    private int timeElapsed;
    private int timeCounter;
    private Counter theCounter;
    private Counter2 thisCounter;
    private resetredbutton resetButton;
    private boolean redbuttontoggle = true;
    public Arena()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        bluebike bluebike1 = new bluebike();
        addObject (bluebike1, 550, 200);

        redbike redbike1 = new redbike();
        addObject (redbike1, 50, 200);

        updateTime();
        addObject(timerDisplay, 80, 20);

        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());

        disk disk1 = new disk();
        for (int i=0; i<1; i++) addObject(new disk(), 450, 350);

        diska disk2 = new diska();
        for (int i=0; i<1; i++) addObject(new disk(), 400, 300);

        diskb disk3 = new diskb();
        for (int i=0; i<1; i++) addObject(new disk(), 350, 250);

        diskc disk4 = new diskc();
        for (int i=0; i<1; i++) addObject(new disk(), 300, 200);

        diskd disk5 = new diskd();
        for (int i=0; i<1; i++) addObject(new disk(), 250, 150);

        diske disk6 = new diske();
        for (int i=0; i<1; i++) addObject(new disk(), 200, 100);

        diskf disk7 = new diskf();
        for (int i=0; i<1; i++) addObject(new disk(), 150, 50);

        diskg disk8 = new diskg();
        for (int i=0; i<1; i++) addObject(new disk(), x, 350);

        diskh disk9 = new diskh();
        for (int i=0; i<1; i++) addObject(new disk(), 300, y);

        theCounter = new Counter();    
        addObject(theCounter, 500, 25);

        thisCounter = new Counter2();
        addObject(thisCounter, 400, 25);

        resetredbutton resetredButton = new resetredbutton();
        addObject(resetredButton, x, y);
The above is in Arena (level one) Below is in ArenaTwo (level two);
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ArenaTwo extends World
{
    private Actor timerDisplay = new timer();
    private int timeElapsed;
    private int timeCounter;
    private Counter theCounter;
    private Counter2 thisCounter;
    private resetredbutton resetButton;
    private boolean redbuttontoggle = true;
    public ArenaTwo()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        bluebike bluebike1 = new bluebike();
        addObject (bluebike1, 550, 200);

        redbike redbike1 = new redbike();
        addObject (redbike1, 50, 200);

        updateTime();
        addObject(timerDisplay, 80, 20);

        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());

        disk disk1 = new disk();
        for (int i=0; i<1; i++) addObject(new disk(), 450, 350);

        diska disk2 = new diska();
        for (int i=0; i<1; i++) addObject(new disk(), 400, 300);

        diskb disk3 = new diskb();
        for (int i=0; i<1; i++) addObject(new disk(), 350, 250);

        diskc disk4 = new diskc();
        for (int i=0; i<1; i++) addObject(new disk(), 300, 200);

        diskd disk5 = new diskd();
        for (int i=0; i<1; i++) addObject(new disk(), 250, 150);

        diske disk6 = new diske();
        for (int i=0; i<1; i++) addObject(new disk(), 200, 100);

        diskf disk7 = new diskf();
        for (int i=0; i<1; i++) addObject(new disk(), 150, 50);

        diskg disk8 = new diskg();
        for (int i=0; i<1; i++) addObject(new disk(), x, 350);

        diskh disk9 = new diskh();
        for (int i=0; i<1; i++) addObject(new disk(), 300, y);

        theCounter = new Counter();    
        addObject(theCounter, 500, 25);

        thisCounter = new Counter2();
        addObject(thisCounter, 400, 25);

        resetredbutton resetredButton = new resetredbutton();
        addObject(resetredButton, x, y);

    }
    public Counter getCounter()
    {
        return theCounter;
    }

    public Counter2 grabCounter()
    {
        return thisCounter;
    }

    public void act() {
        if(Greenfoot.getRandomNumber(1000) < 20) {}

        timeCounter = (timeCounter+1)%55;

        if (timeCounter == 0){
            timeElapsed++;
            updateTime();

        }
        if (timeElapsed == 30){
            Greenfoot.stop();
        }

        addnewObject();
    }

    public void updateTime(){
        timerDisplay.setImage( new GreenfootImage( "Time : " +timeElapsed, 34, Color.RED, null));

    }

    
    public void addnewObject() {
        if (getObjects(resetredbutton.class).isEmpty() && redbuttontoggle == true)
        {

            addObject(new disk(), 300, 200);
            redbuttontoggle = false;
        }   
    }

}
danpost danpost

2018/11/29

#
Oh -- so, actually 4 counters; two in Arena and two in ArenaTwo. In your World subclasses, create the two counters (Counter instances). Name them redCounter and blueCounter. Have a "getter" method for both of them (getRedCounter and getBlueCounter). Call each from their respective bike classes. Your codes look pretty good. Good job.
jonah420123 jonah420123

2018/11/29

#
So in the 'ArenaTwo' world I put;
public class Arena extends World
{
    private Actor timerDisplay = new timer();
    private int timeElapsed;
    private int timeCounter;
    private Counter theCounter;
    private Counter2 thisCounter;
    private Counter getRedCounter;      ]----- Like this?
    private Counter2 getBlueCounter;   ]----- Like this?
    private resetredbutton resetButton;
    private boolean redbuttontoggle = true;
danpost danpost

2018/11/29

#
Like this:
private Counter redCounter;
private Counter blueCounter;

// later
public Counter getRedCounter()
{
    return redCounter;
}

public Counter getBlueCounter()
{
    return blueCounter;
}
jonah420123 jonah420123

2018/11/29

#
So, I've changed this code
        if (getWorld() instanceof ArenaTwo)
        {
        if(disk!=null)
    {
        ArenaTwo arenatwo = (ArenaTwo)getWorld();
        arenatwo.removeObject(disk);
        
        Counter redCounter = arenatwo.getBlueCounter(); <-----
        counter.bumpCount(1);   <----- 'counter' cannot find symbol - variable counter
    }
}
Also, i added this code to ArenaTwo;
    public Counter getRedCounter()
    {
        return redCounter;
    }
 
    public Counter getBlueCounter()
   {
       return blueCounter;
   }
'redCounter' and 'blueCounter' are "cannot find symbol - variable blueCounter" / redCounter I presume this is because i need to make two seperate counters as actors?
jonah420123 jonah420123

2018/11/29

#
Additionally, I added this part to my 'private ArenaTwo()';
       
        redCounter = new Counter();    
        addObject(theCounter, 500, 25);

        blueCounter = new Counter2();
        addObject(thisCounter, 400, 25);
I did this because I think that I need to make actors for them? Can you confirm or guide me on this?
danpost danpost

2018/11/29

#
Please delete your Counter2 class and change your code above to:
redCounter = new Counter();
addObject(redCounter, 500, 25);
blueCounter = new Counter();
addObject(blueCounter, 400, 25);
jonah420123 jonah420123

2018/11/29

#
Will this result in two counters on level one and level two which is what i am looking for. One red counter, one blue counter, on each of the two levels, that collects disks for each specific bike, e.g. red bike collects a coin, the red counter adds one point.
jonah420123 jonah420123

2018/11/29

#
I misunderstood what you said, trying that now. Ignore above ^
jonah420123 jonah420123

2018/11/29

#
        redCounter = new Counter();    
        addObject(redCounter, 500, 25);

        blueCounter = new Counter();
        addObject(blueCounter, 400, 25);

        resetredbutton resetredButton = new resetredbutton();
        addObject(resetredButton, x, y);

    }
    public Counter getRedCounter()
    {
        return redCounter;
    }
 
    public Counter getBlueCounter()
   {
       return blueCounter;
   }
Same errors - "cannot find symbol - variable redCounter", blueCounter etc... I added the following to my 'blueCounter' and 'redCounter' actors;
public class blueCounter extends Actor
{
    private int totalCount = 0;
    
    public blueCounter()
    {
        setImage(new GreenfootImage("0", 20, Color.RED, Color.BLACK));
    }
    
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount,20, Color.RED, Color.BLACK));
    }   
}
danpost danpost

2018/11/29

#
Remove the redCounter and blueCounter classes. The Counter objects are the actors displaying the counts.
jonah420123 wrote...
Same errors - "cannot find symbol - variable redCounter", blueCounter etc...
Show the code of the class where this error appears.
jonah420123 jonah420123

2018/11/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class ArenaTwo extends World
{
    private Actor timerDisplay = new timer();
    private int timeElapsed;
    private int timeCounter;
    private Counter getRedCounter;
    private Counter2 getBlueCounter;
    private resetredbutton resetButton;
    private boolean redbuttontoggle = true;
    public ArenaTwo()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        bluebike bluebike1 = new bluebike();
        addObject (bluebike1, 550, 200);

        redbike redbike1 = new redbike();
        addObject (redbike1, 50, 200);

        updateTime();
        addObject(timerDisplay, 80, 20);

        int x = Greenfoot.getRandomNumber(getWidth());
        int y = Greenfoot.getRandomNumber(getHeight());

        disk disk1 = new disk();
        for (int i=0; i<1; i++) addObject(new disk(), 450, 350);

        diska disk2 = new diska();
        for (int i=0; i<1; i++) addObject(new disk(), 400, 300);

        diskb disk3 = new diskb();
        for (int i=0; i<1; i++) addObject(new disk(), 350, 250);

        diskc disk4 = new diskc();
        for (int i=0; i<1; i++) addObject(new disk(), 300, 200);

        diskd disk5 = new diskd();
        for (int i=0; i<1; i++) addObject(new disk(), 250, 150);

        diske disk6 = new diske();
        for (int i=0; i<1; i++) addObject(new disk(), 200, 100);

        diskf disk7 = new diskf();
        for (int i=0; i<1; i++) addObject(new disk(), 150, 50);

        diskg disk8 = new diskg();
        for (int i=0; i<1; i++) addObject(new disk(), x, 350);

        diskh disk9 = new diskh();
        for (int i=0; i<1; i++) addObject(new disk(), 300, y);

        redCounter = new Counter();    
        addObject(redCounter, 500, 25);
        blueCounter = new Counter();
        addObject(blueCounter, 400, 25);

        resetredbutton resetredButton = new resetredbutton();
        addObject(resetredButton, x, y);

    }
    public Counter getRedCounter()
    {
        return redCounter;
    }
 
    public Counter getBlueCounter()
   {
       return blueCounter;
   }

    public void act() {
        if(Greenfoot.getRandomNumber(1000) < 20) {}

        timeCounter = (timeCounter+1)%55;

        if (timeCounter == 0){
            timeElapsed++;
            updateTime();

        }
        if (timeElapsed == 30){
            Greenfoot.stop();
        }

        addnewObject();
    }

    public void updateTime(){
        timerDisplay.setImage( new GreenfootImage( "Time : " +timeElapsed, 34, Color.RED, null));

    }

    
    public void addnewObject() {
        if (getObjects(resetredbutton.class).isEmpty() && redbuttontoggle == true)
        {

            addObject(new disk(), 300, 200);
            redbuttontoggle = false;
        }   
    }

}
There are more replies on the next page.
1
2
3