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

2014/1/29

What does this error mean?

Nooyi Nooyi

2014/1/29

#
Hello, suddenly, I got this error, but I have no idea what this means. Could someone help me please ? the error: java.lang.ClassCastException: Bomb cannot be cast to Bomb (Bomb is an actor) If necessary, I could also post some code (e.g. from the actor "Bomb"). thanks :)
JefersonCarneiro JefersonCarneiro

2014/1/29

#
Redundancy - is trying to turn a bomb into a bomb. Could you show some code so I can try to understand your logic?
Nooyi Nooyi

2014/1/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.management.*;

/**
 * Write a description of class Bombe here.
 * 
 */
public class Bomb extends Actor
{   
    private int timer = 8;
    private int counterSound = 1;

    /*act*/
    public void act() 
    { 
       playSoundBomb();       
       if (timer>0) {      //Timer as countdown until Bomb explodes
           timer--;
       }
       else{    
           explode();
       }
    }
    
    /* methods and functions */

    /** Bomb removed itself, it explodes*/
    public void explode()   {
        addExplosions();
        getWorld().removeObject(this); 
    }
   
    /** adding explosions (another actor) */
    public void addExplosions()  {
        int x = getX();
        int y = getY(); 
        getWorld().addObject(new Explosion(),x,y);
        getWorld().addObject(new Explosion(),x+1,y);
        getWorld().addObject(new Explosion(),x-1,y);
        getWorld().addObject(new Explosion(),x,y+1);
        getWorld().addObject(new Explosion(),x,y-1);
    }
    
      /** sound when a player places a bomb*/
    private void playSoundBomb() {
        GreenfootSound sound = new GreenfootSound("PlacingBombSound.mp3");   
        sound.setVolume(50);
        
        if(!sound.isPlaying() && counterSound != 0)  
        {  
            sound.play();
            counterSound--;
        } 
    }
}
JefersonCarneiro JefersonCarneiro

2014/1/30

#
this piece of code is correct! I was referring to the piece of code where it is cast (coercion), that caused the error. Ex: Actor k=getOneIntersectingObject(); ((Bomb)k).explode();
You need to login to post a reply.