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

2013/9/24

How to let object spawn randomly after a certain time

Syntac Syntac

2013/9/24

#
Hey guys, So this is my first project I´ve to do by myself. Basicly I´m doing something like an ego shooter. The game is about like this. You are the Scope and you´ve to shoot randomly appearing object, but the object disappears after some seconds so you´ve to be fast because the game is after one or two minutes over. The destroyed object will be counted during the game- like a score. What I´ve already: - if I press the mouse, muzzle fire appears - if I release the mouse, the muzzle fire disappears - if I click on the object I want to destroy, an image of the exploding object appears What I don´t know how to do: - How the object can randomly appear after a certain time and if it hasn´t been shot, disappear after 1/2 sec - How the object, after it has been clicked (shot on) on, disappears.
danpost danpost

2013/9/24

#
The MouseInfo class has a method to 'getActor' and the World class has a method to 'removeObject'. For the counting of approximately 30 cycles, add an instance field to the class of the randomly appearing actors and increment it in the act method, checking each time for the value of 30; when it is 30, remove the object. As a sidenote, to be more complete, the Actor class has a method to 'getWorld'
Syntac Syntac

2013/9/24

#
Ok I´ll this. I tried something out before and it doesn´t worked, maybe you can explain to me why. I put this in the class of the object I want to remove after it has been clicked on. public void destroyObject() { if (Greenfoot.mouseClicked(this)) { set Image("Explosion.png"); // This is what happens if I click on it, first a picture of an explosion should appear removeObject(Object.class); //This is what should happens after the explosion } }
Syntac Syntac

2013/9/24

#
So I changed the code now. public void destroyObject() { if (Greenfoot.mouseClicked(this)) { set Image("Explosion.png"); // This is what happens if I click on it, first a picture of an explosion should appear removeObject(this); //This is what should happens after the explosion } } But now the object disappears immediately, so there is no explosion. How can I put a delay into it? So that it´s like this. I hit the object, the image of an explosion appears for a sec or two and then the whole object disappears?
danpost danpost

2013/9/24

#
The best thing to do is create a new actor subclass for the explosion and add it at the location of the exploding object before removing that object. The act method of the new Explosion class can count so many act cycles before removing itself. I noticed that you are using 'setImage', and in the second post you used 'removeObject(this)'; so, I believe you are trying to code the adding of the explosion and removal of the exploding object at the correct place. However, to remove the object, you need the world instance to remove the object from. Use 'getWorld().removeObject(this);'.
Syntac Syntac

2013/9/24

#
Thank you very much!! :) Ok, so it´s almost done I guess. -Objects are randomly spawning -Objects are disappearing if hit So now I only need to know: -how I can make objects appear for a sec and then disappear and it should automatically create a new object if one is gone I´ve an idea, but I don´t know if it is the smartest way. What I thought of, is this. If I hit one object it should disappear but at the same time it should double itself or create a copy of himself
danpost danpost

2013/9/24

#
To create new object after one is gone: In the world act method get the size of the list that getObjects of that class returns. If it is less than your minimum number of that type object allowed, add one (this can be chanced). With this, you will not have to add any to start. To have an object disappear after a sec or two: In the actor class add an instance int field and set it to 30, or 40, or so, and in the act method decrement its value and check for it equaling zero; if so, have it remove itself.
Syntac Syntac

2013/9/24

#
Great, it works :) Thank you! Is it possible, to put a counter in the game? What I want is, that everytime a object diappears the counter should count. And my final question. I want that the game is over after one minute. Do I have to write this into my world? And how would this code look like? I guess this would be wrong: time = System.currentTimeMillis(); } public void act() { if (time < time + 200) { Greenfoot.stop(); } }
danpost danpost

2013/9/24

#
Using the system time is not recommended. The actual amount of gameplay would be different on one system from the next. Better is to limit the number of act cycles (a minute is usually between 3000 and 3600 act cycles, presuming the scenario speed is about 50 to 60 cycles per second). The world class would probably be the best place to put the code. BTW, the way you coded the system time code above, you are setting 'time' to a value when declaring the field; then in the act method you are comparing that value with 200 more than that value and asking if it is less -- a value is always less than 200 more than that value (I am not sure what you were thinking -- maybe that one of the 'time' values would be updated without calling 'System.currentTimeMillis' again?).
Syntac Syntac

2013/9/25

#
Can you give me an example, because I´ve no idea how to do that.
danpost danpost

2013/9/25

#
The following is all done in your subclass of world. First, declare an instance int field and call it timer. Increment the timer in the act method. After incrementing it, use an if statement checking the value of the timer. If timer is large enough, stop the scenario.
Syntac Syntac

2013/9/25

#
Ok, it´s still a bit confusing but I will try ;) Now as my game is so far done I thought of a startscreen. If I want to implent a start screen, do I have to do another world class? And I would need something like a button, which if I hit it leads me to the actual game. So I guess I have to write it so, that "start screen" appears first and the button is an extra class, is this correct?
danpost danpost

2013/9/25

#
There are several ways to implement the removal of a start screen. (1) Have your start-up screen be your start screen and when the player clicks on the 'Run' button, change the background and add the actors; (2) Have your start screen removed when a key or mouse click is registered (if you prefer the mouse click be on a button, it can be an actor or drawn on the background; the detection of the click and how it was coded would be different depending on which way you go); (3) Have the start-up screen removed after so many act cycles have run or after so much time has elapsed. Using a different world for a start-up screen would still have one of the above be utilized. There are several advantages to having separate worlds, however. One is that you can use it to have the player select options and go back to it during game-play without interfering with the state of the game (it takes extra coding to accomplish this if done in the same world). So, in answer to your questions, (1) no, you do not have to do another world class, (2) no, you would not need something like a button, and (3) no, you do not have to write it so (but you could).
You need to login to post a reply.