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

2013/10/10

Blowing up a Bomb

THELokusta THELokusta

2013/10/10

#
Hey there! Okay I've got a fiew questions and a Source Code which is not running. So first my Questions: 1.Can Greenfoot play Gifs? 2.Can i Scale images? 3.Is there any ShortCut that formats the Code automaticly like strg+shift+f in Eclipse? 4. How can i give Variable values to a other World in the same project (Hoghscore) Okay now my problem: I want that my Bomb explodes after 3 seconds.I'm writing the code which just removes the Bomb.But it deosn't work. Here the Code of the Bomb.class:
 public void act() 
    {
        //Bomb bomb = (Bomb)this.getOneObjectAtOffset(0, 0, Bomb.class);
       
       bombCounter();
    }
    
    public void destroyBomb() {
         getWorld().removeObject(this);
        }
    
  public void bombCounter() {
      int timer = 100;
      
      if (timer>0)  
{  
    timer--;  
    if(timer == 0) {
        
         destroyBomb();
         timer = 100;
    }
}   
    }
And the Code which creates the Bomb:
nt RobX = getX();
         int RobY = getY();
         int counter = 0;
         if(bombs > 0) {
             getWorld().addObject(new Bomb(),RobX,RobY);
              if(bombs > 0 && Greenfoot.getKey() == "space"){
             bombs = bombs -1;
            }else{
                bombs = 0;
                
            }
         
        }
I hope you can help me out! Greetings Marius
erdelf erdelf

2013/10/10

#
1. look at this 2. yes,
GreenfootImage img = new GreenfootImage(40,40);
img.scale(20,20);
3. yes, strg+shift+I 5. maybe u should look in the lunarlander scenario, there is an explosion code
danpost danpost

2013/10/11

#
Each cycle you are having the value of the timer set to 100 and it is then decreased by one. You do not want it reset back to 100 each cycle. Move the setting and declaring of the field outside of the method:
int timer = 100;

public void act()
{      
    timer--;  
    if (timer == 0) getWorld().removeObject(this);        
}
With this, the value is initially set to 100 and repeatedly decreased each act cycle. 4.
WorldName worldName = new WorldName();
worldName.variableName = value; // for public field
// or
worldName.setVariableName(value);  // for private field using a 'set...' method
THELokusta THELokusta

2013/10/11

#
erdelf wrote...
GreenfootImage img = new GreenfootImage(40,40);
img.scale(20,20); 
[/quote]

So I tried it out.I think it doesn't do what I want.The bomb should scale over time and reach the maximum size when the bomb explodes (so the object will be removed).
And I've got this code to set the Bomb image to the Explosion one but the explosion one stays forever, although it should be removed:
[code]if (timer == 4) 
        {   this.setImage("images/explosion.gif");
            if(timer == 0) {
            getWorld().removeObject(this); 
            
        }
        }
Any help for that?
erdelf erdelf

2013/10/11

#
oh, u are checking if the timer value is at zero, while it is on 4
if (timer == 4)   
{   
     this.setImage("images/explosion.gif");  
}
if(timer == 0) {  
     getWorld().removeObject(this);            
}   
and for the scaling, my code was just to show u the scale method for greenfootImages so change the code in the act to something like this
if (timer == 4)   
{   
     this.setImage("images/explosion.gif");  
     this.getImage().scale(10,10);
}
if(timer == 3)
{
     this.getImage().scale(20,20);
}
if(timer == 2)
{
     this.getImage().scale(30,30);
}
if(timer == 1)
{
     this.getImage().scale(40,40);
}
if(timer == 0) {  
     getWorld().removeObject(this);            
}
or with a switch it would look like this
switch(timer)   
{   
   case 4:
     this.setImage("images/explosion.gif");  
     this.getImage().scale(10,10); 
     break;
  case 3:
     this.getImage().scale(20,20);
     break;
  case 2:
     this.getImage().scale(30,30);
     break;
  case 1:
     this.getImage().scale(40,40);
     break;
  case 0: 
     getWorld().removeObject(this);            
}
danpost danpost

2013/10/11

#
Or:
int size = 50-10*timer;
if (size == 10) setImage("images/explosion.gif");
if (size < 50) getImage().scale(size, size); else getWorld().removeObject(this);
THELokusta THELokusta

2013/10/11

