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

2014/4/26

Wondering about efficiency.

avatarcolin avatarcolin

2014/4/26

#
Hello, I am working on a project, and hit a lot of problems when doing gravity, I finally got it to work but I am wondering if anything in the code can be fixed to be more efficient. If you guys could give me any tips or anything that would be awesome! thx!
private int delay;
    private int acceleration = 2;
    private boolean jumping;
    private int vSpeed = 18;
    private int jumpSpeed = 18;
    private int spriteHeight;
    public void act()   
    {  
        checkKeys();  
        checkFall();
        delay++;
    }    
    private int cycle;//cycle timer 
    private int rightAnimate = 0;//image num for right
    private int leftAnimate = 0;//image num for left
    private boolean left;//check to see if his direction is left
    public void checkKeys()  
    {  
        if(Greenfoot.isKeyDown("a"))//left
        {    
            left = true;//his direction is left
            this.setLocation(getX()-acceleration,getY());
            if(onGround())
                cycle++; 
            else
                setImage("playerUp0.png");
            if (cycle <= 7)
            {
                setImage("playerLeft" + leftAnimate +".png");
            }
            if(leftAnimate >= 2)
            {
                leftAnimate = 0;
            }
            if(cycle >= 10)
            {
                cycle = 0;
                leftAnimate++;
            }
        }  
        else if(Greenfoot.isKeyDown("d"))//right
        {    
            left = false;//his direction is not left
            this.setLocation(getX()+acceleration,getY());
            if(onGround())
                cycle++;
            else
                setImage("playerUp1.png");

            if (cycle <= 7)
                setImage("playerRight" + rightAnimate +".png");

            if(rightAnimate >= 2)
                rightAnimate = 0;

            if(cycle >= 10)
            {
                cycle = 0;
                rightAnimate++;
            }
        }  
        if(Greenfoot.isKeyDown("space"))//jumping
        {  
            if(jumping == false)
            {
                jump();
                Greenfoot.playSound("jetpack.wav");
            }
            if(left == true)
                setImage("playerUp0.png");
            else
                setImage("playerUp1.png");
        }  
        if (Greenfoot.isKeyDown("w") &&(delay%12==0))//shooting
        {
            shoot();
        }
    }
    public void jump()  
    {  
        vSpeed = vSpeed - jumpSpeed;
        fall();
        jumping = true;
        getWorld().addObject(new fire(this),getX(),getY());
    }
    public boolean onGround()  
    {  
        spriteHeight = getImage().getHeight();//measures sprite heigth
        int lookForGround = (int)(spriteHeight/2) + 5;//(int) = no decimal!
        Actor ground = getOneObjectAtOffset(0, lookForGround, ground.class);
        if(ground == null)//no ground
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }
    public void fall()  
    {  
        setLocation (getX(), getY() + vSpeed - 7); 
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    } 
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int y = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), y);
        jumping = false;
    }
    public void checkFall()  
    {  
        if (onGround()) 
        {  
            vSpeed = 0;  
        }  
        else 
        {  
            fall();
        }  
    } 
    public void shoot()
    {
        getWorld().addObject(new bullet(left),getX(), getY() + spriteHeight - 50);
        Greenfoot.playSound("laser.wav");
    }
    public int getMyX()
    {
        int myX = getX();
        return myX;
    }
    public int getMyY()
    {
        int myY = getY();
        return myY;
    }
    public boolean getLeft()
    {
       return left;
    }
danpost danpost

2014/4/26

#
I did not see anything that might be considered extremely inefficient. However, I was wondering why you have the methods 'getMyX' and 'getMyY' in the class. It seems a bit redundant, considering you can just use the 'getX' and 'getY' methods supplied by the Actor class.
avatarcolin avatarcolin

2014/4/27

#
I was passing the X and Y to another class and used a getter to pass it through.
danpost danpost

2014/4/27

#
But, you do not need to create a 'get' method for it -- they already exist. The 'getX' and 'getY' methods ARE the 'get' methods for it.
avatarcolin avatarcolin

2014/4/27

#
Oh! Thanks, didn't realize it was.
You need to login to post a reply.