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

2012/6/27

Level 2 problem with shooting

ThinkingPainter ThinkingPainter

2012/6/27

#
I made a second level, for my game. every one of the actors works fine there. except for my shot actor which is firing.. well what else but shots.. but these shots go halfway across the screen and i get error
java.lang.ClassCastException: Space2 cannot be cast to Space
	at Shot.hitAnAsteroid(Shot.java:59)
	at Shot.act(Shot.java:37)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ClassCastException: Space2 cannot be cast to Space
	at Shot.hitAnAsteroid(Shot.java:59)
	at Shot.act(Shot.java:37)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
java.lang.ClassCastException: Space2 cannot be cast to Space
	at Shot.hitAnAsteroid(Shot.java:59)
	at Shot.act(Shot.java:37)
	at greenfoot.core.Simulation.actActor(Simulation.java:565)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:523)
	at greenfoot.core.Simulation.runContent(Simulation.java:213)
	at greenfoot.core.Simulation.run(Simulation.java:203)
Any help on fixing this would be greatly appreciated!
erdelf erdelf

2012/6/27

#
well, I think you should give me the class Shot in the method hitAnAsteroid, line 59
ThinkingPainter ThinkingPainter

2012/6/27

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

/**
 * A bullet to be shot at asteroids.
 * 
 * The Shot moves to the top of the screen and then expires. If it hits an asteroid
 * on the way, it destroys the asteroid, and then expires immediately.
 */
public class Shot  extends Actor
{
private Blip myShip;

/**
* Constructor for a Shot. You must specify the ship the shot comes from.
*/
public Shot(Blip blip)  
{  
setRotation(blip.getRotation()); 
move(0);
}  

/**
* 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() 
{
int ypos = getY();
if (ypos > 0) {
ypos = ypos - 10;
setLocation(getX(), ypos);
Actor rock = getOneIntersectingObject(RedBlip.class);
            
if (rock != null) {
// We've hit an asteroid!
                
hitAnAsteroid();
getWorld().removeObject(rock);
getWorld().removeObject(this);
                
}
}
else {
// I reached the top of the screen
getWorld().removeObject(this);
            
}
        
}
    
/**
* This method gets called (from the act method, above) when the shot hits an
* asteroid. It needs to do only one thing: increase the score counter.
* (Everything else, such as removing the asteroid which was hit, is dealt
* with in the act method).
*/
private void hitAnAsteroid()
{
Space spaceWorld = (Space) getWorld();  // get a reference to the world
    Counter counter = spaceWorld.getCounter();  // get a reference to the counter
    counter.bumpCount(10);
}
   
}

ThinkingPainter ThinkingPainter

2012/6/27

#
sorry i forgot to upload the code
erdelf erdelf

2012/6/27

#
well, in line 59 you ask after world space. let me try.r replace line 59 to:
World spaceworld = getWorld();
if(spaceworld instanceof Space)
{
     spaceworld = (Space) getWorld();
}
if(spaceworld instanceof Space2)
{
     spaceworld = (Space2) getWorld();
}

ThinkingPainter ThinkingPainter

2012/6/27

#
I removed line 59 works great only problem it screwed with my counter how do i fix this
    World spaceworld = getWorld();  
    if(spaceworld instanceof Space)  
    {  
         spaceworld = (Space) getWorld();  
    }  
    if(spaceworld instanceof Space2)  
    {  
         spaceworld = (Space2) getWorld();  
    }  
    
    Counter counter = spaceWorld.getCounter();  // get a reference to the counter
    counter.bumpCount(20);
spaceWorld.getCounter()
is the part giving the error can not find symbol - variable spaceWorld
ThinkingPainter ThinkingPainter

2012/6/27

#
tried to add line 59 back under it again with the new code you gave me makes the counter"score" work but messes up the shooting haha
You need to login to post a reply.