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

2023/2/16

why doesn't my code work

Magzlum Magzlum

2023/2/16

#
private int spawnTimer; public void act() { if (start == 1 ) { timeCount.setValue(tim.millisElapsed() / 5000); } spawnTimer = (spawnTimer+2)%180; if (spawnTimer == 0) addObject(new enemy(), 1589, 463); if (start == 1 ) { timeCount.setValue(tim.millisElapsed() / 5000); } spawnTimer = (spawnTimer+2)%180; if (spawnTimer == 0) addObject(new dog(), 1589, 463); } this is a code i try to use to spawn in 2 enemies at a different time the dogs are supposed to be added like 15 seconds in and the enemy from the begin
danpost danpost

2023/2/16

#
Magzlum wrote...
private int spawnTimer;

public void act()
{
    if (start == 1 )
    {
        timeCount.setValue(tim.millisElapsed() / 5000);
    }
    spawnTimer = (spawnTimer+2)%180;
    if (spawnTimer == 0) addObject(new enemy(), 1589, 463);
          
    if (start == 1 )
    {
        timeCount.setValue(tim.millisElapsed() / 5000);
    }
    spawnTimer = (spawnTimer+2)%180;
    if (spawnTimer == 0) addObject(new dog(), 1589, 463);
}
Here is your code with line numbers, so it can be discussed properly.
danpost danpost

2023/2/16

#
Magzlum wrote...
<< Code Omitted >> this is a code i try to use to spawn in 2 enemies at a different time the dogs are supposed to be added like 15 seconds in and the enemy from the begin
On lines 7 and 14, your code mentions a timeCount object and a tim object. Please provide information on those, preferably their codes. Or, if the objects are standard greenfoot provided objects, at least show how they are declared and any other codes dealing with them (greenfoot provided class codes need not be provided -- usually). There is also a start field whose declaration line should be shown. I am concerned, however, on the duplication of codes as lines 5 thru 9 seem to match lines 12 thru 16. Wondering why you would double code that. Also, you say it is not working; but, that is vague. Are you saying that nothing is being spawned from the code or something else?
Magzlum Magzlum

2023/3/2

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * A Timer class that allows you to display a numerical value on screen. * * The Timer is an actor, so you will need to create it, and then add it to * the world in Greenfoot. If you keep a reference to the Timer then you * can adjust its value. Here's an example of a world class that * displays a counter with the number of act cycles that have occurred: * * <pre> * class CountingWorld * { * private Timer actCounter; * * public CountingWorld() * { * super(600, 400, 1); * actCounter = new Timer("Act Cycles: "); * addObject(actCounter, 100, 100); * } * * public void act() * { * actCounter.setValue(actCounter.getValue() + 1); * } * } * </pre> * * @author Neil Brown and Michael Kölling * @version 1.0 */ public class Timer extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; private String prefix; public Timer() { this(new String()); } /** * Create a new counter, initialised to 0. */ public Timer(String prefix) { background = getImage(); // get image from class value = 0; target = 999999999; this.prefix = prefix; updateImage(); } /** * Animate the display to count up (or down) to the current target value. */ public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } /** * Add a new score to the current counter value. This will animate * the counter over consecutive frames until it reaches the new value. */ public void add(int score) { target += score; } /** * Return the current counter value. */ public int getValue() { return target; } /** * Set a new counter value. This will not animate the counter. */ public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); } /** * Sets a text prefix that should be displayed before * the counter value (e.g. "Score: "). */ public void setPrefix(String prefix) { this.prefix = prefix; updateImage(); } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent); if (text.getWidth() > image.getWidth() - 20) { image.scale(text.getWidth() + 20, image.getHeight()); } image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } } this is the code of my timer
Magzlum Magzlum

2023/3/2

#
sorry its so long and wha i mean with it isnt working is that only the dog spawns and not the enemy also that the timing is wrong with when the dog spawns
danpost danpost

2023/3/2

#
Magzlum wrote...
only the dog spawns and not the enemy also that the timing is wrong with when the dog spawns
Still don't know what tim is and you have not explained the double coding. I can tell you this, however: because of the double coding, spawnTimer is being bumped up by 4 every act and 180 is divisible by 4; so, it will never be 180 after the first bump of 2 and always be 180 after the second one. That is why only dogs are spawned. If enemy is to spawn at beginning, why not just have the world spawn one during preparing of the world and just spawn the dog after 15 seconds:
private int timer;
private Timer timeCount = new Timer();

public MyWorld()
{
    super(600, 400, 1);
    addObject(new enemy(), 1589, 463);
    // etc.
}

public void act()
{
    timer++;
    timeCount.setValue(timer/60);
    if (timer == 15*60) addObject(new dog(), 1589, 463);
    // etc.
}
// etc.
Magzlum Magzlum

2023/3/9

#
it works good now but can you make it so the enemy and dog keep spawning with a few seconds between every time they spawn but not like 3 or 4 more like 1
danpost danpost

2023/3/9

#
Magzlum wrote...
it works good now but can you make it so the enemy and dog keep spawning with a few seconds between every time they spawn but not like 3 or 4 more like 1
You could use the following at line 15 on the code I provided:
if (timer%(4*60) == 0) addObject(new enemy(), 1589, 463);
if (timer%(4*60) == 2*60) addObject(new dog(), 1589, 463);
Half the power of 2s to spawn even quicker (3 of them in the 2 lines).
Magzlum Magzlum

2023/3/16

#
thanks it works finally do you have a good and easy idea for a game that i can make for school
danpost danpost

2023/3/16

#
Magzlum wrote...
do you have a good and easy idea for a game that i can make for school
Avoiders are the easiest type to create. Adding some type of shielding might be an easy idea. Or, you could add shooting ability to the player, which would be a slight step up. You could continue to add other options later, which again will be stepping up.
You need to login to post a reply.