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

2013/12/5

How can I put a delay on my Object?

Lautima Lautima

2013/12/5

#
I am trying to put a delay on the time you can shoot a bullet but I don't know how. Can someone please incorporate the delay into my code? Thanks a lot! import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ship here. * * @author (your name) * @version (a version number or a date) */ public class Ship extends Actor { /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("right")){ setLocation(getX()+7, getY()); } if(Greenfoot.isKeyDown("left")){ setLocation(getX()-7, getY()); } if(Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-7); } if(Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+7); } if(Greenfoot.isKeyDown("space")){ getWorld().addObject (new bullet (), getX()+0, getY()-2); } } }
shrucis1 shrucis1

2013/12/5

#
Everyone probably has a slightly different way of doing this, but I'll show you mine. I would do it like this:
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
27
28
29
30
31
32
33
34
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Ship here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ship extends Actor
{
    int cooldown = 0;
    public void act()
    {
        if(cooldown > 0) {
            cooldown -= 1;
        }
        if(Greenfoot.isKeyDown("right")){
                setLocation(getX()+7, getY());
        }
        if(Greenfoot.isKeyDown("left")){
                setLocation(getX()-7, getY());
        }
        if(Greenfoot.isKeyDown("up")){
                setLocation(getX(), getY()-7);
        }
        if(Greenfoot.isKeyDown("down")){
                setLocation(getX(), getY()+7);
        }
        if(Greenfoot.isKeyDown("space") && (cooldown == 0)) {
                getWorld().addObject (new bullet (), getX()+0, getY()-2);
                cooldown = 10; //here I used 10 as the delay
        }
    }
}
This way there is an integer called cooldown that is decreased by one every time the act method is called. The cooldown integer doesn't go below zero though, and you can only shoot when cooldown is at 0. When it shoots, I reset cooldown to however many 'steps' or 'ticks', whatever you call them, before it will shoot again.
danpost danpost

2013/12/6

#
Another way: Change this: if(Greenfoot.isKeyDown("space")) to this: if(Greenfoot.isKeyDown("space") && getObjectsInRange(500, bullet.class).isEmpty()) You can adjust the '500' to suit.
shrucis1 shrucis1

2013/12/7

#
Interesting, danpost, I had never thought to do it that way, although if you made the bullets disappear when they went offscreen, as I normally do, then you can shoot much faster if you're shooting at the edge when close to it.
danpost danpost

2013/12/8

#
shrucis1 wrote...
Interesting, danpost, I had never thought to do it that way, although if you made the bullets disappear when they went offscreen, as I normally do, then you can shoot much faster if you're shooting at the edge when close to it.
I only offered that suggestion because of its ease in use.
Lautima Lautima

2013/12/8

#
danpost Change this: if(Greenfoot.isKeyDown("space")) to this: if(Greenfoot.isKeyDown("space") && getObjectsInRange(500, bullet.class).isEmpty()) I changed this and it had a syntax error. What do I do now?
Lautima Lautima

2013/12/8

#
shrucis1, Thank you so much it works!
danpost danpost

2013/12/8

#
Lautima wrote...
I changed this and it had a syntax error. What do I do now?
You need to show the code you are using, copy/paste the error message and state which line the error occurs.
Lautima Lautima

2013/12/8

#
It is fixed thanks everyone! Here is the final code: /** * Write a description of class Ship here. * * @author (your name) * @version (a version number or a date) */ public class Ship extends Actor { /** * Act - do whatever the Ship wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("right")){ setLocation(getX()+7, getY()); } if(Greenfoot.isKeyDown("left")){ setLocation(getX()-7, getY()); } if(Greenfoot.isKeyDown("up")){ setLocation(getX(), getY()-7); } if(Greenfoot.isKeyDown("down")){ setLocation(getX(), getY()+7); } if(Greenfoot.isKeyDown("space") && getObjectsInRange(100, Bullet.class).isEmpty()) getWorld().addObject (new Bullet (), getX()+0, getY()-6); } }
You need to login to post a reply.