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

2021/4/20

I wanna reduce the hitbox of an actor in order to make it stop from floating. I'm making a game and one of my actors is floating above the ground due to its original size, is there anyway I can fix this from within the code. My code for that actor is:

TejasKanch TejasKanch

2021/4/20

#
import greenfoot.*;  
public class bowman extends Actor
{
  private int vspeed =5;
    private int accelaration =1; 
      public bowman(){
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/4; 
        int myNewWidth = (int)myImage.getWidth()/4;
        myImage.scale(myNewWidth, myNewHeight); 
    }
     int cooldown = 0;
    public void act() 
    {
                checkFall();
        if (Greenfoot.isKeyDown("a"))
        {
            move(-4 );
          
        }

        if ( Greenfoot.isKeyDown("d")) 
        {
            
            move(4);
            
        }
        if ( Greenfoot.isKeyDown("e"))
        {
            
            down();
           
        }
         if ( Greenfoot.isKeyDown("w")&& (cooldown == 0))
        {
            
            jump();
           cooldown = 39;
        }
         checkFire();
         
            if(cooldown > 0) {
            cooldown -= 1;
        }
    }
      public void down(){
       vspeed = 5;
     
       
    }
    public void jump(){
       vspeed = -17;
     
       
    }
        
        
    public void checkFall(){
    if (onGround()){
        vspeed = 0; 
    }
    else{fall();}
    }
    public  boolean onGround(){
        Actor under = getOneObjectAtOffset(0, getY()/2, Ground.class); 
        return under != null; 
    }
    
    public void fall(){
    setLocation (getX(), getY() +vspeed);
    vspeed = vspeed + accelaration; 
}

    public void checkFire()
{
   if(Greenfoot.isKeyDown("s") && (cooldown == 0)) {
                getWorld().addObject (new arrowright (), getX()+0, getY()-2);
                cooldown = 20;
}}}
danpost danpost

2021/4/20

#
Insert after line 10:
setImage(myImage);
If that doesn't work, try trimming transparency from the image (any row of pixels that are all transparent).
TejasKanch TejasKanch

2021/4/20

#
it doesn't work, how do I do that
danpost danpost

2021/4/20

#
TejasKanch wrote...
it doesn't work, how do I do that
Download my Image Transparency Adder/Trimmer scenario. Open it in greenfoot, then close it (to create scenario folder). Use file directory to locate scenario folder. Open folder and find executable jar file within. Run jar file. Load image from your scenario's image folder, then save it.. It will automatically be trimmed. Close executable and resume your project.
TejasKanch TejasKanch

2021/4/20

#
thank you so much
danpost danpost

2021/4/20

#
TejasKanch wrote...
thank you so much
Please "Like", "Share" and "Subscribe". Thank you in advance. Oh, wait ... that's YouTube (lol)
You need to login to post a reply.