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
5
Vercility Vercility

2018/2/8

#
I just realized my solution has an IndexOutOfBoundsError (<= 17), Dans solution also works.
LaurentiuRohan LaurentiuRohan

2018/2/9

#
danpost wrote...
/** 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.
danpost wrote...
/** 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.
I've already made this, excepting the last 2 line of codes, and Thanks for that , but I don't know how the code you showed to me upper works and where I have to put it, I tried some solutions but doesn't compile, it has to be in a " public void check key"? Could you explain a little bittle detailed or an example that shows where the code you told me has to be, please? About this one am I talking
if (leftIdleAnimation != currentAnimation && rightIdleAnimation != currentAnimation)
{
    if (runningRightAnimation == currentAnimation || jumpRightAnimation == currentAnimation)
        currentAnimation = idleRightAnimation;
    else
        currentAnimation = idleLeftAnimation;
    frame = 1;
}
danpost danpost

2018/2/9

#
Well, you should check both horizontal movement keys before you decide what needs done:
// save animation state
GreenfootImage[] inAnimation = currentAnimation;
int atFrame = frame;

// horizontal movement
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx  == 0) // idle
{
    // code you questioned goes here (use 'frame = 0')
}
else if (dx == 1 && currentAnimation != runningRightAnimation) // right
{
    currentAnimation = runningRightAnimation;
    frame = 0;
}
else if (dx == -1 && currentAnimation != runningLeftAnimation) // left
{
    currentAnimation = runningLeftAnimation;
    frame = 0;
}

/** vertical movement code here */

// run animation
setImage(currentAnimation[frame);
frame = (frame+1)%currentAnimation.length;
danpost danpost

2018/2/9

#
If the jump animation was found to continue, restore the saved state (the horizontal movement code may have changed the animation; so, restoring the state allows the jump animation to continue normally). The code above goes either in the act method or in a method called from the act method.
LaurentiuRohan LaurentiuRohan

2018/2/11

#
danpost wrote...
If the jump animation was found to continue, restore the saved state (the horizontal movement code may have changed the animation; so, restoring the state allows the jump animation to continue normally). The code above goes either in the act method or in a method called from the act method.
Thanks, it works now, but if left or right key is pressed the left or right aimation plays infintely without returning to the idle animation. How could I solve that?
danpost danpost

2018/2/11

#
LaurentiuRohan wrote...
if left or right key is pressed the left or right aimation plays infintely without returning to the idle animation.
Show the code that produces that behavior so it can be examined.
LaurentiuRohan LaurentiuRohan

2018/2/12

#
danpost wrote...
LaurentiuRohan wrote...
if left or right key is pressed the left or right aimation plays infintely without returning to the idle animation.
Show the code that produces that behavior so it can be examined.
I solved this part with the animations. But now I have another one with jumping. I want that the characther to jump 10 when up key is pressed, but the problem is that he jumps continously. I mean if key up is pressed continously he is jumping contimously, I don't want, I want that him to jump 10 if he is on ground and after that to comeback on it even is up key is pressed, so he has to jump only when returns to the ground and not in air when key is pressed. And the boolean "onGrounds" is for my with "s" because I have a class with more different ground subclasees, so I don't think that is the problem for what I want. Could you help with that please? This the code used for jumping :
public class Puppy_Player extends Actor
{
      private int vSpeed = 5;
    private int acceleration = 2;
    private int jumpStrenght = 10;
    public boolean jumping;
    private int speed = 4;
    private boolean onGrounds;

public void checkFall()
  {
    if(onGround()) 
     {
        vSpeed = 0;
     } 
    else 
     {
        fall();
     }
    }
    
  public boolean onGround()
   {
       Actor under = getOneObjectAtOffset(0,getImage().getHeight() /2  ,Grounds.class);
       return under != null;
   }
   
  public void fall()
   {
       setLocation(getX(), getY() + vSpeed);
       vSpeed = vSpeed + acceleration;
   }
   
   public void jump()
   {
       vSpeed = -jumpStrenght;
       fall();
   }    
}
danpost danpost

2018/2/12

#
Insufficient code. I do not see where 'jump' is called from.
LaurentiuRohan LaurentiuRohan

2018/2/12

#
danpost wrote...
Insufficient code. I do not see where 'jump' is called from.
Oh ,sory, here it is:
public void checkKey()
   {
       if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
        }
       if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
       if(Greenfoot.isKeyDown("up"))
        {
                jump();
        }  
   }
