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

2021/3/24

How Do I Create Delay On Projectile Shooting?

Akashi Akashi

2021/3/24

#
This is my code for the dolphin(the actor shooting the projectile) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Dolphin here. * * @author (your name) * @version (a version number or a date) */ public class Dolphin extends Actor { /** * Act - do whatever the Dolphin wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("W")) { setLocation(getX(), getY()-6); } if(Greenfoot.isKeyDown("S")) { setLocation(getX(), getY()+6); } if (Greenfoot.isKeyDown("space")) { Shoot(); } } /** * Shoot - shoots projectile straight out from dolphin's position. * the 'Act' or 'Run' button gets pressed in the environment. */ public void Shoot() { getWorld().addObject(new Bubble(), getX(), getY()); } } And this is my code for the bubble(the projectile) import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class bubble here. * * @author (your name) * @version (a version number or a date) */ public class Bubble extends Actor { /** * Act - do whatever the bubble wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX() + 10, getY()); } }
Super_Hippo Super_Hippo

2021/3/24

#
Here is an option: Create an instance variable. Set the variable to some reload time (can be another variable) when the object shoots. Decrease the variable by one every act cycle if it is greater than one. Only allow shooting when the variable is 0. One second is usually between 50 and 60 act cycles at speed 50.
You need to login to post a reply.