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

2019/4/19

Help with isTouching() method

AwesomeKid AwesomeKid

2019/4/19

#
Hello, I need help with my isTouching() method in my checkIntersections() Method. In this method, I rely on isTouching() to locate the classes, but isTouching() isn't working. I printed out the value of isTouching() multiple times and the code always printed out "false". Can someone please help me with this problem? Here is my code below:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Player extends Actor
{

    private int timer = 600;
    private final int TOTALAIRTIME;
    private int verticalChange,accelerationUpwards, accelerationDownwards, airTime,ground;
    private boolean inAir,pressedUp;
    private String jumpSound;
    public Player()
    {
        verticalChange = 5;
        accelerationUpwards = -1;
        accelerationDownwards = 2;
        airTime = 0;
        inAir = false;
        pressedUp = false;
        TOTALAIRTIME = 15;
        jumpSound = "player_jump_effect3.wav";
    }
    public void act() 
    {
      checkKeyPress();
      checkIntersections();
      updateTimer();
      if (timer>0){
        timer--;
        getWorld().showText("Time left: "+timer,150,30);
        if(timer == 0) {
            getWorld().showText("GAME OVER",300,200);
            Greenfoot.stop();
        }
      } 
    }  
    public void checkKeyPress()
     {
        int dx = 0,dy = 0;
        checkGround();
       if(getY() == ground) 
       {
           if(airTime == 0)
           {
              verticalChange = 5;
              airTime = TOTALAIRTIME;  
              inAir = true;
           }
       }
       if(Greenfoot.isKeyDown("right")) 
           dx = 4;
       if(Greenfoot.isKeyDown("left"))
           dx = -4;
       if(Greenfoot.isKeyDown("up") && airTime > 0)
       {
           Greenfoot.playSound(jumpSound);
           pressedUp = true;
           verticalChange += accelerationUpwards;
           airTime--;
        }     
       if(!pressedUp && getY() < ground)
        {
           verticalChange += accelerationDownwards;
           if(ground - getY() < Math.abs(verticalChange)) verticalChange = ground - getY();
        } 
       if(getY() + verticalChange > ground) 
       {
            verticalChange = ground - getY(); 
       }
        pressedUp = false;
        setLocation(getX() + dx, getY() + verticalChange);
        Greenfoot.getKey();
    }
    public void checkGround()
    {
       Actor possiblePlatform = getOneObjectAtOffset(0,getImage().getHeight(), Platform.class);
       if (possiblePlatform == null) {
         possiblePlatform = getOneObjectAtOffset(0,getImage().getHeight(), MiniPlatform.class);
       }
       if(possiblePlatform != null) {
           ground = possiblePlatform.getY() - possiblePlatform.getImage().getHeight();
        }
       else {
           ground = getWorld().getHeight() - getImage().getHeight()/2 - 30;
        }    
    }
    public boolean platformIsAbove()
    {
        Actor possiblePlatform = getOneObjectAtOffset(0,getImage().getHeight(), Platform.class);
        if (possiblePlatform == null) {
          possiblePlatform = getOneObjectAtOffset(0,getImage().getHeight(), MiniPlatform.class);
        }
        if(isTouching(Platform.class) && getY() - getImage().getHeight() >= ground) 
        {
            System.out.println(getY());
            return true;
        }
        if(isTouching(Platform.class) && getY() - getImage().getHeight() >= ground) 
        {
            System.out.println(getY());
        }
        if(isTouching(MiniPlatform.class) && getY() - getImage().getHeight() >= ground) 
        {
            System.out.println(getY());
        }
        if(isTouching(MiniPlatform.class) && getY() - getImage().getHeight() >= ground) 
        {
            System.out.println(getY());
            return true;
        }
        return false;
    }
    public void checkIntersections()
    {
        System.out.println(isTouching(Lava.class));
        if(isTouching(Lava.class)){
            
            setLocation(20,237);
            Greenfoot.playSound("LavaDeath.wav");
        }
        if(isTouching(Points.class)){
            removeTouching(Points.class);
            timer += 100;
        }
        if(isTouching(Portal.class))Greenfoot.setWorld(new Level2());
        if(isTouching(Portal2.class))Greenfoot.setWorld(new Level3());
        if(isTouching(Bee.class)){
           setLocation(20,200);
        }
        if(isTouching(Portal3.class)){
           getWorld().showText ("YOU WIN!!!", 300, 150);
           getWorld().showText ("Play again?", 300, 200);
           Greenfoot.stop();
        }

    }
    public void updateTimer()
    {
        timer --;
    }
}

AwesomeKid AwesomeKid

2019/4/19

#
I printed out the value of isTouching() on line 115
N0way N0way

2019/4/20

#
change line 116 if(getOneIntersectingObject(Lava.class) != null) {
You need to login to post a reply.