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

2015/10/30

Remove an object after time

BroTolo BroTolo

2015/10/30

#
I'm trying to spawn an object every 500 points which i need help with on, as well as removing the object after 4 seconds. Alot of the code below is more trial and error. If the score is at 500 i need the laser spawned, then after 4 seconds it gets removed, then after another 500 points(so now at 1000) id like another spawned and then removed after 4 seconds
1
2
3
4
5
6
7
8
9
10
11
if(scoreV == 50)
        {
            if(getWorld().getObjects(Laser.class).size() != 1)
            {
                getWorld().addObject(new Laser(),(Greenfoot.getRandomNumber(1050) + 50), 215);
                   if(timer == 0)
                {
                    getWorld().removeObjects(getWorld().getObjects(Laser.class));
                }
          
        }
danpost danpost

2015/10/30

#
The lack of background and code never ceases to amaze me. * What class is this snippet of code in? * Where in the class is the snippet located (what method)? * How and where is the 'timer' variable declared? * Why are you only checking for a 'scoreV' value of 50? * Why would you try to remove a Laser object in a block of code that starts with no Laser objects in the world? (only if 'timer' was already '0' when the laser was added into the world would a laser be removed; in other words, if a laser is already in the world at the time this snippet is executed, no actions -- adding or removing of Laser object -- would take place) * What code do you have for the Laser class?
BroTolo BroTolo

2015/10/30

#
This is in the player class, because that's where I declared scoreV. private int timer =0; I'm asking how to change the checking of the scoreV value. It's a counter for the score in the game. Every 500 points earned the laser will randomly spawn last 4 seconds then disapear.. Laser code: If(canSee(player.class)) eat(player.class); I'm asking for a lot, but your work is highly appreciated. Sorry for the lack of code provided, still new to this. I'm just trying to make this laser spawn after 500 points are scored. Then it will Disapear after 4 seconds. It will spawn randomly in the world, making a chance to kill the player and end the game
danpost danpost

2015/10/30

#
Okay. I guess it is alright to place the code for spawning Laser objects in the class of the player (since the score is located there and you probably do not need Laser objects to be spawned when there is no player object in the world). However, the 'timer' and the removing of the Laser objects should be in the Laser class. I see a complication, however, in just placing the Laser object into the world even with the conditions you have presented. If no score is accumulated within the four seconds after it has reached a multiple of 500 and a Laser object is spawned, what will prevent another one from spawning after the first one is removed. Maybe a field to indicate the last score at which a Laser object was created would do the trick.
BroTolo BroTolo

2015/10/31

#
if(getWorld().getObjects(Laser.class).size() != 1) Will only allow one to be spawned, but yes after it is deleted, another one would need to be spawned after another 500 points are scored. (i know this is wrong, but i thought it looked like this) if(scoreV == 500) + 500 So every 500 points it spawns a laser. But i know that code is wrong. But i will add the code to the laser itself, i should be able to get the timer working but im not sure how to make it spawn every 500 points. I'm still newish to greenfoot, even coding in general
danpost danpost

2015/10/31

#
BroTolo wrote...
if(getWorld().getObjects(Laser.class).size() != 1) Will only allow one to be spawned, but yes after it is deleted, another one would need to be spawned after another 500 points are scored.
I was just saying that if no score is accumulated within the 4 seconds that one is in the world, another one would be immediately spawned. Hence, the need for another field besides 'scoreV' to help control when one is spawned:
1
2
3
4
5
6
7
8
9
// instance fields
private int scoreV;
private int laserCount;
// within code
if (scoreV/500 > laserCount)
{
    getWorld().addObject(new Laser(), Greenfoot.getRandomNumber(1050)+50, 215);
    laserCount++;
}
BroTolo BroTolo

2015/11/1

#
I'm very confused, This is what i have now...
1
2
3
4
5
6
int scoreV = ((ScoreCounter)getWorld().getObjects(ScoreCounter.class).get(0)).getValue();
if (scoreV/500 > laserCount)
        {
            getWorld().addObject(new Laser(), Greenfoot.getRandomNumber(1050)+50, 215);
            laserCount++;
        }
1
private int laserCount;
This is all in my player, So in my mind this makes me think, scoreV divided by 500 is larger than laser count, so 1. add new laser and then it would add to laserCount. What exactly does it do. How can i get the timer to remove the object? I'd put that in the laser? then for it to spawn every 500 score. im not sure how to write that.
danpost danpost

2015/11/1

#
BroTolo wrote...
So in my mind this makes me think, scoreV divided by 500 is larger than laser count, so 1. add new laser and then it would add to laserCount. What exactly does it do.
When the quotient is greater than the value of laserCount, and new laser is added to the world and the value of laserCount is incremented to catch up to the value of the quotient. The expression 'scoreV/500' is an int value since 'scoreV' is declared as an int. Therefore, its value would be one until a value of '100' is reached for 'scoreV'. Then, again, the expression will evaluate to one more than the value of laserCount, '2', where another laser will be added and the laserCount field incremented to match the new quotient.
How can i get the timer to remove the object? I'd put that in the laser?
Yes, put the timer in the Laser class and use its act method to have it remove itself when the time has expired.
BroTolo BroTolo

2015/11/2

#
What would the code be to make a timer for the laser? I think its this private int timer; private void deSpawn() { timer --; if(timer == 0) { getWorld().removeObject(this); } }
danpost danpost

2015/11/2

#
BroTolo wrote...
What would the code be to make a timer for the laser? I think its this < Code Omitted >
That is close -- but if the timer is initially zero and decremented each time the 'deSpawn' method is called, it will never return back to zero. That, and you will need to call this method from the 'act' method of the class.
thor747 thor747

2015/11/2

#
Hi i need some help with a multiplier game. Can someone help me?
BroTolo BroTolo

2015/11/2

#
private int timer = 4; public void act() { timer--; If(timer == 0) { //removing of object timer++; }
danpost danpost

2015/11/2

#
@BroTolo, the initial timer value is too small -- the actor will barely blink in the world. An initial timer value of something between 200 an 240 would be more appropriate. Also, the last line, 'timer++;' is not needed -- once the actor is removed from the world, the 'act' method for that actor will no longer be executed at all.
BroTolo BroTolo

2015/11/2

#
Alright thanks for the help again!
You need to login to post a reply.