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

2018/12/20

reloading ammo, help

1
2
shah5826 shah5826

2018/12/20

#
Hello, I have created a game where the main object (in this case, a ship) is shooting at enemy objects. The ship has a limited amount of bullets, and once the bullets finish, it must acquire the Ammo object that spawns once the ammo = 0. I'm having trouble making the object spawn once randomly in the world, as it spawns multiple times. I also need to know how to reload the bullets back to the original amount.
void myMethod()
{
      if (!allowedToFire && Greenfoot.isKeyDown("space"))
        {
            if (munition() == true)
            {
                allowedToFire = true;
                Greenfoot.playSound("shootsound.wav");
                getWorld().addObject(new Bullet(direction), getX(), getY());
                --ammo;
            }
           
        }
        if (allowedToFire && !Greenfoot.isKeyDown("space"))
        {
            allowedToFire = false;
        }
    }

    public boolean munition(){
        if(ammo>0){
            return true;
        }
        else{
            return false;
            
        }
    }
    
    private void spawnAmmo()
    {
        if (ammo == 0)
        {
            
            ((OceanWorld)getWorld()).spawnAmmo();
        }
    }
}
shah5826 shah5826

2018/12/20

#
Limiting the bullets works perfectly, however, reloading and spawning the ammo is what I am having trouble with.
danpost danpost

2018/12/20

#
shah5826 wrote...
Limiting the bullets works perfectly, however, reloading and spawning the ammo is what I am having trouble with.
Change line 32 to this:
if (ammo == 0 && getWorld().getObjects(Ammo.class).isEmpty())
danpost danpost

2018/12/20

#
To reload, use:
if (isTouching(Ammo.class))
{
    removeTouching(Ammo.class);
    ammo = 10; // whatever the max value is
}
shah5826 shah5826

2018/12/20

#
Thank you very much for replying! The spawning of the Ammo class has helped and it works perfectly. However, in my game when my character collides with the enemy, it loses a life and respawns somewhere else in the game, after these corrections, an error appears whenever the main character collides with the enemy. The error says "Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed."
shah5826 shah5826

2018/12/20

#
This is the code for when it collides with the enemy
 private void checkCollision()
    {
        Shark s = (Shark) getOneIntersectingObject(Shark.class);
        if (s != null)
        {
            getWorld().removeObject(s);  
            disappear = true;    
            getWorld().addObject(new Explosion(), getX(), getY());
            ((OceanWorld)getWorld()).loseLives();            
            ((OceanWorld)getWorld()).spawnShip();

        }
    }

    private void disappearShip()
    {
        if (disappear == true)
            getWorld().removeObject(this);
    }
danpost danpost

2018/12/20

#
shah5826 wrote...
This is the code for when it collides with the enemy << Code Omitted >>
And the act method, please?
shah5826 shah5826

2018/12/20

#
This is the act method.
 public void act() 
    {
        checkKeys();
        checkCollision();
        disappearShip();
        collectAmmo();
        spawnAmmo();
    }    
shah5826 shah5826

2018/12/20

#
Hello! I have just realized my problem, I put the "dissapearShip" at the end of the act method, and now it works perfectly. Thank you very much for your help!
shah5826 shah5826

2018/12/21

