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

2017/2/3

Moving Animation

Hondrok Hondrok

2017/2/3

#
It blocks and I can't see the problem
import greenfoot.*;
   
public class Mario extends Actor
{
    private int speed = 7;
    
    private int acceleration = 2;
    
    public int armour = 3;
    //frame is to keep track of what frame of animation you are on.
	private int frame;
	//direction is supoosed to be either less than 0 for left, or more than 0 for right.
	private int direction;
	//to check if Mario is standing still.
	private boolean standingStill;
	//the power of the jump.
	private int jumpStrength;
	//to keep track of whether Mario is jumping.
	private boolean jumping;
	//the power of gravity.
	private double gravityStrength;
	//the vertical speed of Mario
	private double vSpeed;
    
	public Mario()
	{
	    setImage("Mario_fr0.png");
	    frame = 0;
	    direction = 0;
	    jumpStrength = 30;
	    jumping = false;
	    gravityStrength = 3;
	    vSpeed = 0;
	}
    
	public void act() 
    {
       if (! getWorld().getObjects(GameOver.class).isEmpty()) 
       {
           return;
       }      
       checkFall();
       if(!jumping) {
            standingStill = true;
       }
       if(Greenfoot.isKeyDown("left")){
        	moveLeft();
        	direction = -1;
            if(onGround()) {
            	animateWalking();
                mirror();
            }
        	//if Mario is walking left, she is not StandingStill.
            standingStill = false;
       }
        else if(Greenfoot.isKeyDown("right")){
        	moveRight();
        	direction = 1;
            if(onGround())
            	animateWalking();
        	//if Mario is walking right, she is not StandingStill.
            standingStill = false;
       }
       if(Greenfoot.isKeyDown("z")){
            if(!jumping)
                jump();
            animateJumping();
            if(direction < 0)
                mirror();
       }
       attack();
       checkFall();
       hitGoomba();
       hitCastle_l1();
       hitCastle_l2();
       checkArmour();
    }
    
    public void checkArmour()
    {
        Actor sphere = getOneIntersectingObject(Sphere_left.class);
        if(sphere != null)
        {
           armour = armour - 1;
           ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
           getWorld().removeObject(sphere);
        }
        
        if(armour == 0)
        {
            getImage().setTransparency(0);
            World world = new Game_Over_Lose();
            Greenfoot.setWorld(world);
        }
    }
    
    public void animateWalking()
    {
        if(frame == 0) {setImage("Mario_fr0.png");}
        else if(frame == 1) {setImage("Mario_fr1.png");}
        else if(frame == 2) {setImage("Mario_fr2.png");}
        else if(frame == 3) {setImage("Mario_fr3.png");}
        else if(frame == 4) {setImage("Mario_fr4.png");}
        else if(frame == 5) {setImage("Mario_fr5.png");}
        else if(frame == 6) {setImage("Mario_fr6.png");}
        else if(frame == 6) {    
            setImage("Mario_fr7.png");
            frame = 0;
            return;
        }
        frame++;
    }
    
    public void animateJumping()
    {
        if(frame == 0) {setImage("Mario_fr0.png");}
        else if(frame == 1) {setImage("Mario_fr1.png");}
        else if(frame > 1) {
            setImage("Mario_fr2.png");
        }
        frame++;
    }
    
    private void moveLeft()
    {
        setLocation(getX() - 10, getY());
    }
    
    private void moveRight()
    {
        setLocation(getX() + 10, getY());
    }
    
    private void jump()
    {
        frame = 0;
        vSpeed -= jumpStrength;
        fall();
        jumping = true;
    }
    
    private void checkFall()
    {
        if(!onGround()){
        	fall();
        }
        if(underGround()) {
        	setLocation(getX(), getY() - 2);
            jumping = false;
            vSpeed = 0;
        }
    }
    
    private void fall()
    {
        setLocation(getX(), getY() + (int)vSpeed);
        vSpeed += gravityStrength;
    }
    
    public boolean onGround()
    {
        if(getY() > 400) {
        	return true;
        }
        return false;
    }
    
    public boolean underGround()
    {
        if(getY() > 420) {
        	return true;
        }
        return false;
    }
    
    public void mirror()
    {
        getImage().mirrorHorizontally();
    }
    
    public void attack()
    {
        if("space".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Sphere_right(), getX() + 5, getY());
        }
    }

    public void hitGoomba()
    {
        Actor goomba = getOneIntersectingObject(Goomba.class);
        if(goomba != null)
        {
            armour = armour - 1;
            getWorld().removeObject(goomba);
            ((Health_Bar) getWorld().getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
     
    public void hitCastle_l1()
    {
        Actor castle = getOneIntersectingObject(Castle.class);
        if(castle != null)
        {
            World world = new Level_2();
            Mario mario2 = (Mario)world.getObjects(Mario.class).get(0);
            mario2.armour = this.armour;
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
    
    public void hitCastle_l2()
    {
        Actor castle = getOneIntersectingObject(Castle_l2.class);
        if(castle != null)
        {
            World world = new Final_level();
            Mario mario3 = (Mario)world.getObjects(Mario.class).get(0);
            mario3.armour = this.armour; 
            Greenfoot.setWorld(world);
            ((Health_Bar)world.getObjects(Health_Bar.class).get(0)).updateImage(armour);
        }
    }
}
danpost danpost

2017/2/4

#
Hondrok wrote...
It blocks and I can't see the problem < Code Omitted >
Please be more specific as to your issue (what exactly do you want to happen and how is it currently behaving that you do not want).
Hondrok Hondrok

2017/2/5

#
danpost wrote...
Hondrok wrote...
It blocks and I can't see the problem < Code Omitted >
Please be more specific as to your issue (what exactly do you want to happen and how is it currently behaving that you do not want).
fixed the problem thank you anyway
You need to login to post a reply.