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

2012/12/19

Image change problem

Hawx_ Hawx_

2012/12/19

#
I am making a asteroid game which can be found here :(Space Dodge ). I am having a problem when trying to change the character image to a different image, I have this , for where I want to set the image after:
    public void stopped()
    {
        Actor Rocket = getOneIntersectingObject(Rocket.class);
        if (Rocket != null) 
            Greenfoot.stop();
        return;

    }
I have been trying this :
    public void stopped()
    {
        Actor Rocket = getOneIntersectingObject(Rocket.class);
        if (Rocket != null) 
setImage("game Over.jpeg")
            Greenfoot.stop();
        return;

    }
so it is meant to set the image and then stop the game , but it doesn't seem to work. help please. Hawx
actinium actinium

2012/12/19

#
What class is this method in ?. Also the return statement is not needed.
Hawx_ Hawx_

2012/12/19

#
It's in the Rocket class.
Hawx_ Hawx_

2012/12/19

#
The return statement is what stops the game and keeps it working , supplied by danpost(I think that's his name..)
Zamoht Zamoht

2012/12/19

#
I don't get what you're trying to find using "Actor Rocket = getOneIntersectingObject(Rocket.class);" if this is already the Rocket class. I would like to know when and how the stopped() void is executed and then by the information I have now I would say that you should write it like this.
public void stopped()  
    {   
      setImage("game Over.jpeg")  
      Greenfoot.stop();  
    }  
Hawx_ Hawx_

2012/12/19

#
The void stopped() is there only to catch an intersecting object and determine what to do with it , which is why it has intersecting object statement . I should really change it to this .
    public void stopped()  
    {  
        Actor Rocket = getOneIntersectingObject(Mine.class);  
        if (Rocket != null)   
setImage("game Over.jpeg")  
            Greenfoot.stop();  
        return;  
  
    }  
But this doesn't work and just stops the game on start. I just mainly need to know how to change the image of an actor when it is hit by another object. Thanks, Hawx
danpost danpost

2012/12/19

#
I do not think I ever said (wrote) that. I said the 'return' statement exits the method, not that it stops the game. Also, it is not wise to call the 'stopped' method directly; and using 'Greenfoot.stop()' within the method is redundant (and may cause unwanted behaviour). Better would be to use 'Greenfoot.stop()' instead of 'stopped()' wherever you placed it. The 'stopped' method will executed automatically. Check the 'World' class documentation on this method.
Hawx_ Hawx_

2012/12/19

#
Stopped() is just what I refactorised the method as. So there is no point using the return command?
actinium actinium

2012/12/19

#
will the Rocket.class ever intersect the rocket, should you not be intersecting the object the rocket collides with. The return statement is doing nothing because the method returns void and ends at the last curly bracket after the return statement.
Hawx_ Hawx_

2012/12/19

#
Post Script: I am new to java and this is my first game (I may not know what I'm talking about, but please correct me and help) Hawx
Hawx_ Hawx_

2012/12/19

#
So it should look something like this:?
    public void stopped()    //Just what I refactorised it as.
    {    
        Actor Rocket = getOneIntersectingObject(Mine.class);    
        if (Rocket != null)     
setImage("game Over.jpeg")    
            Greenfoot.stop();   
    
    }    
danpost danpost

2012/12/19

#
You could put the following 'stopped' method in the world class:
public void stopped()  
{  
    Actor rocket = (Actor) getObjects(Rocket.class).get(0);
    rocket.setmage("game Over.jpeg");
    rocket.setLocation(getWidth()/2, getHeight()/2);
    repaint(); // do not know if this would be needed
}
and just use in the actor class that detects the rocket
if(getOneInstersectingObject(Rocket.class)!=null)
{
    Greenfoot.stop();
    return;
}
The 'return' statement is not neccessary if there is no code following it in the method.
danpost danpost

2012/12/19

#
In the 'stopped' method, change rocket and Rocket to your main actor class (which would be the actor whose image you wanted to change to the game over image).
Hawx_ Hawx_

2012/12/19

#
Thanks a lot Danpost! And thank to everyone else for the comments. Helped a lot , Hawx
You need to login to post a reply.