#
Thank you all! Works now.I've got just one last question because i didn't find something that fits in the documentary.If a Robot gets hit by the explosion it should be destroyed.It should also be checked which robot (there are 2) is destroyed.My Problem is, that i just scale the image and not the object. How could i solve this?
danpost danpost

2013/10/11

#
Please explain better as I am not sure as to what you need or want (or what unwanted behavior you are getting). Why do you need to know which one is destroyed? are they getting destroyed? what is it that you want with the image and the object as far as scaling? You should give a step-by-step as to what should be happening, explaining when each action occurs.
THELokusta THELokusta

2013/10/11

#
Okay: Now a Step by step thing what i want to happen: 1:Exploding Image Scales 2:If a Robot (a player can Controll a Robot) get's hit by the explosion the robbot should disapear. 3:I watn to know which robot was hit, because there are two. 4:I want to return(in a new World) which player has won For better understanding my Bomb Source:
public class Bomb extends Actor
{

    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    int timer = 10;
    public void act() 
    {

        timer--;    
       

     
    if (timer == 4)     
    {     
        this.setImage("images/explosion.gif");    
        this.getImage().scale(40,40);  
    }  
    if(timer == 3)  
    {  
        this.getImage().scale(80,80);  
    }  
    if(timer == 2)  
    {  
        this.getImage().scale(160,160);  
    }  
    if(timer == 1)  
    {  
        this.getImage().scale(240,240);  
    }  
    if(timer == 0) {    
       
        getWorld().removeObject(this);   
        
    }  
}
}
Marius
erdelf erdelf

2013/10/11

#
I am quite sure, that the object is scaled with the image This code will destroy the robot, but i need to know the way u made to check which robot that was.
    public class Bomb extends Actor  
    {  
      
        /** 
         * Act - do whatever the Bomb wants to do. This method is called whenever 
         * the 'Act' or 'Run' button gets pressed in the environment. 
         */  
      
        int timer = 10;  
        public void act()   
        {  
      
            timer--;      
        if(getOneIntersectingObject(Robot.class)!=null)
        {
             getWorld().remove((Actor)getOneIntersectingObject(Robot.class));
        }
        if (timer == 4)       
        {       
            this.setImage("images/explosion.gif");      
            this.getImage().scale(40,40);    
        }    
        if(timer == 3)    
        {    
            this.getImage().scale(80,80);    
        }    
        if(timer == 2)    
        {    
            this.getImage().scale(160,160);    
        }    
        if(timer == 1)    
        {    
            this.getImage().scale(240,240);    
        }    
        if(timer == 0) {      
             
            getWorld().removeObject(this);     
              
        }    
    }  
    }  
danpost danpost

2013/10/11

#
Start the Bomb class act method with this:
Actor robot = getOneIntersectingObject(Robot.class);
if (robot != null)
{
    // if you have a specific class for each robot
    if (robot instanceof /* name of one robot class */) // or
    // if robots are same class and you have public fields in the world holding references to them
    if (robot == ((/* name of world */)getWorld()).robotFieldname) // or
    // if robots are same class and you have private fields in world with 'get' methods
    if (robot == ((/* name of world */)getWorld()).getRobotFieldname())
    {
        Greenfoot.setWorld(new GameOver(1));
    }
    else // robot was other class
    {
        Greenfoot.setWorld(new GameOver(2));
    }
}
Another way to handle this, is by putting the following in the act method of your subclass of world:
if (getObjects(Robot.class).size() < 2)
{
    if (getObjects(Robot.class).size == 1)
    {
        Robot robot = (Robot)getObjects(Robot.class).get(0);
        // same check as above starting at line 4 removing '((/* name of world */)getWorld()).'
    }
    else
    {
        Greenfoot.setWorld(new GameOver(0));// no winner (both lost)
    }
}
THELokusta THELokusta

2013/10/11

#
Okay Guys, thanks but I don't know exactly what i need to do now.Maybe i should tell you that I've got the class "Roboter" and a Subclass for each Roboter: "Robby" & "Robita".These are the 2 Robots that can be played.So it would be nice if you could explain to me what i should do now. Thanks Marius
danpost danpost

2013/10/11

#
Since you have two classes, one for each robot, you can distinguish one from the other with the 'instanceof' method I gave above. If that does not help, then you need again to explain what you need to happen and when, that you are having problems with -- not just to say 'what should I do now'.
THELokusta THELokusta

2013/10/12

#
Thanks guy! Works now!
You need to login to post a reply.