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

2018/2/7

Play charater idle animation if there aren't any keys pressed

1
2
3
4
LaurentiuRohan LaurentiuRohan

2018/2/7

#
Hello everyone. I try to make a sidescrooling game with a main actor called Puppy. I want to add to my characther some animation, running left, right, jump right and left, and idle left and right with more frames for each animation. Now, I finished making the animation for left and right runing when key are pressed, but I don't know how to make it jump when up arrow key is pressed and being idle if there isn't any key pressed. For example if Puppy runs to the left and then he stops I want the idle animation start until he runs again, same for the right side and jumping. And I want to say that after I added eaxh image for left and right idle animation, my actor isn't spawning in the world, but only with runing and jumping, is there a limit for adding images? Please help guys. Here is my code :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Puppy_Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Puppy_Player extends Actor
{
       private int vSpeed = 0;
    private int acceleration = 2;
    public boolean jumping;
    private int jumpStranght = 20;
    private int speed = 4;
    
    /*** Running Left Images*/
   
    private GreenfootImage runright1 = new GreenfootImage("Puupy_RR1.png");
    private GreenfootImage runright2 = new GreenfootImage("Puupy_RR2.png");
    private GreenfootImage runright3 = new GreenfootImage("Puupy_RR3.png");
    private GreenfootImage runright4 = new GreenfootImage("Puupy_RR4.png");
    private GreenfootImage runright5 = new GreenfootImage("Puupy_RR5.png");
    private GreenfootImage runright6 = new GreenfootImage("Puupy_RR6.png");
    private GreenfootImage runright7 = new GreenfootImage("Puupy_RR7.png");
    private GreenfootImage runright8 = new GreenfootImage("Puupy_RR8.png");
    private GreenfootImage runright9 = new GreenfootImage("Puupy_RR9.png");
    private GreenfootImage runright10 = new GreenfootImage("Puupy_RR10.png");
    private GreenfootImage runright11 = new GreenfootImage("Puupy_RR11.png");
    private GreenfootImage runright12 = new GreenfootImage("Puupy_RR12.png");
    private GreenfootImage runright13 = new GreenfootImage("Puupy_RR13.png");
    private GreenfootImage runright14 = new GreenfootImage("Puupy_RR14.png");
    private GreenfootImage runright15 = new GreenfootImage("Puupy_RR15.png");
    private GreenfootImage runright16 = new GreenfootImage("Puupy_RR16.png");
    private GreenfootImage runright17 = new GreenfootImage("Puupy_RR17.png");
    private GreenfootImage runright18 = new GreenfootImage("Puupy_RR18.png");
    
    /*** Running Right Images*/
   
    private GreenfootImage runleft1 = new GreenfootImage("Puupy_RL1.png");
    private GreenfootImage runleft2 = new GreenfootImage("Puupy_RL2.png");
    private GreenfootImage runleft3 = new GreenfootImage("Puupy_RL3.png");
    private GreenfootImage runleft4 = new GreenfootImage("Puupy_RL4.png");
    private GreenfootImage runleft5 = new GreenfootImage("Puupy_RL5.png");
    private GreenfootImage runleft6 = new GreenfootImage("Puupy_RL6.png");
    private GreenfootImage runleft7 = new GreenfootImage("Puupy_RL7.png");
    private GreenfootImage runleft8 = new GreenfootImage("Puupy_RL8.png");
    private GreenfootImage runleft9 = new GreenfootImage("Puupy_RL9.png");
    private GreenfootImage runleft10 = new GreenfootImage("Puupy_RL10.png");
    private GreenfootImage runleft11 = new GreenfootImage("Puupy_RL11.png");
    private GreenfootImage runleft12 = new GreenfootImage("Puupy_RL12.png");
    private GreenfootImage runleft13 = new GreenfootImage("Puupy_RL13.png");
    private GreenfootImage runleft14 = new GreenfootImage("Puupy_RL14.png");
    private GreenfootImage runleft15 = new GreenfootImage("Puupy_RL15.png");
    private GreenfootImage runleft16 = new GreenfootImage("Puupy_RL16.png");
    private GreenfootImage runleft17 = new GreenfootImage("Puupy_RL17.png");
    private GreenfootImage runleft18 = new GreenfootImage("Puupy_RL18.png");
    
   /*** IJump Right Images*/
   
    private GreenfootImage jumpright1 = new GreenfootImage("Puppy_JumpRight1.png");
    private GreenfootImage jumpright2 = new GreenfootImage("Puppy_JumpRight2.png");
    
    /*** Jump Left Images*/
   
    private GreenfootImage jumpleft1 = new GreenfootImage("Puppy_JumpLeft1.png");
    private GreenfootImage jumpleft2 = new GreenfootImage("Puppy_JumpLeft2.png");
    
     /*** Idle Right*/
   
    private GreenfootImage inactiveright1 = new GreenfootImage("Puupy_Inactive_Right1.png");
    private GreenfootImage inactiveright2 = new GreenfootImage("Puupy_Inactive_Right2.png");
    private GreenfootImage inactiveright3 = new GreenfootImage("Puupy_Inactive_Righ3.png");
    private GreenfootImage inactiveright4 = new GreenfootImage("Puupy_Inactive_Right4.png");
    private GreenfootImage inactiveright5 = new GreenfootImage("Puupy_Inactive_Right5.png");
    private GreenfootImage inactiveright6 = new GreenfootImage("Puupy_Inactive_Right6.png");
    private GreenfootImage inactiveright7 = new GreenfootImage("Puupy_Inactive_Right7.png");
    private GreenfootImage inactiveright8 = new GreenfootImage("Puupy_Inactive_Right8.png");
    private GreenfootImage inactiveright9 = new GreenfootImage("Puupy_Inactive_Right9.png");
    private GreenfootImage inactiveright10 = new GreenfootImage("Puupy_Inactive_Right10.png");
    private GreenfootImage inactiveright11 = new GreenfootImage("Puupy_Inactive_Right11.png");
    
    /*** Idle Right*/
   
    private GreenfootImage inactiveleft1 = new GreenfootImage("Puupy_Inactive_Left1.png");
    private GreenfootImage inactiveleft2 = new GreenfootImage("Puupy_Inactive_Left2.png");
    private GreenfootImage inactiveleft3 = new GreenfootImage("Puupy_Inactive_Left3.png");
    private GreenfootImage inactiveleft4 = new GreenfootImage("Puupy_Inactive_Left4.png");
    private GreenfootImage inactiveleft5 = new GreenfootImage("Puupy_Inactive_Left5.png");
    private GreenfootImage inactiveleft6 = new GreenfootImage("Puupy_Inactive_Left6.png");
    private GreenfootImage inactiveleft7 = new GreenfootImage("Puupy_Inactive_Left7.png");
    private GreenfootImage inactiveleft8 = new GreenfootImage("Puupy_Inactive_Left8.png");
    private GreenfootImage inactiveleft9 = new GreenfootImage("Puupy_Inactive_Left9.png");
    private GreenfootImage inactiveleft10 = new GreenfootImage("Puupy_Inactive_Left10.png");
    private GreenfootImage inactiveleft11 = new GreenfootImage("Puupy_Inactive_Left10.png");
    

   
    private int frame = 1;
    /**
     * Act - do whatever the Doors_Level_Select wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKey();
        
    }   
    
    public void moveRight()
     {
         setLocation(getX()+speed, getY());
         AnimateRight();
        }
        
         public void moveLeft()
     {
         setLocation(getX()-speed, getY());
         AnimateLeft();
        }
        
        public void checkKey()
        {
            if(Greenfoot.isKeyDown("right"))
            {
                moveRight();
            }
           if(Greenfoot.isKeyDown("left"))
            {
                moveLeft();
            }
        
          
        }
    
    public void AnimateRight()
   {
       if(frame == 1)
       {
           setImage(runright1);
        }
        else if(frame == 2 )
        {
            setImage(runright2);
        }
        else if(frame == 3 )
        {
            setImage(runright3);
        }
        else if(frame == 4 )
        {
            setImage(runright4);
        }
        else if(frame == 5 )
        {
            setImage(runright5);
        }
        else if(frame == 6 )
        {
            setImage(runright6);
        }
        else if(frame == 7 )
        {
            setImage(runright7);
        }
        else if(frame == 8 )
        {
            setImage(runright8);
        }
        else if(frame == 9 )
        {
            setImage(runright9);
        }
        else if(frame == 2 )
        {
            setImage(runright2);
        }
        else if(frame == 10 )
        {
            setImage(runright10);
        }
        else if(frame == 11 )
        {
            setImage(runright11);
        }
        else if(frame == 12 )
        {
            setImage(runright12);
        }
        else if(frame == 13 )
        {
            setImage(runright13);
        }
        else if(frame == 14 )
        {
            setImage(runright14);
        }
        else if(frame == 15 )
        {
            setImage(runright15);
        }
        else if(frame == 16 )
        {
            setImage(runright16);
        }
        else if(frame == 17 )
        {
            setImage(runright17);
        }
         else if(frame == 18 )
        {
            setImage(runright18);
            frame = 1;
            return;
        }
        frame ++;
        
    }
    
        
         public void AnimateLeft()
   {
       if(frame == 1)
       {
           setImage(runleft1);
        }
        else if(frame == 2 )
        {
            setImage(runleft2);
        }
         else if(frame == 3 )
        {
            setImage(runleft3);
        }
         else if(frame == 4 )
        {
            setImage(runleft4);
        }
         else if(frame == 5 )
        {
            setImage(runleft5);
        }
         else if(frame == 6 )
        {
            setImage(runleft6);
        }
         else if(frame == 7 )
        {
            setImage(runleft7);
        }
         else if(frame == 8 )
        {
            setImage(runleft8);
        }
         else if(frame == 9 )
        {
            setImage(runleft9);
        }
         else if(frame == 10 )
        {
            setImage(runleft10);
        }
         else if(frame == 11 )
        {
            setImage(runleft11);
        }
         else if(frame == 12 )
        {
            setImage(runleft12);
        }
         else if(frame == 13 )
        {
            setImage(runleft13);
        }
         else if(frame == 14 )
        {
            setImage(runleft14);
        }
         else if(frame == 15 )
        {
            setImage(runleft15);
        }
         else if(frame == 16 )
        {
            setImage(runleft16);
        }
         else if(frame == 17 )
        {
            setImage(runleft17);
        }
         else if(frame == 18 )
        {
            setImage(runleft18);
            frame = 1;
            return;
        }
        frame ++;
        
    }
 
    }
        
    

danpost danpost

2018/2/7

#
LaurentiuRohan wrote...
after I added eaxh image for left and right idle animation, my actor isn't spawning in the world, but only with runing and jumping, is there a limit for adding images?
There is a memory limit. What is the average size of these images?
LaurentiuRohan LaurentiuRohan

2018/2/7

#
It's 100x100 pixels
danpost danpost

2018/2/7

#
LaurentiuRohan wrote...
It's 100x100 pixels
With 60 images, that is equivalent to a good sized world background in itself. You also have a scrolling world. Are you manipulating the world background image during scrolling? and if so, what is the size of your world and what is the size of the background image you are applying to it?
LaurentiuRohan LaurentiuRohan

2018/2/7

#
Oh sory, I've just found the problem with the images. I've imported an image in Greenfoot that it wasn't in my images folder, when I saved each image from a gif image with photoshop I was hurry and I forgot one. So the problem with importing the images is solved, now I have to create the animation with jumping when up arrow key pressed and idle when none...Could you help with that please?
Vercility Vercility

2018/2/7

#
Use a boolean idle variable and set it false whenever a move key is held down and true else Why didn't you just use a singular line tho
setImage(new GreenfootImage("Puupy_RR" +Integer.toString(frame)+".png"));
danpost danpost

2018/2/7

#
Vercility wrote...
Use a boolean idle variable and set it false whenever a move key is held down and true else Why didn't you just use a singular line tho
setImage(new GreenfootImage("Puupy_RR" +Integer.toString(frame)+".png"));
Using a boolean is not a very good idea. The "idle"ness can be determined by neither left or right keys down and not in air. The single line of code would be okay if the image was not changing so often. With both animation and scrolling, you would want to do as much as possible to limit CPU usage. Reading from the file directory is a time-consuming operation (as far as computer ops).
Vercility Vercility

2018/2/7

#
Yeah I figured just now too, was about to edit. Array would be fine to reduce code
 
setImage(frameArray[frame]) ;
Don't understand your problem with the boolean tho What's your approach
danpost danpost

2018/2/7

#
Vercility wrote...
Yeah I figured just now too, was about to edit.
I was also about to edit and say this also -- the last idle state is useless as far as what the current state may be.
Vercility Vercility

2018/2/7

#
But that's why Continually update it :o It May fall behind in one act cycle assuming it's calculated before movement comes in but that shouldn't be a problem
danpost danpost

2018/2/7

#
LaurentiuRohan wrote...
I have to create the animation with jumping when up arrow key pressed and idle when none...Could you help with that please?
I am seasoned and am set in my ways as far as movement where jumping is involved. I do not save any jump state in a field -- I check the on ground state on the fly (during the current act step). I also separate horizontal movement from vertical movement and do collision checking for each. I dislike the way most beginners program their jumping/moving codes (they get their codes, or ideas for them, from what other beginners have created). On the other foot, beginners seem to not like the way I do it -- I guess because I do not use a bunch of simple straight-forward methods. I usually just have this for an act method:
public void act()
{
    horizontalMovement();
    verticalMovement();
}
and just have those two methods to accomplish everything that needs done. I use a local 'onGround' variable in the 'verticalMovement' method, set to 'false' and, during collision checking (or ground checking), set it to 'true' if needed; then, at the end of the method, if it is 'true', check for jump key and initiate jumping if the key is detected.
danpost danpost

2018/2/7

#
Vercility wrote...
Don't understand your problem with the boolean tho What's your approach
With all the animations, I would put each in a array of their own and then have a single array for the current animation. As the animation set changes, the individual arrays can be set to the current animation array. That way, it can be easily checked to see which animation is currently active. For a pseudo-code example:
if (runningRightAnimation == currentAnimation)
then either the current animation continues or a different animation is set. If nether the "left" or "right" keys were found down, then:
if (leftIdleAnimation != currentAnimation && rightIdleAnimation != currentAnimation)
{
    if (runningRightAnimation == currentAnimation || jumpRightAnimation == currentAnimation)
        currentAnimation = idleRightAnimation;
    else
        currentAnimation = idleLeftAnimation;
    frame = 1;
}
This is only an example of what you might have. The order of priority of the animations will need to be considered. Jump animation takes priority over the other animations; so, before changing the image set to something else, the on ground state would need checked after falling. Maybe the current animation and frame should be saved at the beginning of the act method so any changes can be undone. That way the image set can be changed to an idle or running set and reset back to a jump set if the actor is still in the middle of a jump.
LaurentiuRohan LaurentiuRohan

2018/2/8

#
danpost wrote...
Vercility wrote...
Don't understand your problem with the boolean tho What's your approach
With all the animations, I would put each in a array of their own and then have a single array for the current animation. As the animation set changes, the individual arrays can be set to the current animation array. That way, it can be easily checked to see which animation is currently active. For a pseudo-code example:
if (runningRightAnimation == currentAnimation)
then either the current animation continues or a different animation is set. If nether the "left" or "right" keys were found down, then:
if (leftIdleAnimation != currentAnimation && rightIdleAnimation != currentAnimation)
{
    if (runningRightAnimation == currentAnimation || jumpRightAnimation == currentAnimation)
        currentAnimation = idleRightAnimation;
    else
        currentAnimation = idleLeftAnimation;
    frame = 1;
}
This is only an example of what you might have. The order of priority of the animations will need to be considered. Jump animation takes priority over the other animations; so, before changing the image set to something else, the on ground state would need checked after falling. Maybe the current animation and frame should be saved at the beginning of the act method so any changes can be undone. That way the image set can be changed to an idle or running set and reset back to a jump set if the actor is still in the middle of a jump.
Sory but I don't understand , I'm a little bit confused, how my code should be with that sinlge array?
Vercility Vercility

2018/2/8

#
GreenfootImage[] array = new GreenfootImage[18];
for ( int i = 0; i <= 18; i++) {
array[i - 1] = new GreenfootImage("Puupy_RR" +Integer.toString(i) +".png");

public void animateRight(){
setImage(array[frame-1]);
}
-1 because your image counting doesnt start at 0 but Arrays do.
danpost danpost

2018/2/8

#
/** Running Right Images */
private final GreenfootImage[] runningRightAnimation =
{
    new GreenfootImage("Puupy_RR1.png"),
    new GreenfootImage("Puupy_RR2.png"),
    new GreenfootImage("Puupy_RR3.png"),
    new GreenfootImage("Puupy_RR4.png"),
    new GreenfootImage("Puupy_RR5.png"),
    new GreenfootImage("Puupy_RR6.png"),
    new GreenfootImage("Puupy_RR7.png"),
    new GreenfootImage("Puupy_RR8.png"),
    new GreenfootImage("Puupy_RR9.png"),
    new GreenfootImage("Puupy_RR10.png"),
    new GreenfootImage("Puupy_RR11.png"),
    new GreenfootImage("Puupy_RR12.png"),
    new GreenfootImage("Puupy_RR13.png"),
    new GreenfootImage("Puupy_RR14.png"),
    new GreenfootImage("Puupy_RR15.png"),
    new GreenfootImage("Puupy_RR16.png"),
    new GreenfootImage("Puupy_RR17.png"),
    new GreenfootImage("Puupy_RR18.png")
};

/** Running Left Images */
private final GreenfootImage[] runningLeftAnimation =
{
    new GreenfootImage("Puupy_RL1.png"),
    new GreenfootImage("Puupy_RL2.png"),
    new GreenfootImage("Puupy_RL3.png"),
    new GreenfootImage("Puupy_RL4.png"),
    new GreenfootImage("Puupy_RL5.png"),
    new GreenfootImage("Puupy_RL6.png"),
    new GreenfootImage("Puupy_RL7.png"),
    new GreenfootImage("Puupy_RL8.png"),
    new GreenfootImage("Puupy_RL9.png"),
    new GreenfootImage("Puupy_RL10.png"),
    new GreenfootImage("Puupy_RL11.png"),
    new GreenfootImage("Puupy_RL12.png"),
    new GreenfootImage("Puupy_RL13.png"),
    new GreenfootImage("Puupy_RL14.png"),
    new GreenfootImage("Puupy_RL15.png"),
    new GreenfootImage("Puupy_RL16.png"),
    new GreenfootImage("Puupy_RL17.png"),
    new GreenfootImage("Puupy_RL18.png")
};

/** Jumping Right Images */
private final GreenfootImage[] jumpingRightAnimation =
{
    new GreenfootImage("Puppy_JumpRight1.png"),
    new GreenfootImage("Puppy_JumpRight2.png")
};

/** Jumping Left Images */
private finalGreenfootImage[] jumpingLeftAnimation =
{
    new GreenfootImage("Puppy_JumpLeft1.png"),
    new GreenfootImage("Puppy_JumpLeft2.png")
};

/** Idlie Right Images */
private final GreenfootImage[] idleRightAnimation =
{
    new GreenfootImage("Puupy_Inactive_Right1.png"),
    new GreenfootImage("Puupy_Inactive_Right2.png"),
    new GreenfootImage("Puupy_Inactive_Right3.png"),
    new GreenfootImage("Puupy_Inactive_Right4.png"),
    new GreenfootImage("Puupy_Inactive_Right5.png"),
    new GreenfootImage("Puupy_Inactive_Right6.png"),
    new GreenfootImage("Puupy_Inactive_Right7.png"),
    new GreenfootImage("Puupy_Inactive_Right8.png"),
    new GreenfootImage("Puupy_Inactive_Right9.png"),
    new GreenfootImage("Puupy_Inactive_Right10.png"),
    new GreenfootImage("Puupy_Inactive_Right11.png")
};

/** Idle Left Images */
private final GreenfootImage[] idleLeftAnimation =
{
    new GreenfootImage("Puupy_Inactive_Left1.png"),
    new GreenfootImage("Puupy_Inactive_Left2.png"),
    new GreenfootImage("Puupy_Inactive_Left3.png"),
    new GreenfootImage("Puupy_Inactive_Left4.png"),
    new GreenfootImage("Puupy_Inactive_Left5.png"),
    new GreenfootImage("Puupy_Inactive_Left6.png"),
    new GreenfootImage("Puupy_Inactive_Left7.png"),
    new GreenfootImage("Puupy_Inactive_Left8.png"),
    new GreenfootImage("Puupy_Inactive_Left9.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png"),
    new GreenfootImage("Puupy_Inactive_Left10.png")
};

/** Current Animation */
private GreenfootImage[] currentAnimation = idleRightAnimation;
The last field can be reassigned any animation set at any time. Only use it for setting the image of the actor. Allow 'frame' to be zero and reset to zero any time the image set changes or immediately upon increasing its value to the length of the current animation set.
There are more replies on the next page.
1
2
3
4