Thursday, November 8, 2012

Running, Jumping and falling


Instead of working on  chapter 6 I have been working on another project where I am learning how to switch images when moving  a picture and make it able to stay on a ground and when it is not on the ground it falls. I will continue watching the tutorial so that I can make it my actor jump. Here are the links for the videos I have been watching Part 1  Part 2. So in my project I have a pig. The pig can move left and right. I found the picture of the pig on google images. When I got the picture I put it in word and flipped it so that it could face to the left. Then when I typed in the code I told it that it would move to the right but also have the picture face the right and when I am moving to the left the pig face the left. This what the code looks like.

 private void checkkeys()
    {
        if (Greenfoot.isKeyDown("A") )
        {
            setImage("Pig Left.png");
            moveLeft();
        }
        if (Greenfoot.isKeyDown("D") )
        {
            setImage("Pig Right.png");
            moveRight();
        }

This is also the first time I used a moveRight(); method. Usually I use move(4) and Move (-4). I have to say the moveRight() method is more complex and I don't see me using it that much. You can't tell it just to move right. You have to say the speed and then write this  public void moveRight()
    {
        setLocation ( getX() + speed, getY() );
    }
    public void moveLeft()
    {
        setLocation ( getX() - speed, getY() );
    }


So ya that was new.  Then I learned how to make the pig fall.  I first needed to make a command called public void fall(). Then I set the vertical speed to 0 and made it accelerate by 2. Here is the code.

public void fall()
    {
        setLocation ( getX(), getY() + vspeed);
        vspeed = vspeed + acceleration;
       
    }

Then I wanted to make the Pig stand onground and not fall. This was pretty complex and hard for me but the video helped a lot.
public boolean onGround()
    {
        Actor under = getOneObjectAtOffset ( 0, getHeight() / 2, Ground.class);
        return under != null;
    }   


I am going to finish watching the videos and learn how to make the pig jump. There are some flaws in the project through. For example, its seems whenever I compile it everything works fine for once but when I set it up manually without compiling it doesn’t work right. I might change into a game once I fix everything. It probably be having to get to a certain point to another while avoiding certain obstacles.

Here's the project so far 





No comments:

Post a Comment