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

2015/3/17

Character animation in a scrolling World help!

OwlyL OwlyL

2015/3/17

#
I have a problem with my little jump and run game. I painted 6 pictures of the character to animate that he's walking. the animation works until the player reaches the point where the screen starts to scroll. The animation only doesn't work while scrolling, it's just showing one picture of the character. Has anybody an idea where the problem in my code is?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Herman is the Player
 * he can move to the left or the right
 * can jump and shoot
 * he also can collect sandwiches to fill his lifebar
 * 
 * @author (Lukas Hauser) 
 * @version (V5 16.03.15)
 */
public class Player extends Erm
{  
    private int hermanSpeed = 3;
    GreenfootImage hermanWalk1 = new GreenfootImage ("Herman1R.png");        GreenfootImage hermanWalk7 = new GreenfootImage ("Herman1L.png");
    GreenfootImage hermanWalk2 = new GreenfootImage ("Herman2R.png");        GreenfootImage hermanWalk8 = new GreenfootImage ("Herman2L.png");
    GreenfootImage hermanWalk3 = new GreenfootImage ("Herman3R.png");        GreenfootImage hermanWalk9 = new GreenfootImage ("Herman3L.png");
    GreenfootImage hermanWalk4 = new GreenfootImage ("Herman4R.png");        GreenfootImage hermanWalk10 = new GreenfootImage ("Herman4L.png");
    GreenfootImage hermanWalk5 = new GreenfootImage ("Herman5R.png");        GreenfootImage hermanWalk11 = new GreenfootImage ("Herman5L.png");
    GreenfootImage hermanWalk6 = new GreenfootImage ("Herman6R.png");        GreenfootImage hermanWalk12 = new GreenfootImage ("Herman6L.png");

    public int animationCount = 1;
    public int frame = 1;

    GreenfootSound pickUpItem = new GreenfootSound ("PickUpItem.mp3");
    boolean touchingEnemy = false;
    private int movementSpeed =5;
    private int fallingSpeed = 0;
    private int gravity = 1;
    private int jumpPower = -19;
    Bullet Bullet = new Bullet();
    /**
     * Here we call our methods from below
     * and a part of the Fall method to check if the number 3 was given to call the jump method, if not he falls
     */
    public void act()
    {
        fireOnCommand();
        hitEnemy ();
        changeWorld ();
        checkKeys();
        GameOver();
        Collect();
        if(isOnGround())
        {
            if(user_input() == 3)
            {
                jump();
            }
        }
        else
        {
            fall();            
        }

    }

    /**
     * This Method let you collect sandwiches
     */
    public void Collect()
    {
        Actor sandwich = getOneIntersectingObject(Sandwich.class);
        if (sandwich !=null)
        {
            World myWorld=getWorld();
            myWorld.removeObject(sandwich);
            pickUpItem.play();
        }
    }

