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

2022/4/17

Incompatible types: greenfoot.actor cannot be converted into boolean.

200263 200263

2022/4/17

#
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Scroller
{
    int gravity;
    
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        checkKeyPress(); //This should be included in the act() method so that the game is always checking for user input.
        gravity--;
        setLocation(getX(), getY() - gravity);
        checkForJump();
        
        if(getOneIntersectingObject(Enemy.class)!=null){
             
              displayGameOver();
              Greenfoot.stop();
           }
         
        
    }
    
    /**
     * This is used for responding to keyboard input by the user.
     * This will only happen when the player is far enough to the left or right of the screen for scrolling to be needed.
     * Code to make the character jump is below. 
     */
    public void checkKeyPress()
    {
        MyWorld World = (MyWorld) getWorld();
        Scroller scroller = (Scroller) World.getScroller();
        if(Greenfoot.isKeyDown("left") && scroller.shouldScroll == false)
        {
            move(-3);
        }
        else if(Greenfoot.isKeyDown("right") && scroller.shouldScroll == false)
        {
            move(3);
        }
                
    }
     private void checkForJump(){
        MyWorld World = (MyWorld) getWorld();
        Scroller scroller = (Scroller) World.getScroller();
        if(Greenfoot.isKeyDown("space") && scroller.shouldScroll == false){
            gravity = 20; // this will make the character jump
            
        }
        else if(getOneIntersectingObject(Ground.class)){
            gravity = 0;
            
        }

        
    
    }
    
    

    public void displayGameOver(){
                GameOver gameOver = new GameOver(); //Creating new variable called gameover, add it to the world.  
                getWorld().addObject(gameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); //Put it in the center
                Greenfoot.stop();                
    
        
        
    }
    
    
    /**
     * This method will make the player move right a certain amount.
     * Entering a negative amount will cause a movement towards the left.
     * @param The speed of the movement
     */
    public void move(int amount)
    {
        int x = getX() + amount;
        int y = getY();
        
        setLocation(x, y);
    }
}

        
    
    }
The error here is at "else if(getOneIntersectingObject(Ground.class)){ gravity = 0;". What I'm trying to do here is to make the gravity 0 when the character is intersecting the object"ground".
AbdulrazaqAS AbdulrazaqAS

2022/4/17

#
It should be
else if(getOneIntersectingObject(Ground.class) != null){
            gravity = 0;
         }
As in line 24
You need to login to post a reply.