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

2021/3/22

Projectile Spam

Akashi Akashi

2021/3/22

#
I am making a simple game where you move the actor up and down with w and s and it shoots projectiles. I got it to move up and down and shoot and stuff but how do I make it so that it only shoots 1 projectile per space press?
danpost danpost

2021/3/22

#
Akashi wrote...
how do I make it so that it only shoots 1 projectile per space press?
// add field
private boolean spaceDown; // to track state of "space" key

// in act
if (spaceDown != Greenfoot.isKeyDown("space")) // key state change
{
    spaceDown = ! spaceDown; //updating state
    if (spaceDown) shoot(); // shoot on key press
}
Akashi Akashi

2021/3/22

#
danpost wrote...
Akashi wrote...
how do I make it so that it only shoots 1 projectile per space press?
// add field
private boolean spaceDown; // to track state of "space" key

// in act
if (spaceDown != Greenfoot.isKeyDown("space")) // key state change
{
    spaceDown = ! spaceDown; //updating state
    if (spaceDown) shoot(); // shoot on key press
}
Where does the private boolean go? in the object shooting the projectile or the actual projectile? and same for the "in act" one
Akashi Akashi

2021/3/22

#
danpost wrote...
Akashi wrote...
how do I make it so that it only shoots 1 projectile per space press?
// add field
private boolean spaceDown; // to track state of "space" key

// in act
if (spaceDown != Greenfoot.isKeyDown("space")) // key state change
{
    spaceDown = ! spaceDown; //updating state
    if (spaceDown) shoot(); // shoot on key press
}
Ill just send you the code I have right now. Dolphin(the thing 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()); } } Bubble(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()); } }
danpost danpost

2021/3/22

#
Akashi wrote...
Where does the private boolean go? in the object shooting the projectile or the actual projectile? and same for the "in act" one
Both in the object shooting the projectile (your Dolphin class)
Akashi Akashi

2021/3/23

#
danpost wrote...
Akashi wrote...
Where does the private boolean go? in the object shooting the projectile or the actual projectile? and same for the "in act" one
Both in the object shooting the projectile (your Dolphin class)
Im getting alot of errors when i put it in and i dont really understand it
danpost danpost

2021/3/23

#
Akashi wrote...
Im getting alot of errors when i put it in and i dont really understand it
Show revised class codes. Use code tags (see below reply box).
Akashi Akashi

2021/3/23

#
danpost wrote...
Akashi wrote...
Im getting alot of errors when i put it in and i dont really understand it
Show revised class codes. Use code tags (see below reply box).
can you just put it inside this code so i can copy paste it into mine? 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()); } }
Akashi Akashi

2021/3/23

#
danpost wrote...
Akashi wrote...
Im getting alot of errors when i put it in and i dont really understand it
Show revised class codes. Use code tags (see below reply box).
Hello?...
Akashi Akashi

2021/3/24

#
danpost wrote...
Akashi wrote...
Im getting alot of errors when i put it in and i dont really understand it
Show revised class codes. Use code tags (see below reply box).
The bubble(projectile) code is the same but I put what you said in my Dolphin(projectile shooter) code and nothing changed it still spams the projectiles - New Dolphin Code - 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 { private boolean spaceDown; /** * 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(); } if (spaceDown != Greenfoot.isKeyDown("space")) // key state change { spaceDown = ! spaceDown; //updating state if (spaceDown) Shoot(); // shoot on key press } } /** * 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()); } }
danpost danpost

2021/3/24

#
You kept your old shooting code in.
You need to login to post a reply.