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

yesterday

Hello how could i summarize my code

SanjiNaha SanjiNaha

yesterday

#
; import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Pxl1 here. * * @author (your name) * @version (a version number or a date) */ public class Pxl1 extends Actor { private GreenfootImage image1; private GreenfootImage image2; private GreenfootImage image3; private GreenfootImage image4; private GreenfootImage image5; private int vSpeed = 0; private int acceleration = 1; private int jumpHeight= - 8; Fire fire = new Fire(); /** * Act - do whatever the Pxl1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { movement(); constructor(); checkFall(); pictures(); fireball(); } public void constructor() { image1 = new GreenfootImage("Fighter-1.png"); image2 = new GreenfootImage("Fighter-punch.png"); image3 = new GreenfootImage("move.png"); image4 = new GreenfootImage("Fighter-kicking.png"); image5 = new GreenfootImage("move_forward.png"); } /** * This method lets the character move */ public void movement() { int y = getY(); int x = getX(); if(Greenfoot.isKeyDown("d")) { move(5); setImage("move_forward.png"); } if(Greenfoot.isKeyDown("a")) { move(-5); setImage("move.png"); } else { setImage("Fighter-1.png"); } if(Greenfoot.isKeyDown("w")) { jump(); } } public void pictures() { if(Greenfoot.isKeyDown("e")) { setImage("Fighter-punch.png"); } else { setImage("Fighter-1.png"); } if(Greenfoot.isKeyDown("q")) { setImage("Fighter-kicking.png"); } if(Greenfoot.isKeyDown("a")) { setImage("move.png"); } if(Greenfoot.isKeyDown("d")) { setImage("move_forward.png"); } } /** * makes the character jump */public void jump() { vSpeed= -5; gravity(); } /** * makes the character fall */ public void checkFall() { if(onGround()) { vSpeed = 0; } else { gravity(); } } /** * makes the fall look real */ private void gravity() { setLocation(getX(), getY() +vSpeed); vSpeed = vSpeed + acceleration; } /** * makes the character land on the ground */ public boolean onGround() { Actor under = getOneObjectAtOffset(0,getImage().getHeight()/2,Ground.class); return under != null; } public void fireball() { if (Greenfoot.isKeyDown("r")) { setImage("Fighter-punch.png"); World MyWorld = getWorld(); MyWorld.addObject(fire, 0, 0); fire.setLocation(getX(), getY()); fire.setRotation(getRotation()); } } }
danpost danpost

yesterday

#
SanjiNaha wrote...
how could i summarize my code
A class with useless image fields and unnecessarily repetitive action. Your GreenfootImage fields, image1 thru image5 are never used and the constructor method, called every act cycle, constantly creates new images for those fields. There is more code that is for naught. Let's clean it up a bit:
import greenfoot.*; 

public class Pxl1 extends Actor
{
    private int vSpeed;
    Fire fire = new Fire();
    
    public void act() 
    {
        movement();
        checkFall();
        pictures();
        fireball();
    }
    
    public void movement()
    {
        setImage("Fighter-1.png");
        if (Greenfoot.isKeyDown("d"))
        {
            move(5);
            setImage("move_forward.png");
        }
        if( Greenfoot.isKeyDown("a"))
        {
            move(-5);
            setImage("move.png");
        }
        if (Greenfoot.isKeyDown("w")) jump();
   }

   public void pictures()
    {
        if (Greenfoot.isKeyDown("e")) setImage("Fighter-punch.png");
        if (Greenfoot.isKeyDown("q")) setImage("Fighter-kicking.png");
        if (Greenfoot.isKeyDown("a")) setImage("move.png");
        if (Greenfoot.isKeyDown("d")) setImage("move_forward.png");
    }

    public void jump()
    {
        vSpeed= -5;   
        gravity();
    }
    
    public void checkFall()
    {
        if (onGround()) vSpeed = 0;  else gravity();
    }
   
    private void gravity()
    {
         setLocation(getX(), getY()+vSpeed);
         vSpeed++;
    }
    
    public boolean onGround()
    {
        return getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class) != null;
    }
    
    public void fireball()
    {
        if (Greenfoot.isKeyDown("r"))
        {
            setImage("Fighter-punch.png");
            getWorld().addObject(fire, getX(), getY());
            fire.setRotation(getRotation());
        }
    }
}
You need to login to post a reply.