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

2014/8/17

Specific Location

roeilthegreat roeilthegreat

2014/8/17

#
What i want to achieve is: if my bomb is at the edge of the screen, AND if my player is already dead, the lose screen will appear. I'm just not sure how to put in the specific location (x,y) for the bomb. Can someone please help? Here's the code to better understand:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bomb here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bomb extends Obstacles
{
    GreenfootSound deathsfx = new GreenfootSound("smb_mariodie.wav");
    /**
     * Act - do whatever the Bomb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
      move(-6);  
        // check hit player 
        Start player = (Start) getOneIntersectingObject(Start.class);  
        if (player != null)  
        {  
            getWorld().removeObject(player);  
            deathsfx.play();
            return; 
        }  
         
       int gapX = 1;
       Bomb bomb = (Bomb) getOneIntersectingObject(Bomb.class);
       if (getX() >= getWorld().getWidth()-gapX || getX() <= gapX)
        {  
          getWorld().removeObject(this);
        }  
        
        else  if (bomb(??) && (player == null))
        {
          Level.bgMusic.pause();
          Greenfoot.setWorld(new Loser());
        }
    }
}
davmac davmac

2014/8/17

#
You can use getX() and getY(). Something like:
if (bomb.getX() == ... && bomb.getY() == ... && player == null) {
probably though, you don't want to use exact coordinates but look for a value within a range, as you do for the player on line 30.
roeilthegreat roeilthegreat

2014/8/17

#
Thanks for the help, but when I used that code, my game freezes. No errors, just freezes. Even tried using the other code on line 30, but still got the same result. So instead of waiting until the bomb hits the edge of the screen, I altered my code so that, when the player == null it goes to the lose screen immediately.
public void act() 
    {
      move(-6);
       // check hit player 
       Start player = (Start) getOneIntersectingObject(Start.class);  
        if (player != null)  
        {  
            getWorld().removeObject(player);  
            deathsfx.play();
            Level.bgMusic.pause();
            Greenfoot.setWorld(new Loser());  
            return;
        }  
        int gapX = 1;
        if (getX() >= getWorld().getWidth()-gapX || getX() <= gapX)
            {  
                getWorld().removeObject(this);
            }
At least it got the job done. Not the way I wanted but, simplicity is best. Thanks again.
danpost danpost

2014/8/17

#
With your original code, line 29 seems out of sorts. With that line, 'bomb' will reference a Bomb object that is intersecting 'this' bomb, if there is one. I do not think line 35 was intended to check the state of that bomb, if there was one. Also, on line 35, 'player == null' will always be true. 'player' refers to an intersecting Start object as determined by line 20. Had 'player != null', lines 23 through 25 would have been executed due to the condition on line 21 and the execution of the method would cease there (due to the 'return' statement). However, you do not want the condition of an intersecting Start object; you want the condition of the world containing a Start object:
if (getWorld().getObjects(Start.class).isEmpty())
would check to see if the player was in the world. I think if you re-arrange your code starting at line 29 to the following, you might be closer to what you originally wanted:
if (getX() >= getWorld().getWidth()-gapX || getX() <= gapX)
{
  if (getWorld().getObjects(Start.class).isEmpty())
  {
    Level.bgMusic.pause();
    Greenfoot.setWorld(new Loser());
  }
  else getWorld().removeObject(this);
}
roeilthegreat roeilthegreat

2014/8/17

#
Wow. That code works exactly how I originally imagined my scene. Thanks a lot!
You need to login to post a reply.