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

2019/1/29

My Score Is Not Incrementing When I Pickup A Coin

denizkb denizkb

2019/1/29

#
So what I am trying to do is when my sprite it touching the coin (emerald) the coin should be removed and then score should be incremented by one but it is not working and I am running into errors. Here are the classes that are necessary
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class score here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class score extends OTHER
{
    int score = 0;
    public void updateImage() 
    {
        setImage(new GreenfootImage("Score: " + score, 20, Color.WHITE, Color.BLACK));
    }
    
    public score()
    {
        updateImage();
    }
    
    public void addScore()
    {
        score++;
        updateImage();
    }
}   

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

/**
 * Write a description of class coins here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class coins extends OBJECTS
{
    private score score;
    public void act() 
    {
        scrollingMethods();
        checkCoins();
    }
    public void checkCoins()
    {
        Actor sprite = getOneObjectAtOffset(0,0,sprite.class);
        if (sprite != null)
        {
            score++;
            getWorld().removeObject(this);
        }
    }
}
I would appreciate the help!
danpost danpost

2019/1/29

#
denizkb wrote...
Here are the classes that are necessary << Codes Omitted >>
Actually, those are not the main classes necessary. Those that would normally be necessary are your world class and sprite class codes. However, I have spotted one problem in your coins class and this has to do with a misunderstanding. Fields like in line 11 are given to objects created from the class. Each object created gets its own field initialized to zero (default for int fields). Once an object is removed from the world and all references to it are lost, its field is lost also. So, with what you currently have, all coins in the world have a score value of zero and once a coin has its score value incremented to one, it is removed from the world. At this point, again, all coins in the world have a score value of zero. If you think about it, a coin object should not have a score at all (maybe the sprite object should have it).
danpost danpost

2019/1/29

#
Oops. I just noticed the type of that field at line 11. It should hold a score object; yet you are treating it as an int at line 22. Also, as an object reference field, its default value is null and no score object is ever assigned to it.
danpost danpost

2019/1/29

#
The coins class should be a simple class:
public coins extends OBJECTS{}
The OBJECTS class can apply the scrolling to all objects of its subclasses (unless they need to override the act method)::
public void act()
{
    scrollingMethods();
}
denizkb denizkb

2019/1/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MyWorld extends World
{
    private Scroller scroll;
    public int scene;
    public int spriteStartX;
    public int spriteStartY;
    score score = new score();

    
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1300, 860, 1,false);
        Greenfoot.setSpeed(50);
        setPaintOrder(block2.class, sprite.class);
        levelOne();
        getScore();
    }

    public void levelOne()
    {
        sprite sprite = new sprite();
        addObject(sprite,41,753);
        block2 block2 = new block2();
        spike spike = new spike();
        boxPlatform boxPlatform = new boxPlatform();
        
        for (int x = 15; x < 1500; x += 47)
        {
            for (int y = 830; y <= 860; y += 47)
            {
                addObject( new block2(), x, y);
            }
        }
        
        for (int x = 1510; x < 1690; x += 47)
        {
            for (int y = 840; y <= 870; y += 47)
            {
                addObject( new spike(), x, y);
            }
        }
        
        for (int x = 1700; x < 2500; x += 47)
        {
            for (int y = 830; y <= 860; y += 47)
            {
                addObject( new block2(), x, y);
            }
        }
        
        for (int x = 2510; x < 2750; x += 47)
        {
            for (int y = 840; y <= 870; y += 47)
            {
                addObject( new spike(), x, y);
            }
        }
        
        for (int x = 2730; x < 3600; x += 47)
        {
            for (int y = 830; y <= 860; y += 47)
            {
                addObject( new block2(), x, y);
            }
        }
        
        for (int x = 3610; x < 3790; x += 47)
        {
            for (int y = 840; y <= 870; y += 47)
            {
                addObject( new spike(), x, y);
            }
        }
        
        for (int x = 3800; x < 4600; x += 47)
        {
            for (int y = 830; y <= 860; y += 47)
            {
                addObject( new block2(), x, y);
            }
        }
        
        for (int x = 1500; x < 1700; x += 47)
        {
            for (int y = 650; y <= 650; y += 47)
            {
                addObject( new boxPlatform(), x, y);
            }
        }
        
        for (int x = 2510; x < 2700; x += 47)
        {
            for (int y = 650; y <= 650; y += 47)
            {
                addObject( new boxPlatform(), x, y);
            }
        }
        
        for (int x = 3400; x < 3625; x += 47)
        {
            for (int y = 650; y <= 650; y += 47)
            {
                addObject( new boxPlatform(), x, y);
            }
        }
        
        for (int x = 3775; x < 4000; x += 47)
        {
            for (int y = 650; y <= 650; y += 47)
            {
                addObject( new boxPlatform(), x, y);
            }
        }
        
        addObject(new CactusEnemy(), 1600, 590);
        addObject(new CactusEnemy(), 2600, 590);        
        addObject(new CactusEnemy(), 3500, 590);
        addObject(new CactusEnemy(), 3875, 590);
        addObject(new CactusEnemy(), 4200, 760);
        addObject(new Enemy(), 2025, 773);
        addObject(new Enemy(), 3300, 773);
        addObject(new Enemy(), 3850, 773);
        addObject(new boxPlatform(), 1600, 450);
        addObject(new coins(), 1600, 385); 
        addObject(new coins(), 2540, 735);
        addObject(new coins(), 3000, 555);
        addObject(new coins(), 3590, 750);
        addObject(new coins(), 4125, 420);
        addObject(new portal(), 4540, 740);
        addObject(new score(), 75, 50);
        
        
        
        
    }
    public void worldBGScene()
    {
        GreenfootImage bg = new GreenfootImage ("BG.png");
    }

    public void scrollSpeed(int amount)
    {
        scroll.checkKeyPress(amount);
    }
    
    public score getScore()
    {
        return (score)getObjects(score.class).get(0);
    }
}
This is MyWorld code if that helps, I am still stuck and the score is not incrementing :/
danpost danpost

2019/1/30

#
denizkb wrote...
<< Code Omitted >> This is MyWorld code if that helps. I am still stuck and the score is not incrementing ;/
Change line 132 to:
addObject(score, 75, 50);
and line 150 to
return score;
If problem persist, show sprite class next.
denizkb denizkb

2019/2/4

#
danpost wrote...
denizkb wrote...
<< Code Omitted >> This is MyWorld code if that helps. I am still stuck and the score is not incrementing ;/
Change line 132 to:
addObject(score, 75, 50);
and line 150 to
return score;
If problem persist, show sprite class next.
Thank you so much, the score now increments whenever I collect a coin. I would kindly like to ask if you could help me with the lives counter: https://www.greenfoot.org/topics/61747/0#post_130426
You need to login to post a reply.