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

2012/3/26

question

Michionlion Michionlion

2012/3/26

#
does getIntersectingObjects(java.lang.Class cls) return null if there are no objects of that type intersecting it? becuase i have a condition where it is always returning true, not sure if that is because of something else though. here's the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.lang.Class;
/**
 * The player controled caracter.
 * 
 * @author Michionlion
 * @version 3/25/2012
 */
public class Jumper extends GameObject {
    int xSpeed;
    int ySpeed;
    boolean touchingPad;


    /**
     * Act - do whatever the Jumper wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        getKeysAndMove();
        touchingPad = TouchingPad();
    }    
    
    
    
    public void getKeysAndMove() {
        if (!touchingPad) {
            if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) xSpeed--;
            if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) xSpeed++;
            if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up") || Greenfoot.isKeyDown("space")) {
                if (touchingPad) {
                    ySpeed -= 3;
                }
            }
        
            
        setLocation(getX() + xSpeed, getY() + ySpeed);
    
        if (ySpeed < 6) ySpeed++;
    }
    }
    
    public boolean TouchingPad() {
        
        if (getIntersectingObjects(Pad.class) != null) {
            System.out.println("pads checked, returned true");
            System.out.println(getIntersectingObjects(Pad.class));
            return true;
        }
        else {
            System.out.println("pads checked, returned false");
            return false;
        }
    }
    
}

Michionlion Michionlion

2012/3/26

#
oh... i think i got it. it returns a List no matter what, but the list is empty if nothing is intersecting it. is there a way to turn an empty list into 'null' or 'false'? there is.... List.isEmpty() will return true if the list is empty.... ok, got it ;)
You need to login to post a reply.