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

2014/9/1

HELP ME SCORE COUNTER PROBLEM

1
2
sharifahhilwa sharifahhilwa

2014/9/1

#
I have my actor trying to "eat" its gold and the score adds to one, but its not working. please help me!!!! very urgent. /** * Act - do whatever the Crosser wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { eat(); } } public void eat() { Actor gold; gold = getOneIntersectingObject(Gold.class); if (gold != null) { World myWorld = getWorld(); myWorld.removeObject(gold); RoadWorld RoadWorld = (RoadWorld)myWorld; Counters Counters = RoadWorld.getCounters(); Counters.addScore(); RoadWorld.removeObject(this); } } The problems are occuring on my actor crosser. My counter world class has no problem.
lordhershey lordhershey

2014/9/1

#
It the problem that when you eat gold the actor disappears as well?
danpost danpost

2014/9/1

#
In the middle of the 'if' block, you are declaring a variable, 'RoadWorld', with the exact name of the class. This is probably confusing the compiler. Change the name of the variable to 'roadWorld' (with a small 'r') and use it throughout the rest of the block.
sharifahhilwa sharifahhilwa

2014/9/1

#
@lordhershey yes the problem is when i eat the gold, my actor disappear. i dont want that. i only want my gold to disappear
sharifahhilwa sharifahhilwa

2014/9/1

#
@danpost ok will try and update if any mistake occur
sharifahhilwa sharifahhilwa

2014/9/1

#
btw it still doesnt work, i still have my actor missing after i try to eat my gold
danpost danpost

2014/9/1

#
That last line:
roadWorld.removeObject(this);
will do that. Remove it.
sharifahhilwa sharifahhilwa

2014/9/1

#
@danpost great!!!!!! it works but then how do i get the score counter to move everytime it eats the gold. my counter class public class Counters extends Actor { int score = 0; /** * Act - do whatever the Counter wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Score ; " + score, 24, Color.BLUE, Color.BLACK)); } public void addScore() { score++; } }
danpost danpost

2014/9/1

#
Are you getting any error messages? or is the counter just not showing any changes in score?
sharifahhilwa sharifahhilwa

2014/9/1

#
nope not getting errorrmessages just that the score is not showing
danpost danpost

2014/9/1

#
Make sure the score object in the world is the same one that you have referenced in your world class. If you are unsure what I mean, post your world class code.
sharifahhilwa sharifahhilwa

2014/9/2

#
@danpost thats all i put. i followed thru a youtube video by jim stewart public Counters getCounters() { return counters; }
danpost danpost

2014/9/2

#
You should have the 'counters' field declared in the world class; plus, you should have the Counters object being created and added into the world somewhere in the world class. Again, maybe you should post your world class code. Oh, and please use the 'code' link below the 'Post a reply' content box to insert your code into your post.
sharifahhilwa sharifahhilwa

2014/9/3

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

/**
 * Write a description of class roadWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class roadWorld extends World
{
    
    GreenfootSound MyMusic = new GreenfootSound("Original Tetris theme (Tetris Soundtrack).wav");
    GreenfootSound GameOverSound = new GreenfootSound("GameOver.wav");
    GreenfootSound MySmush = new GreenfootSound("smush.wav");
    GreenfootSound LevelUpSound = new GreenfootSound("mariocoin.mp3");
    
    // instance variables
     
    private int Count = 0;
    private int spawnRate;
    private int spawnY;
    private int vehicleSpeed;
    private int Ynumber;

    private int lives = 3;

    private int level;
    private int autoCount;
    private Level currentLevel;
    private boolean levelLoaded = false;
    private boolean spawning;

    private boolean actionPaused = false;
    private boolean showScore = false;
    private boolean deathSequenceActive = false;
    
    // Not implemented yet:
    private int chanceOfDouble;
    private int chanceOfTriple;

    // Sounds and Graphics loading
    private GreenfootImage RunImage = new GreenfootImage ("person.png");
    private GreenfootImage NoMen = new GreenfootImage ("Dead.png");
    
    // For some reason sounds work better if initialized in the constructor
    

   
    // owned instance objects
    private LifeCounter lifeCounter = new LifeCounter ();
    private LevelCounter levelCounter = new LevelCounter ();
    private Crosser myRun = new Crosser();
    
    Counters counters = new Counters();
    /**
     * Constructor for objects of class Desert.
     */
    public roadWorld()
    {    
        // Create a new world with 800x600 cells with a cell size of 1x1 pixels.
        super(150, 100, 4);  
        prepare();
    
        // Ensure that Crosser and Blood occur over top of everything else
        setPaintOrder(Crosser.class, BloodPool.class, ScoreBoard.class, Enemy.class);

        // Add the life counter to the World
        addObject (lifeCounter, 20, 5);
        addObject (levelCounter, 130, 5);
        
        // Initialize sound within the constructor to avoid wierd sound lag
        MyMusic = new GreenfootSound("Original Tetris theme (Tetris Soundtrack).wav");
        

        
        // Set level to 1
        level = 1;
        autoCount = 0;
        

    }
    
    public Counters getCounters()
    {
        return counters;
    }
        
    public void act ()
    {
        // Load level if necessary
        if (levelLoaded == false)
        {
            // Load level if player has lives left
            if (lives > 0)
            {
                levelLoader();
                levelLoaded = true;
            }
            else 
            {
                ScoreBoard s = new ScoreBoard (level,"Game Over", "On Level");
                addObject (s, 70,50);
                GameOverSound.play();
        
                // End program
                Greenfoot.stop();
                
            }
            }

        // If appropriate, spawn cars
        if (actionPaused == false && spawning == true)
        {
            spawnCars();
        }

        // Control sequence to ensure that after death, program waits until
        // all cars are off-screen before spawning the next Crosser
        if (deathSequenceActive)
        {
            if (autoCount == 0)
            {
                deathSequenceActive = false;
                levelLoaded = false;
            }
        }
        {
            MyMusic.play();


        }

    }

    // Method to set necessary values to change level
    private void levelLoader ()
    {
        // Add a fresh RunningMan object
        addObject(myRun, 70, 95);
        
        // Update the info stored in currentLevel
        currentLevel = new Level (level);

        levelCounter.setLevel(level);
        
        // Set image to frog (in case "NoMen" was being displayed)
        myRun.setImage(RunImage);
      
        // change appropriate booleans for game flow
        actionPaused = false;
        spawning = true;

        // load instance variables from Level
        spawnRate = currentLevel.getSpawnRate();
        vehicleSpeed = currentLevel.getVehicleSpeed();
        chanceOfDouble = currentLevel.getChanceOfDouble();
        chanceOfTriple = currentLevel.getChanceOfTriple();
        myRun.setSpeed(currentLevel.getCrossermovement());

        // Ensure that level is not loaded again until necessary
        levelLoaded = true;
    }
    
     /**
     * Controlled spawning of new cars
     */
    private void spawnCars()
    {
        int randVal = Greenfoot.getRandomNumber(3)+1;
        int spawnChance = Greenfoot.getRandomNumber(100) + 1;
        
        // spawn single
        if (Count % spawnRate == 0)
        {
            if (randVal == 1)
                spawnY = 15;
            else if (randVal == 2)
                spawnY = 20;
            else
                spawnY = 35;
            addObject(new EastCar(vehicleSpeed), 20, spawnY);
            autoCount++;
            
        }
        
       else if (Count % spawnRate == spawnRate / 2)
        {
            if (randVal == 1)
                spawnY = 25;
            else if (randVal == 2)
                spawnY = 45;
            else
                spawnY = 40;
            addObject(new WestCar(vehicleSpeed), 150, spawnY);
            autoCount++;
            
        }
        
          else if (Count % spawnRate == spawnRate / 4)
        {
            if (randVal == 1)
                spawnY = 35;
            else if (randVal == 2)
                spawnY = 60;
            else
                spawnY = 55;
            addObject(new EastAuto(vehicleSpeed), 20, spawnY);
            autoCount++;
            
        }
        
        else if (Count % spawnRate == spawnRate / 6)
        {
            if (randVal == 1)
                spawnY = 70;
            else if (randVal == 2)
                spawnY = 70;
            else
                spawnY = 75;
            addObject(new WestLorry(vehicleSpeed), 150, spawnY);
            autoCount++;
            
        }
        Count++;
    }
    
    /**
     * 
     */
    private void spawn (int num, int y, boolean east)
    {
        
    }
    
    /**
     * This method gets called when the player dies or finishes a level
     * 
     * True = player completed level
     * False = player died
     */
    public void endLevel(boolean win)
    {
        ScoreBoard s = null;
        actionPaused = true;
        spawning = false;
        
        if (win == true)
        {
            level++;
            LevelUpSound.play();
            removeObject (myRun);
            levelLoaded = false;
            Greenfoot.delay(50);

        }
        else
        {
            //Play a smush sound
            MySmush.play();
  
            BloodPool b = new BloodPool();
            addObject (b, myRun.getX(), myRun.getY());

            lives -= 1;
            lifeCounter.subtractLife();
            actionPaused = false;
            
            deathSequenceActive = true;

            removeObject (myRun);
        }
    }
    
    //create a method for adding a life------------------------------------------------------------
    public void collectHeart() 
    {
        lives++;
        lifeCounter.addLife();
    }
    //---------------------------------------------------------------------------------------------

    public boolean isActionPaused ()
    {
        return actionPaused;
    }

    public void reduceAutoCount (int byHowMany)
    {
        autoCount -= byHowMany;
    }

    
    /**
     * Prepare the world for the start of the program. That is; create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Counters counters = new Counters();
        addObject(counters, 100,4);

        Gold gold = new Gold();
        addObject(gold, 65, 18);
        Gold gold2 = new Gold();
        addObject(gold2, 89, 25);
        Gold gold3 = new Gold();
        addObject(gold3, 70, 50);
        Gold gold4 = new Gold();
        addObject(gold4, 90, 80);
    }
}


    
    
sharifahhilwa sharifahhilwa

2014/9/3

#
that is my world.class code and this is my counter class code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counters extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score ; " + score, 24, Color.BLUE, Color.BLACK));
    }   
    
    public void addScore()
    {
        score++;
    }
}
There are more replies on the next page.
1
2