    /**
     * This method sets the Game Over screen when you fall out of the world
     */
    public void GameOver()
    {
        World myWorld = getWorld();
        if (isAtEdge())
        {
            GameOver gameover = new GameOver ();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); //Adds the gameover object in the middle of the world
            Greenfoot.stop(); 
        }
    }

    /**
     * This method checks if the Player is touching the ground
     */
    public boolean isOnGround()     //check if Player Touches Ground
    {
        //window Window = (window) getWorld();
        // if(getY() + (getImage().getHeight() /2) >= (Window.getHeight() - 1))
        // {
        //     return true;
        // }

        Actor ground = getOneObjectAtOffset (0, 79, Map.class);
        if(ground == null)
        {
            return false;
        }
        else
        {
            return true;
        }

    }

    public void jump()      
    {
        fallingSpeed = jumpPower;
        fall();
    }

    public void fall()
    {
        setLocation(getX(), getY() + fallingSpeed);
        fallingSpeed = fallingSpeed + gravity;
    }

    /**
     * This method makes a smooth jumping with gravity by pressing the up key
     */
    public int user_input()
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Erm erm = (Erm) theWorld.getErm();

        if (Greenfoot.isKeyDown("up"))   //checks if the "up" key is pressed
        {
            return 3;                    //defines the number "3" for pressing the "up" key
        }

        return 5;

    }

    /**
     * This is the Method for the left and right movement of the Player
     * it checks if the left or right button is pressed and moves the Player left or right with an animation of the walking
     */
    public void checkKeys ()
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Erm erm = (Erm) theWorld.getErm();
        if(Greenfoot.isKeyDown("Right") && erm.shouldScroll == false) //checks if the right Key is Pressed and the Scroll method from class Erm
        {
            moveRight();                    //moves the Player right by pressing left arrow
            if (animationCount % 6 == 0)    //slows the animation down
                animation1();                   //calls the animation method

            animationCount += 1;

        }

        else if(Greenfoot.isKeyDown("Left") && erm.shouldScroll == false) //checks if the left Key is Pressed and the Scroll method from class Erm
        {
            moveLeft();                     //moves the Player left by pressing left arrow
            if (animationCount % 6 == 0)    //slows the animation down
                animation2();                   //calls the animation method

            animationCount += 1;
        }
    }

    public void moveRight()
    {
        setLocation(getX() + movementSpeed, getY());
    }

    public void moveLeft()
    {
        setLocation(getX() - movementSpeed, getY());
    }

    /**
     * This is the animation Method where the different pictures are set for every single Frame of the movement
     * This is the animation for walking to the right side
     */
    public void animation1()
    {
        if(frame == 1)
        {
            setImage (hermanWalk1);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (hermanWalk2);
            frame = 3;
        }
        else if(frame == 3)
        {
            setImage (hermanWalk3);
            frame = 4;
        }
        else if(frame == 4)
        {
            setImage (hermanWalk4);
            frame = 5;
        }
        else if(frame == 5)
        {
            setImage (hermanWalk5);
            frame = 6;
        }
        else if(frame == 6)
        {
            setImage (hermanWalk6);
            frame = 1;

        }
    }

    /**
     * This is the animation Method where the different pictures are set for every single Frame of the movement
     * This is the animation for walking to the left side
     */
    public void animation2()
    {
        if(frame == 1)
        {
            setImage (hermanWalk7);
            frame = 2;
        }
        else if(frame == 2)
        {
            setImage (hermanWalk8);
            frame = 3;
        }
        else if(frame == 3)
        {
            setImage (hermanWalk9);
            frame = 4;
        }
        else if(frame == 4)
        {
            setImage (hermanWalk9);
            frame = 5;
        }
        else if(frame == 5)
        {
            setImage (hermanWalk10);
            frame = 6;
        }
        else if(frame == 6)
        {
            setImage (hermanWalk11);
            frame = 1;

        }
    }

    /**
     * This method makes the player shooting a bullet by pressing the space key
     */
    public void fireOnCommand()
    {
        if(Greenfoot.isKeyDown("space"))             //checks if the "enter" key is pressed
        {
            World Area = getWorld();
            Area.addObject(Bullet, 0,0);
            Bullet.setLocation(getX(), getY());     //adds the bullet at location from the player

        }
    }  

    /**
     * This method decreases the healthbar by touching the enemy
     * you can touch a enemy 4 times until the healthbar is empty, then the Gameover screen is called
     */
    public void hitEnemy ()
    {
        Actor Enemy1 = getOneIntersectingObject (Enemy1.class);
        if (Enemy1 != null)
        {
            World myWorld = getWorld();
            ScrollingWorld scrollingworld = (ScrollingWorld)myWorld;
            HealthBar healthbar = scrollingworld.getHealthBar();

            if (touchingEnemy == false)          //checks whether the player is touching an enemy
            {
                healthbar.loseHealth();
                touchingEnemy = true;           //makes sure that the player loses only one health touching the same Enemy

                if (healthbar.health <=0)
                {
                    GameOver gameover = new GameOver ();
                    myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2); //Adds the gameover object in the middle of the world
                    Greenfoot.stop(); 
                    //[myWorld.removeObject(this);]
                }

            }

        } 
        else
        {
            touchingEnemy =false;
        }
    }

    /**
     * This method changes the World by touching the Goal Flag
     */
    public void changeWorld ()
    {
        if(isTouching(Goal.class)) //checks if the goal class is touched
        {
            Greenfoot.setWorld(new Level2()); //sets a new world/level 
        }
    }

}
danpost danpost

2015/3/17