danpost danpost

2018/2/12

#
Okay. If you only want to be able to jump when on ground, you should probably check for on ground before jumping:
if (onGround() && Greenfoot.isKeyDown("up"))
LaurentiuRohan LaurentiuRohan

2018/2/15

#
danpost wrote...
Okay. If you only want to be able to jump when on ground, you should probably check for on ground before jumping:
if (onGround() && Greenfoot.isKeyDown("up"))
Thanks for you answer. Now I'm a little bit confused with making my actor as mainActor. I a lot of scenarios t that I've seen one actor is the main. But I didn't found any code that make it main. I mean in some of worlds there are codes for the mainActor, but how the main actor is defined ?
danpost danpost

2018/2/15

#
The term 'mainActor' is just a field name. There is no special significance given to that name than any other name you could give it (other than to the one looking at the code).
LaurentiuRohan LaurentiuRohan

2018/2/15

#
danpost wrote...
The term 'mainActor' is just a field name. There is no special significance given to that name than any other name you could give it (other than to the one looking at the code).
Well I've tried to make like your scenario "Scrooling SuperWorld(UP) So in my Scrooling_Worlds class I tried to do hat you've done too, where it was this "A main actor MUST be supplied." i changed with "puppy Player so ScrollingWorld look like this :
public void setScrollingBackground(GreenfootImage scrollingBackground)    
    {
        if(mainActor==null)
        {
            System.out.println("setMainActor Puppy_Player");
            System.out.println("");
            return;
        }
        background = new GreenfootImage(scrollingBackground);
        background.scale(scrollingWidth*getCellSize(), scrollingHeight*getCellSize());
        scrollBackground();
    }
    
    public void fillScrollingBackground(GreenfootImage fillImage)
    {
        if(mainActor==null)
        {
            System.out.println("setMainActor Puppy_Player");
            System.out.println("");
            return;
        }
        if (fillImage.getWidth()<getWidth() && fillImage.getHeight()<getHeight())
        {
            setBackground(new GreenfootImage(fillImage));
            fillImage = getBackground();
        }
        background = new GreenfootImage(scrollingWidth*getCellSize(), scrollingHeight*getCellSize());
        for (int x=0; x<background.getWidth(); x+=fillImage.getWidth())
            for (int y=0; y<background.getHeight(); y+=fillImage.getHeight())
                background.drawImage(fillImage, x, y);
        scrollBackground();
    }
And to add the main actor to world I wrote your code too :
  setMainActor(new Puppy_Player(), 250, 300); // the int parameters are centered window x and y ranges
But say's that can't find symbol methodsetMainActor(Puppy_Player,int,int) Can you tell me how could I solve this please?
Super_Hippo Super_Hippo

2018/2/15

#
Why exactly did you change the print commands?
danpost danpost

2018/2/15

#
First of all, no changes are meant to be done in the SWorld class (but, I guess if you only changed what was printed to the terminal, it should not mess things up any). Secondly, the SWorld class has a 'setMainActor(Actor actor)' method which you need in your Scrolling_World class (as well as the 'Actor mainActor' field, if not already there). Btw, the SWorld class, as is, was meant to be re-usable support class. That is, you can add it to any scenario and subclass it with a world that you want scrolling implemented in. However, it can be difficult to work with and may not be compatible with all types of scrolling. I, more recently, wrote aScroller class. which is a support class that is easy to work with and does all types of scrolling. It is well documented and has a tutorial to go along with it.
There are more replies on the next page.
1
2
3
4
5