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

2024/5/5

my character stopped jumping and idk why

Bashni Bashni

2024/5/5

#
public void act()
    {
        Keys();
        
        fall();
        arrow();
        checkFall(); 
        onGround();
        
        //onGround1();
        //onGround2();
        collisionOffset();
        //collisionOffset2();
        //pickupstick();
        checkCeiling();
        
    }
    
    
    
     private void fall()
    {
       setLocation( getX(), getY()+ vSpeed); 
        if( vSpeed<= 10)
      {
        vSpeed = vSpeed + gravity;
      } 
      isJumping = true; 
    }
    
    
    private void checkFall()
    {
        if( onGround() == true )
        {
            vSpeed = 0; 
        }
        else
        {
            fall(); 
        }
    }
    
   private void jump()
    {
       vSpeed = jumpHeight;
        isJumping = true; 
        fall();  
    }
    
    private void Keys()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+9, getY()); 
          animateR(); 
        }
        
        
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-9, getY()); 
            animateL(); 
        }
        
        if(Greenfoot.isKeyDown("space") && isJumping ==false)
        {
         jump(); 

        }    
    }
    
    public void animateR()
    {
       if (getImage() == right1)
       {
           setImage(right2); 
       }
       else if(getImage() == right2)
       {
           setImage(right3); 
       }
       else
       {
          setImage(right1);  
       }
    }
    
     public void animateL()
    {
        if(getImage() == left1)  
        {
            setImage(left2); 
        }
        else if( getImage() == left2) 
        {
            setImage(left3); 
        }
        else
        {
            setImage(left1); 
        }
    }
    
    
     private void moveToGround(Actor ground) 
    {
       int groundHeight = ground.getImage().getHeight(); 
       int newY = ground.getY() - ( groundHeight + getImage().getHeight() ) /2; 
                                    
       setLocation(getX (), newY );    
       isJumping = false;
       
    }
    
    
    private void jumpy()
    {
        if (getWorld() instanceof Instructions)
        {
            onGround();
        }
    }
    
    private boolean onGround()
    {
        int spriteHeight = getImage().getHeight(); 
        int lookForGround = spriteHeight/2; 
        Actor ground = getOneObjectAtOffset( 0 ,lookForGround, Platform.class ); 
        
        if( ground == null )
        { 
            isJumping = true; 
            return false; 
        }
        else
        {
            moveToGround(ground); 
           return true;  
           
        }
        
    }
    
     
    private boolean onGround1()
    {
        int spriteHeight = getImage().getHeight(); 
        int lookForGround = spriteHeight/2; 
        
        Actor ground = getOneObjectAtOffset( 0 ,lookForGround, Floor1.class ); 
        
        if( ground == null )
        { 
            isJumping = true; 
            return false; 
        }
        else
        {
            moveToGround(ground); 
           return true;  
           
        }
        
    }
    
        private boolean onGround2()
    {
        int spriteHeight = getImage().getHeight(); 
        int lookForGround = spriteHeight/2; 
        
        Actor ground = getOneObjectAtOffset( 0 ,lookForGround, Platform2.class ); 
        
        if( ground == null )
        { 
            isJumping = true; 
            return false; 
        }
        else
        {
            moveToGround(ground); 
           return true;  
           
        }
        
    }
    
    private void bopHead( Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight(); 
        int newY = ceiling. getY() + (ceilingHeight + getImage().getHeight())/2; 
        
        setLocation(getX(), newY ); 
        isJumping = false; 
        vSpeed = 3; 
    }
    
      
     private void collisionOffset()
    {
       Actor rightWall = getOneObjectAtOffset( getImage().getWidth()/4 , 0, Platform.class); 
           
       if (rightWall !=null)
       {
           setLocation(getX()-5, getY()); 
       }
         Actor leftWall = getOneObjectAtOffset( -getImage().getWidth()/4 , 0, Platform.class); 
       
       if (leftWall !=null)
       {
           setLocation(getX()+5, getY()); 
       }
         Actor topWall = getOneObjectAtOffset( 0 , -getImage().getHeight()/4 , Platform.class); 
       
       if (topWall !=null)
       {
           setLocation(getX(), getY()); 
       }
    }
    
      private void collisionOffset2()
    {
       Actor rightWall = getOneObjectAtOffset( getImage().getWidth()/4 , 0, Platform2.class); 
           
       if (rightWall !=null)
       {
           setLocation(getX()-5, getY()); 
       }
         Actor leftWall = getOneObjectAtOffset( -getImage().getWidth()/4 , 0, Platform2.class); 
       
       if (leftWall !=null)
       {
           setLocation(getX()+5, getY()); 
       }
         Actor topWall = getOneObjectAtOffset( 0 , -getImage().getHeight()/4 , Platform2.class); 
       
       if (topWall !=null)
       {
           setLocation(getX(), getY()); 
       }
    }
    
    
    private void worldEnd()
    {
       if (getY() == 649)
       {
        setLocation(120, 75); 
       }
    }
    
    
     private boolean checkCeiling()
    {
        int spriteHeight = getImage().getHeight();
        int lookForCeiling = spriteHeight/2;
        
        Actor ceiling =getOneObjectAtOffset( 0, -lookForCeiling, Platform.class); 
        
        if( ceiling != null) 
        {
            bopHead(ceiling); 
            return true;
        }
        else
        {
            return false; 
        }
    }
    
    private void arrow()
    {
        if(getWorld() instanceof Instructions)
        {
            if (isTouching(Arrow.class))
            {
                Greenfoot.setWorld(new level1() ); 
            }
        }   
    }
    
    //private void pickupstick()
    //{
        //if(getWorld() instanceof level1)
        //{
            //if (isTouching(Stick.class))
            //{
                //setImage(stick);
            //}
       // }
    //}
   
    
}
Bashni Bashni

2024/5/5

#
when I turn off one of the onGround(); it works but not when all are on anything I can do to fix this?
danpost danpost

2024/5/6

#
Bashni wrote...
when I turn off one of the onGround(); it works but not when all are on anything I can do to fix this?
I suggest that you add a new class. A subclass of Actor called Ground that can be extended to all the objects that your character can stand or bop its head on (Floor1, Platform and Platform2). That way you only need to check for the Ground class instead of checking all three different types separately. Same for the collision offsets -- for each direction, you would only need one check for Ground class).
You need to login to post a reply.