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

2019/1/30

Score is not increasing when I pickup a coin

denizkb denizkb

2019/1/30

#
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. MyWorld code.
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;
    
    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();
    }

    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);
        score = new score();
        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;
    }
}
Coins class.
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();
        
        Actor sprite = getOneObjectAtOffset(0,0,sprite.class);
        if (sprite != null)
        {
            ((MyWorld)getWorld()).getScore().addScore();
            getWorld().removeObject(this);
        }
    }
}
Score class.
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 Actor
{
    int score = 0;
    
    public score()
    {
        updateImage();
    }  
    public void updateImage()
    {
        setImage(new GreenfootImage("Score: " + score, 20, Color.WHITE, Color.BLACK));
    }
    public void addScore()
    {
        score++;
        updateImage();
    }
}
Claw76 Claw76

2019/1/30

#
if(isTouching(coins.class)) {
            removeTouching(null);
            Score.addScore();
        }
This is the code I came up with, when seeing your project and your problem. You need to put it into the sprite class I hope it will help you. (I didn't test it)
You need to login to post a reply.