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

2018/3/25

How do I add an actor based on the score?

1
2
UnKnown89 UnKnown89

2018/3/25

#
I have a working score counter and I would like to add a new actor based on the score. How would I code something to do this?
danpost danpost

2018/3/25

#
UnKnown89 wrote...
I have a working score counter and I would like to add a new actor based on the score. How would I code something to do this?
How do you have the score coded?
UnKnown89 UnKnown89

2018/3/25

#
Yes public class Counter extends Actor { int score=0; public Counter() { setImage(new GreenfootImage("Score: 0", 25, Color.WHITE, Color.BLACK)); } public void changeScore(int num) { score = score + num; setImage(new GreenfootImage("Score: "+ score, 25, Color.WHITE,Color.GREEN)); } public int returnScore() {return score;} }
UnKnown89 UnKnown89

2018/3/25

#
It does work successfully but I don't know how to code a way to add an actor based on score
danpost danpost

2018/3/25

#
Okay. A basic Counter class with a score field and a public method to return its value. Now, what codes do you have in your World subclass related to the Counter class and object(s) created from the class? Also, are you aware that you can add an act method into your subclass of World?
UnKnown89 UnKnown89

2018/3/25

#
private Counter scoreCounter; public Counter getCounter() { return scoreCounter; } those are the code in my world that relate to the counter but no I was not aware of this
danpost danpost

2018/3/25

#
UnKnown89 wrote...
no I was not aware of this
Knowing this, do you still require assistance? Keep in mind that the value of the Counter object may remain steady for multiple act cycles and any action taken at a value should be regulated so as to perform only once.
UnKnown89 UnKnown89

2018/3/25

#
The problem is I don't know the exact code to make a conditional statement saying if score = 500 (for example) to spawn in an enemy. How would I code it?
UnKnown89 UnKnown89

2018/3/25

#
I have tried to do this but there are lots of errors. This is an attempt to add a helicopter (separate class/actor) into the game when the score equals 100. public void Helicopter() { Actor helicopter = getOneObjectAtOffset(0, 0, Helicopter.class); if(Counter != 100) { World ocean = getWorld(); ocean.addObject(helicopter); }}
danpost danpost

2018/3/25

#
UnKnown89 wrote...
The problem is I don't know the exact code to make a conditional statement saying if score = 500 (for example) to spawn in an enemy. How would I code it?
That is the easier part:
if (scoreCounter.getScore() == 500  &&  /*  some regulating condition) */ )
{
    addObjecct(new Boss(), 550, 375);
}
The harder part is what the other condition should be. Of course, suitable replacements are required for Boss 550 and 375 .
danpost danpost

2018/3/25

#
UnKnown89 wrote...
I have tried to do this but there are lots of errors. This is an attempt to add a helicopter (separate class/actor) into the game when the score equals 100. << Code Omitted >>
Alright. Now that I see you will be adding actors at multiple score values, it is apparent you will need an int field in your ocean class to track the score (do not confuse this with keeping the score). This field will provide an appropriate regulating condition. You would have this in your world class:
// regulating field
private int maxScore;

// act method
public void act()
{
    int score = scoreCounter.getScore();
    if (score > maxScore)
    {
        maxScore = score;
        if (maxScore == 100) addObject(new Helicoptor(), 550, 500;
        if (maxScore == 500) addObject(new Boss(), 550, 375);
    }
}
Remove the public void Helicoptor method from your Actor subclass. Please note that the score must land on these values of 100 and 500 exactly at the end of an act cycle for the actors to spawn.
UnKnown89 UnKnown89

2018/3/25

#
That is fantastic thank you very much.
UnKnown89 UnKnown89

2018/3/25

#
int score = scoreCounter.getScore();
Fantastic but it says it cannot find scoreCounter is there an alternative I should be using or is it a method that should be declared? Every time I try to change it to something else it results in more errors so I'm at loss at what to do.
danpost danpost

2018/3/25

#
UnKnown89 wrote...
it says it cannot find scoreCounter is there an alternative I should be using or is it a method that should be declared?
I got that from code you provided here. Show your world class code for review if you continue to have problems with it.
UnKnown89 UnKnown89

2018/3/26

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

/**
 * Write a description of class CopyOfMyWorld2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CopyOfMyWorld2 extends World
{
private MainCar myMainCar;
 private Counter scoreCounter;
Dead dead = new Dead();
private int maxScore;
    /**
     * Constructor for objects of class CopyOfMyWorld2.
     * 
     */
    public CopyOfMyWorld2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
       scoreCounter = new Counter();
       addObject(scoreCounter ,40,15);
       addObject( dead,720,15);
    }
 public Dead getDead()
    { return dead;

    }

    public Counter getCounter()
    {
        return scoreCounter;
    }
    

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {

        Police popo = new Police();
        addObject(popo,783,332);
        Police popo2 = new Police();
        addObject(popo2,724,339);
        popo2.setLocation(721,332);
        removeObject(popo2);
        removeObject(popo);
        Police police3 = new Police();
        addObject(police3,789,323);
        Money money = new Money();
        addObject(money,647,27);
        Money money2 = new Money();
        addObject(money2,22,22);
        Money money3 = new Money();
        addObject(money3,643,554);
        MainCar maincar = new MainCar();
        addObject(maincar,72,488);
        Tank tank = new Tank();
        addObject(tank,71,46);
        Helicopter helicopter = new Helicopter();
        addObject(helicopter,771,34);
    }
   
  


}
World class is above the error is on the actor class where a variable couldn't be found.
There are more replies on the next page.
1
2