#
OwlyL wrote...
Has anybody an idea where the problem in my code is?
You are restricting your player from moving and running its animation with the 'if' condition on lines 146 and 156 of 'erm.shouldScroll == false'.
OwlyL OwlyL

2015/3/17

#
Ahh i understand why it stops the animation, but i have no idea how to figure it out that the character keeps the animation. If i delete the 'erm.shouldScroll == false' my character is moving faster than the screen scrolls.
danpost danpost

2015/3/17

#
The movement of the player should be independent of the scrolling. The scrolling, on the other hand, should be dependent on the location of the player.
OwlyL OwlyL

2015/3/18

#
I have no idea how to separate the movement and the scrolling, beacause, the Player.class is a subclass of the scrolling class. here is the class which extends the Player class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sactor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Erm extends Actor
{
    public boolean shouldScroll; //A boolean to determine if scrolling is needed, according to how far left/right across the screen the player is.
    public int playerX; //The player's x value
    
    public void act() 
    {
        updateShouldScroll(200, 500); //This should be in the act method so that the shouldScroll boolean is constantly updated.
    }
    
    /**
     * This method checks the players x position and sets the shouldScroll boolean accordingly.
     * @param The x value limits within which scrolling is not required for left/right movement.
     */
    public void updateShouldScroll(int minX, int maxX)
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        playerX = player.getX();
        if(playerX <= minX || playerX >= maxX)
        {
            shouldScroll = true;
        }
        else
        {
            shouldScroll = false;
        }
        
        if(shouldScroll == true && (playerX >= 500 && Greenfoot.isKeyDown("left")) || (playerX <= 200 && Greenfoot.isKeyDown("right")))
        {
            shouldScroll = false;
        }
    }
}
danpost danpost

2015/3/18

#
What are the limits of scrolling in this project (horizontal, vertical, or both? distance limited, image size limited, or not limited at all?) and what is being scrolled (the background, the actors, or both)?
OwlyL OwlyL

2015/3/18

#
Only Horizontal Scrolling, the Scrolling should start until the player reaches the middle of the screen. The distance should be limited, image size not. It would be nice if the actor and the background would scroll. I am searching for an relative easy way to solve this, a way i can easily relate.
danpost danpost

2015/3/18

#
You gave 'image size not' as well as 'would be nice if the actor and the background would scroll'. Would the background image be a repeating one, then? Maybe you could use my Easy Infinite Scroller Support Class which would work for a repeating background image or for no background scrolling at all. Although, without more information about your scrolling, it would be difficult to say if it is the correct type of engine for your scenario. The scenario with the scrolling engine has several examples of its use and my Wombat Racing scenario also utilizes that engine (for another example).
OwlyL OwlyL

2015/3/19

#
Thanks for your advices. I fixed the Bug per seperating the animation from scrolling. was more easier than I thought!
 public void mAnimation()
    {
        if (Greenfoot.isKeyDown("Right"))
        {                   //moves the Player right by pressing left arrow
            if(animationCount % 6 == 0)    //slows the animation down
                animation1();                   //calls the animation method

            animationCount += 1;
            
        }
        else if (Greenfoot.isKeyDown("Left"))
        {                   //moves the Player left by pressing left arrow
            if (animationCount % 6 == 0)    //slows the animation down
                animation2();                   //calls the animation method

            animationCount += 1;
            
        }
    }
    
    /**
     * This is the Method for the left and right movement of the Player
     * it checks if the left or right button is pressed and moves the Player left or right with an animation of the walking
     */
    public void checkKeys ()
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Scroll scroll = (Scroll) theWorld.getScroll();
        if(Greenfoot.isKeyDown("Right") && scroll.shouldScroll == false)  //checks if the right Key is Pressed and the Scroll method from class Scroll
        {
            moveRight();                    //moves the Player right by pressing left arrow
            //if(animationCount % 6 == 0)    //slows the animation down
            //    animation1();                   //calls the animation method

            //animationCount += 1;

        }

        else if(Greenfoot.isKeyDown("Left") && scroll.shouldScroll == false) //checks if the left Key is Pressed and the Scroll method from class Scroll
        {
            moveLeft();                     //moves the Player left by pressing left arrow
            //if (animationCount % 6 == 0)    //slows the animation down
            //    animation2();                   //calls the animation method

            //animationCount += 1;
        }
    }
You need to login to post a reply.