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

2020/5/20

Is there a way to get something to happen if there are no enemies on the world?

1
2
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
I'm working on a project that requires an event to happen after all the enemies are killed. Can anyone help me please?
Weeb3.exe Weeb3.exe

2020/5/20

#
Show your current code
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
Weeb3.exe wrote...
Show your current code
That's the part I tried to do. public void Killed_Enemies() { if (Killed_Enemies <= 3) { Greenfoot.playSound("yipee.wav"); } }
Weeb3.exe Weeb3.exe

2020/5/20

#
MYSTICPHOENIXXX wrote...
Weeb3.exe wrote...
Show your current code
That's the part I tried to do. public void Killed_Enemies() { if (Killed_Enemies <= 3) { Greenfoot.playSound("yipee.wav"); } }
first off can you kill more than 3 enemies? and if you cant change your code to this
 public void Killed_Enemies()
    {
       Greenfoot.playSound("yipee.wav"); 
      if (Killed_Enemies == 3) {
            
        }
    }
is that Killed_Enemies a void type ?
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
Weeb3.exe wrote...
MYSTICPHOENIXXX wrote...
Weeb3.exe wrote...
Show your current code
That's the part I tried to do. public void Killed_Enemies() { if (Killed_Enemies <= 3) { Greenfoot.playSound("yipee.wav"); } }
first off can you kill more than 3 enemies? and if you cant change your code to this
 public void Killed_Enemies()
    {
       Greenfoot.playSound("yipee.wav"); 
      if (Killed_Enemies == 3) {
            
        }
    }
is that Killed_Enemies a void type ?
yes
Weeb3.exe Weeb3.exe

2020/5/20

#
MYSTICPHOENIXXX wrote...
Weeb3.exe wrote...
MYSTICPHOENIXXX wrote...
Weeb3.exe wrote...
Show your current code
That's the part I tried to do. public void Killed_Enemies() { if (Killed_Enemies <= 3) { Greenfoot.playSound("yipee.wav"); } }
first off can you kill more than 3 enemies? and if you cant change your code to this
 public void Killed_Enemies()
    {
       Greenfoot.playSound("yipee.wav"); 
      if (Killed_Enemies == 3) {
            
        }
    }
is that Killed_Enemies a void type ?
yes
show your whole code please
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Angels here. * * @author (your name) * @version (a version number or a date) */ public class Angels extends Skythrust { private int lives = 2; private int Enemies = 3; /** * Act - do whatever the Angels wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { handleMovement(); if(Greenfoot.isKeyDown("space")){ getWorld().addObject (new Bullet_ (), getX()-48, getY()+2); } detect_Bullet_Collision(); Killed_Enemies(); } public void handleMovement() { if (Greenfoot.isKeyDown("right")) { setLocation(getX()+4,getY()); } if (Greenfoot.isKeyDown("left")) { setLocation(getX()-4,getY()); } if (Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+4); } if (Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-4); } } public void removeLife() { lives--; testEndGame(); } public void detect_Bullet_Collision() { if (isTouching(Bullet.class)) { removeLife(); } } public void testEndGame() { if (lives <= 0) { removeTouching(Angels.class); } } public void Killed_Enemies() { Greenfoot.playSound("yipee.wav"); if (Killed_Enemies == 3){ } } }
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
It says it can't find the variable of Killed_Enemies
danpost danpost

2020/5/20

#
You want something to happen when no enemies are in the world -- or, equivalently, when the list of enemies in the world is of size zero or is empty. So, get a list of all enemies in the world and check either way (if is empty or if size is zero).
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
danpost wrote...
You want something to happen when no enemies are in the world -- equivalently, when the list of enemies in the world is of size zero or is empty. So, get a list of all enemies in the world and check either way (if is empty or if size is zero).
Can you please give an example visually?
Weeb3.exe Weeb3.exe

2020/5/20

#
ok so I've noticed a few problems with your code first off
 public void Killed_Enemies()
    {
            Greenfoot.playSound("yipee.wav");
           if (Killed_Enemies == 3){
        }
you would need a separate method for Killed_Enimeies because you would be calling the exact method inside it you also don't have a code showing that you have killed enemies show the enemies code
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
Weeb3.exe wrote...
ok so I've noticed a few problems with your code first off
 public void Killed_Enemies()
    {
            Greenfoot.playSound("yipee.wav");
           if (Killed_Enemies == 3){
        }
you would need a separate method for Killed_Enimeies because you would be calling the exact method inside it you also don't have a code showing that you have killed enemies show the enemies code
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Enemies here. * * @author (your name) * @version (a version number or a date) */ public class Enemies extends Actor { private int timer; public void act() { if (timer > 0) timer--; if (timer == 0) { timer = 60; getWorld().addObject (new Bullet (), getX()-22, getY()+2); } if (Greenfoot.getRandomNumber(2) < 1) { setLocation(getX(),getY()-1); } else if (Greenfoot.getRandomNumber(2) > 1) { setLocation(getX(),getY()+1); } if (atBottomEdge()) { setLocation(getX(), 21); } else if (atTopEdge()) { setLocation(getX(), getWorld().getHeight() - 21); } } public boolean atBottomEdge() { if(getY() > getWorld().getHeight() - 20) return true; else return false; } public boolean atTopEdge() { if(getY() < 20) return true; else return false; } }
danpost danpost

2020/5/20

#
MYSTICPHOENIXXX wrote...
Can you please give an example visually?
Well, I think you are coding this in the wrong place to begin with. This action sounds like a major game procedure which should be a World subclass behavior. Remove all concerning killed enemies from Angels class and then in your World subclass, use an act method there:
public void act()
{
    if (getObjects(Enemies.class).size() == 0) { }
    // or
    if (getObjects(Enemies.class).isEmpty()) {}
}
MYSTICPHOENIXXX MYSTICPHOENIXXX

2020/5/20

#
danpost wrote...
MYSTICPHOENIXXX wrote...
Can you please give an example visually?
Well, I think you are coding this in the wrong place to begin with. This action sounds like a major game procedure which should be a World subclass behavior. Remove all concerning killed enemies from Angels class and then in your World subclass, use an act method there:
public void act()
{
    if (getObjects(Enemies.class).size() == 0) { }
    // or
    if (getObjects(Enemies.class).isEmpty()) {}
}
I just put that in but for some reason it's silent.
danpost danpost

2020/5/20

#
MYSTICPHOENIXXX wrote...
t's silent.
What do you mean -- 'it's silent"?
There are more replies on the next page.
1
2