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

2017/5/18

I need help with my code the actor is supposed to run and jump but it just floats and moves left and right need help due monday

Programmingprodigy Programmingprodigy

2017/5/18

#
     int jumpHeight=9;
    public int speed=0;
    private int vSpeed=0;
    private int acceleration=2;
    public int jumpstrength=15;
    private Actor under;
    private int walkSpeed=5;
    private double fallSpeed=0.4;
    private boolean inTheAir=false;
    private double deltaX =0;
    private double deltaY =0;
    private int groundHeight=getImage().getHeight()/2;
    private int sideWidth =getImage().getWidth()/2;
    private World DesWorld1;
    int worldHeight;
    int worldWidth;
   
   
    public void addedtoWorld(World DesWorld1)
   {
     
     this.DesWorld1=DesWorld1;
      this.worldHeight=DesWorld1.getHeight();
      this.worldWidth=DesWorld1.getWidth();
    }

    /**
     * Act - do whatever the Bee wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       if(inTheAir)
       {
        Fall();
       } else{
       { 
           getCommand();
       }
       if (((DesWorld1)(getWorld())).isPaused == false) 
       {
            
            CheckFall();
            CheckKeys();
           
           
          


       }
    }
    move();
    }
    /**
     * Act - do whatever the Bee wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void run (String direction)
    {
        {
           if(direction=="left")
           deltaX =walkSpeed*-1;
           else
           deltaX =walkSpeed;
        }
    }
    public void stop()
   {
        deltaX= 0;
    }
    public void CheckKeys()   
    {   
        if(Greenfoot.isKeyDown("a"))
       {
            
            moveLeft();
       }
       if(Greenfoot.isKeyDown("up"))
       {
            
            jump();
       }
       if(Greenfoot.isKeyDown("space"))
       {
            
            Jump();
       }
        if(Greenfoot.isKeyDown("D"))
        
       {
            
            moveRight();
       }
     
    }
    public void moveRight()

    {
        setLocation (getX() + speed,getY());

    }
    public void moveLeft()
    {
        setLocation (getX() - speed,getY());

     }
    public void CheckFall()
    {
       if(onGround()) {
       vSpeed=0;
        }
       else{
       Fall();
      }
    }
    public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getWorld().getHeight() /8,Ground.class);
        if (under !=null)
        return false;
        else
        return true;
    }
    
    public void Fall()
    { 
        setLocation (getX(), getY()+ vSpeed);
        vSpeed =vSpeed + acceleration;
    }  
     public void fall()
    { 
       deltaY=+fallSpeed;
    }  
      public void Jump()
    { 
        vSpeed +=jumpstrength;
        Fall();
    }      
      public void jump()
    { 
      deltaY +=jumpHeight;
      inTheAir=false;
    }      
     public void move()
    { 
     double newX = getX() + deltaX;
     double newY = getY() + deltaY;
     Actor platformBelow = getOneObjectAtOffset(0,groundHeight+ 5,Ground.class);
     Actor platformAbove = getOneObjectAtOffset(0, -(groundHeight+5),Ground.class);
     Actor platformToLeft = getOneObjectAtOffset(sideWidth+5,0,Ground.class);
     Actor platformToRight = getOneObjectAtOffset(-(sideWidth+5),0,Ground.class);
       if(platformBelow!=null)
       {
           if(deltaY< 0)
       {
        deltaY =0;
        inTheAir=false;
        GreenfootImage platformImage =platformBelow.getImage();
        int topOfPlatform=platformBelow.getY() - platformImage.getHeight()/2;
        newY = topOfPlatform- groundHeight;
      }else if(getY()>=worldHeight -groundHeight){
       deltaY=0;
       inTheAir=false;
       newY = worldHeight -groundHeight;
       }
          
      else { 
        inTheAir=false;  
      }
      }
     if(platformAbove!=null)
     {
      if(deltaY>0)
      {
        deltaY=0;
        GreenfootImage platformImage=platformAbove.getImage();
        int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2;
        newY= bottomOfPlatform + groundHeight;
       }
     }
     if(platformToLeft!=null)
     {
      
       deltaX=Math.abs(deltaX);
     }
        if(platformToRight!=null)
     {
      deltaX=Math.abs(deltaX) *-1;
    }
        if(getX()<=sideWidth)
    {
        deltaX=Math.abs(deltaX);
    }
          if(getX()>=worldWidth-sideWidth)
    {
        deltaX=Math.abs(deltaX);
    }
    setLocation((int)newX,(int)newY);
    }
    public void getCommand()
    {
          if(Greenfoot.isKeyDown("left"))
    
        {
            run("left");
        } else if ((Greenfoot.isKeyDown("right")))
        { 
           run("right");
        }
        else
        { 
           stop();
        }          
    }
    
       /**
     * Method gameOver
     *
     */
     public void disapear()
    {
       
    }
        /**
     * Method gameOver
     *
     */
    
    private void displayGameOver()//game over
    {
        Greenfoot.stop();
        GameOver GameOver = new GameOver();
        getWorld().addObject(GameOver, getWorld().getWidth()/2, getWorld().getHeight()/2); 
    }
   }
You need to login to post a reply.