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

2014/4/27

Stop one object for specified time

Draggondroppings Draggondroppings

2014/4/27

#
How do I cause one of my moving objects to stop for a specified time without delaying the entire game? I can make a fly land on a cactus but if I use delay it delays the entire game and I only want to delay the fly for 5 seconds or so and then have him start to move again. Thanks for any help!! Obviously, I am new to this.
danpost danpost

2014/4/27

#
Add an instance field to be a timer for the holding of position.
1
private int holdTimer = 0;
Then, qualify when to move:
1
if (holdTImer == 0) { /** movement code */ } else holdTimer--;
When a cactus is to be stopped at, just set the timer to some value -- maybe somewhere around 250 or 300 (a normal scenario speed, with the speed slider handle around the middle of the slider, is about 50 to 60 cycles per second).
Draggondroppings Draggondroppings

2014/4/27

#
Thanks for the info! Can't try it right at the moment At the ER with a relative. Will this stop all activity within the world or just with the one object?
danpost danpost

2014/4/27

#
Draggondroppings wrote...
Will this stop all activity within the world or just with the one object?
It will not hinder the actions of other actors or the world.
Draggondroppings Draggondroppings

2014/4/27

#
Thanks!!
Draggondroppings Draggondroppings

2014/4/27

#
OK, back home and I know I do not understand how to apply what you have written so below is what I have entered for my Fly under the animal class. I am trying to get the fly to stay for at least 5 seconds on my cactus which remains still for the entire game and then he turns 180 degrees and flies away. but what I have written just makes him fly right through the cactus.How do I get him to stop on the cactus for a few seconds? public class Fly extends Animal { /** * Act - do whatever the Fly wants to do without the ability to control it. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int pause = 5000; public void act() { if ( atWorldEdge() ) { turn(45); //when at any edge of the world turns the fly 45 degrees before moving on } if ( Greenfoot.getRandomNumber(100) < 60 ) { turn(Greenfoot.getRandomNumber(90)-45); } move(); if ( canSee(Cactus.class) ) { if(pause>0) pause--; if(pause == 0) turn(180); } if ( canSee(Spider.class) ) { Greenfoot.playSound("uh-oh3.wav"); } } }
Draggondroppings Draggondroppings

2014/4/27

#
Oops! that was trying another way, but still any help is appreciated.
Draggondroppings Draggondroppings

2014/4/27

#
The code below is how I would enter your information at my level of knowledge and, again obviously it is wrong. Can you offer me any help? public class Fly extends Animal { /** * Act - do whatever the Fly wants to do without the ability to control it. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int holdTimer = 250; public void act() { if ( atWorldEdge() ) { turn(45); //when at any edge of the world turns the fly 45 degrees before moving on } if ( Greenfoot.getRandomNumber(100) < 60 ) { turn(Greenfoot.getRandomNumber(90)-45); } move(); if ( canSee(Cactus.class) ) { if (holdTimer == 0) turn(180); else holdTimer--; } if ( canSee(Spider.class) ) { Greenfoot.playSound("uh-oh3.wav"); } } }
danpost danpost

2014/4/28

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public class Fly extends Animal
{
    private int holdTimer;
    Actor atCactus;
 
    public void act()
    {
        if (holdTimer == 0)
        {
            move();
            if  (atWorldEdge()) turn(45);
            if (Greenfoot.getRandomNumber(100)<60) turn(Greenfoot.getRandomNumber(90)-45);
            if (atCactus != null && !intersects(atCactus)) atCactus = null;
            if (canSee(Cactus.class && atCactus == null)
            {
                holdTimer = 250;
                atCactus = getIntersectingObject(Cactus.class);
            }
        }
        else
        {
            if ((--holdTimer) == 0) turn(180);
            if (Greenfoot.getRandomNumber(100) < 6) turn(Greenfoot.getRandomNumber(11)-5);
        }
    }
}
I added another field to hold the cactus object landed on. It is needed to ensure the fly leaves it totally before trying to land on it again (otherwise, it could repeatedly land on the same one without having a chance to leave it). I also added a small random turning while on a cactus (just to show some life).
Draggondroppings Draggondroppings

2014/4/28

#
Woah!! I really appreciate your work! I am a student and cannot use the great improvements that you have introduced here. I will use the code that causes the Fly to stay on the Cactus though and throw back in my silly sounds. Thanks again, Paul
You need to login to post a reply.