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

2014/4/24

Bullet is deleting itself before an enemy can detect it

mcGreen mcGreen

2014/4/24

#
The bullet is being removed when it collides with an enemy and the enemy has a method to detect collision with bullets, but it does not trigger because its being removed ... first? Dont exactly know how to describe. Here's the bullet code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ShotL extends Fire
{
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
     setLocation(getX() + speed, getY());  
     checkHit();
          
     if(this.checkHit() || this.atWorldEdge())
     {
         getWorld().removeObject(this);
     }
   }

   private int speed = -10;  
   public boolean atWorldEdge()
   {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
   }
   
   private boolean checkHit()
   { 
       if(getOneIntersectingObject(Enemy.class)!= null || getOneIntersectingObject(Platform.class) != null)
       {
          return true;
        }
        else
        {
            return false;
        }
    }
}    
And here the Terrorist code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ShotL extends Fire
{
    /**
     * Act - do whatever the Shot wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   public void act() 
   {
     setLocation(getX() + speed, getY());  
     checkHit();
          
     if(this.checkHit() || this.atWorldEdge())
     {
         getWorld().removeObject(this);
     }
   }

   private int speed = -10;  
   public boolean atWorldEdge()
   {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
   }
   
   private boolean checkHit()
   { 
       if(getOneIntersectingObject(Enemy.class)!= null || getOneIntersectingObject(Platform.class) != null)
       {
          return true;
        }
        else
        {
            return false;
        }
    }
}    
And how do I give the bullet a damage value?
danpost danpost

2014/4/24

#
Only check for the collision between an enemy and the bullet in one place (not in both).
mcGreen mcGreen

2014/4/24

#
Oops its late at night so I posted the same code two times... sorry for that :P So here's the terrorist code now (hopefully)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Terrorist extends Enemy
{
    int health = 200;
    
    public void act() 
    {
        checkFall();
        moveAround();
        takeDamage();
        checkDamage();
        die();
    } 
    
     public boolean checkDamage()
    {
        if(getOneIntersectingObject(Fire.class) != null)
        {
            return true;
        }
        else
        {
            return false;
        }   
    }
    
    public void takeDamage()
    {
        if(this.checkDamage())
        {
            health --;
            Greenfoot.playSound("Hit1.mp3");            
        }
    }
    
    public void die()
    {
        if(health <= 0)
        {
            death();
        }
    }
}
davmac davmac

2014/4/25

#
You still have the same problem as danpost pointed out - you are checking for collision both in the ShotL class and in the Terrorist class. You need to do it in one location, not both.
mcGreen mcGreen

2014/4/25

#
If I put this in, it kind of works but it deletes all other bullets too which is not what im looking for, so I must put it into the bullet class right? If so, how do I get to transfer the collision detection from the bullet to the terrorist class?
 public boolean checkDamageShotL()
    {
        if(getOneIntersectingObject(ShotL.class) != null)
        {
            getWorld().removeObjects(getWorld().getObjects(ShotL.class));
            health --;
            Greenfoot.playSound("Hit1.mp3");   
            return true;
        }
        else
        {
            return false;
        }   
    }
Edit: Figured it out myself. This finally works
 public boolean checkDamageShotR()
    {
        if(getOneIntersectingObject(ShotR.class) != null)
        {
            getWorld().removeObject(getOneIntersectingObject(ShotR.class));
            health --;
            Greenfoot.playSound("Hit1.mp3");   
            return true;
        }
        else
        {
            return false;
        }   
    }
mcGreen mcGreen

2014/4/25

#
Ive got a new problem, now I want to make a Game Over screen once all the terrorist are dead, but I just cannot figure out how to count them.
danpost danpost

2014/4/25

#
The List class has a 'size' method as well as an 'isEmpty' method. The World class has the 'getObjects' method (which returns a List object).
mcGreen mcGreen

2014/4/25

#
Thats working now, but as always there is more to come... I hope you are ready for more questions! After all the terrorists have been killed, I want the game to end. The method for that is working alright but its being executed again and again. Im trying to get it working with a boolean like you suggested in other threads, but my brain is struggling a lot with it so I could appreciate some help
danpost danpost

2014/4/25

#
Use 'if list of terrorists objects is empty AND list of gameover objects is empty'. I try to suggest using fields (whether boolean or not) only when absolutely necessary. If you can get the information required by method calls that return current conditions, that would be the way to go. For an example of what can go wrong when using a field, let us say you had the rotation of your actor in a field -- 'private int rotation = 0;'. Now, at any time, you can use the 'getRotation' method to get the current value; but, if you use the 'rotation' field and it is not kept current, you will probably end up getting some unwanted behavior out of your actor.
mcGreen mcGreen

2014/4/25

#
Next problem: Some strange Java error about the mp3 file im using for the EndScreen; Exception in thread "SoundStream:file:/F:/Project/sounds/EpicWin.mp3" java.lang.ArrayIndexOutOfBoundsException: 15 at javazoom.jl.decoder.LayerIDecoder$SubbandLayer1Stereo.read_allocation(LayerIDecoder.java:402) at javazoom.jl.decoder.LayerIDecoder.readAllocation(LayerIDecoder.java:108) at javazoom.jl.decoder.LayerIDecoder.decodeFrame(LayerIDecoder.java:72) at javazoom.jl.decoder.Decoder.decodeFrame(Decoder.java:147) at greenfoot.sound.Mp3AudioInputStream.read(Mp3AudioInputStream.java:230) at greenfoot.sound.SoundStream.run(SoundStream.java:302) at java.lang.Thread.run(Thread.java:744)
danpost danpost

2014/4/25

#
Evidently, the Sound decoder does not like something about your sound file. If you sound file has information tags, that can cause problems; but, usually the index would be something like '580', not '15'. Anyway, try to re-save the file ensuring that no information tags are applied to it. Maybe that will help. EDIT: after looking at other discussions (when doing a search on 'ArrayIndexOutOfBoundsException: 15'), I am pretty confident that ID3 (information) tags are the problem.
mcGreen mcGreen

2014/4/25

#
What do you exactly mean with re-saving it? Downloading it again? I just tried and it didnt work. Maybe you could test if you get the same error, here is the site it was downloaded from. Edit: Ok, is there a way to fix it?
danpost danpost

2014/4/25

#
I mean you need an audio editing program to load the file into and re-save it without the tags.
mcGreen mcGreen

2014/4/25

#
That did the trick! Indeed there was a tag on it. Somehow its now too large to upload it. Edit: Tried again and it suddenly works... strange. Anyway, only thing I need now are enemies who can run around from left to right.. I would like them to lets say, walk 3m in one direction, then stop, and then walk 3m in the other direction. Other than that the game is pretty much finished Another question: Why do the Sprites glitch like that?
You need to login to post a reply.