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

2016/12/15

Player Won't Die

CakeMonster CakeMonster

2016/12/15

#
For some reason during my asteroid game when the asteriod hits the Player it is supposed to remove the player and stop the Greenfoot game using if (isTouching(Player.class) and removeTouching(Player.class); but when the Asteriod hits the player nothing happens it just phases through the player.
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     private int timer;
    public void act() 
    {
        if (Greenfoot.isKeyDown("right"))
        {
            turn(5);
    }   
    if (Greenfoot.isKeyDown("left"))
        {
            turn(-5);
    
    }
     if (timer > 0) timer--; 
    int x = getRotation()-360;
    if (timer == 0 && Greenfoot.isKeyDown("space"))
    {
        timer = 50; 
         getWorld().addObject(new Lazer(getRotation()),getX(),getY());
    }
 }
}
CakeMonster CakeMonster

2016/12/15

#
The asteroids code
public class BigAsteroid extends Actor
{
    /**
     * Act - do whatever the BigAsteroid wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    /** Size of this asteroid */
 

    /** When the stability reaches 0 the asteroid will explode */
  
   
   
    
    
    
    public void act()
    {         
       move(1);
       World g = getWorld();
       randomturn();
       teleAtEdge();
}
    public void killPlayer()
    {
        if(isTouching(Lazer.class))
        {
            removeTouching(Lazer.class);
    }
}
    int direction = Greenfoot.getRandomNumber(360);
    int tumbleRate = 0;
    public void randomturn()
    {
        
        // with instance field
      
 
    // sample act method might be

    {
    int rotation = getRotation(); // save tumble state
    setRotation(direction); // set rotation to direction of movement
    move(5); // move
    setRotation(rotation+tumbleRate); // return tumble state rotation plus change in tumble per act
    // wrap code window here
   }
  }
    public void teleAtEdge()
    {
        if (isAtEdge())
        {
            int a = 20;
            int w = getWorld().getWidth();
            int h = getWorld().getHeight();
            int x = getX();
            int y = getY();
 
            if (x < a || x > w-a-1) {
            setLocation(w-x-1, y);
        }
            if (y < a || y > h-a-1) {
            setLocation(x, w-y-1);
        }
      }
}
}
Nosson1459 Nosson1459

2016/12/16

#
I see a mthod called killPlayer but in it is:
public void killPlayer()
    {
        if(isTouching(Lazer.class))
        {
            removeTouching(Lazer.class);
    }
nothing to check if (isTouching(Player.class) or removeTouching(Player.class). P. S. It would be easier to read your code if it had proper indentation, the fastest way to make proper indentation is by pressing <Ctrl> + 'i' in the class editor.
You need to login to post a reply.