#
Hi! Sorry bothering you again. I concluded that my game has worked perfectly before, however, there seems to be another problem. Whenever my character loses all bullets and then collides with the enemy before reloading the ammunition, my character suddenly has all the bullets again without even acquiring the ammunition. This is all the code for my main character:
public class Ship extends Actor
{
    private boolean allowedToFire;
    private int direction;
    private boolean disappear;
    public int ammo;
    public Ship()
    {
        allowedToFire = true;
        getImage().scale(275, 200);
        direction = 0;
        
        ammo = 7;

    }

   
    public void act() 
    {
        checkKeys();

        collectAmmo();
        spawnAmmo();
        checkCollision();
        disappearShip();

    }    

    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY()-3);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+3);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+3, getY());
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-3, getY());
        }
       

       
        if (!allowedToFire && Greenfoot.isKeyDown("space"))
        {
            if (munition() == true)
            {
                allowedToFire = true;
                Greenfoot.playSound("shootsound.wav");
                getWorld().addObject(new Bullet(direction), getX(), getY());
                --ammo;
            }
           

        }
        if (allowedToFire && !Greenfoot.isKeyDown("space"))
        {
            allowedToFire = false;
        }
    }

    public boolean munition(){
        if(ammo>0){
            return true;
        }
        else{
            return false;

        }
    }

    private void spawnAmmo()
    {
        if (ammo == 0 && getWorld().getObjects(Ammo.class).isEmpty())
        {
            ((OceanWorld)getWorld()).spawnAmmo();
        }
    }

    public void collectAmmo()
    {
        
        if (isTouching(Ammo.class))
        {
            removeTouching(Ammo.class);
            ammo = 7; 
        }
    }

    private void checkCollision()
    {
        Shark s = (Shark) getOneIntersectingObject(Shark.class);
        if (s != null)
        {
            getWorld().removeObject(s);  
            disappear = true;    
            getWorld().addObject(new Explosion(), getX(), getY());
            ((OceanWorld)getWorld()).loseLives();            
            ((OceanWorld)getWorld()).spawnShip();

        }
    }

    private void disappearShip()
    {
        if (disappear == true)
        {
            getWorld().removeObject(this);

           
        }

    }
}

danpost danpost

2018/12/21

#
shah5826 wrote...
Whenever my character loses all bullets and then collides with the enemy before reloading the ammunition, my character suddenly has all the bullets again without even acquiring the ammunition.
That is because the re-spawned ship is a NEW ship (not the same Ship object that has disappeared). Please show all World subclass codes (this is where the problem can be dealt with as that is where the NEW ship is being added into the world from).
shah5826 shah5826

2018/12/21

#
public class OceanWorld extends World
{

    private Scoreboard score;
    
    public OceanWorld()
    {    
        
        super(1000, 700, 1); 
        setPaintOrder(Ship.class, Bullet.class, Smoke.class);
        addObject(new Ship(), getWidth()-900, getHeight()/2);
        addObject(score = new Scoreboard(), 100, 50);

    }

    public void act()
    {
        
        placeShark();

    }
    private void placeShark()
    {
        if (Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new Shark(0), getWidth()-5, Greenfoot.getRandomNumber(getHeight()));
           
        }
    }
    public void addToScore()
    {
        score.addToScore();
    }

    public void minusScore()
    {
        score.minusScore();
    }
    
    

    public void loseLives()
    {
        score.loseLives();
    }
    public void spawnShip()
    {
        addObject(new Ship(), getWidth()-900, Greenfoot.getRandomNumber(getHeight()));
    }
    public void spawnAmmo()
    {
        addObject(new Ammo(), Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
    }
    

}
danpost danpost

2018/12/21

#
shah5826 wrote...
<< Code Omitted >>
Add field:
private Ship ship = new Ship();
Change 'new Ship()' in lines 11 and 48 to 'ship'. Now the same Ship object will be re-spawned.
shah5826 shah5826

2018/12/21

#
Now, whenever the ship collides with the enemy, no ship is respawned, it is only removed from the world.
danpost danpost

2018/12/21

#
shah5826 wrote...
Now, whenever the ship collides with the enemy, no ship is respawned, it is only removed from the world.
Right. Now, change the checkCollision method to this:
private void checkCollision()
{
    if (isTouching(Shark.class)
    {
        removeTouching(Shark.class);
        OceanWorld ocean = (OceanWorld)getWorld();
        ocean.addObject(new Explosion(), getX(), getY());
        ocean.removeObject(this);
        ocean.loseLives();
        ocean.spawnShip();
    }
}
There are more replies on the next page.
1
2