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

2018/12/26

Game Creation: Directional Idle, Shooting and Jumping.

1
2
3
4
Unlimited Unlimited

2018/12/26

#
Hello everyone, im trying to create a game for class and im having a couple of problems. for example i cannot seem to figure out how to animate directional idle animations and shooting animations. Having a idle animation in one direction was simple enough since i only animated one direction. However i dont know how to tell the programm in what direction i have previously used as in to give my game information what idle or standing shooting animation to use. Next Problem is, if i want to create an improved jump i am facing two problems. First, i do not know how to program a jump in which i can control the high of so to give it that megaman feel. Second, nor do i know if i am able to create such a jump how to tell the game to play a different animation after the apex of the jump and when landing rather then after some unchangable time . The following is the entire code of my Character. It would be really nice if somebody could help me. I know there are probably already threats about these problems but despite my hardest effort i was not able to understand them.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
    
    
    
    
   
    
    private GreenfootImage Stand1 = new GreenfootImage("Stand_01.png");
    private GreenfootImage Stand2 = new GreenfootImage("Stand_02.png");
    private GreenfootImage Stand3 = new GreenfootImage("Stand_03.png");
    private GreenfootImage Stand4 = new GreenfootImage("Stand_04.png");
    private GreenfootImage Stand5 = new GreenfootImage("Stand_05.png");
    private GreenfootImage Stand6 = new GreenfootImage("Stand_06.png");
    private GreenfootImage Stand7 = new GreenfootImage("Stand_07.png");
    private GreenfootImage Stand8 = new GreenfootImage("Stand_08.png");
    private GreenfootImage Stand9 = new GreenfootImage("Stand_09.png");
    
    private GreenfootImage WalkA1 = new GreenfootImage("Walk-A_01.png");
    private GreenfootImage WalkA2 = new GreenfootImage("Walk-A_02.png");
    private GreenfootImage WalkA3 = new GreenfootImage("Walk-A_03.png");
    private GreenfootImage WalkA4 = new GreenfootImage("Walk-A_04.png");
    private GreenfootImage WalkA5 = new GreenfootImage("Walk-A_05.png");
    private GreenfootImage WalkA6 = new GreenfootImage("Walk-A_06.png");
    private GreenfootImage WalkA7 = new GreenfootImage("Walk-A_07.png");
    private GreenfootImage WalkA8 = new GreenfootImage("Walk-A_08.png");
    private GreenfootImage WalkA9 = new GreenfootImage("Walk-A_09.png");
    private GreenfootImage WalkA10 = new GreenfootImage("Walk-A_10.png");
    private GreenfootImage WalkA11 = new GreenfootImage("Walk-A_11.png");
    
    private int frame = 1;
    private int animationCounter = 0;
   
    
   
    

    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
        
        
        
        
        ceiling();
        
        
        
        animationCounter ++;
        
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
            
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
            
        }
        
        
        
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
        move();
        canMoveRight();
        canMoveLeft();
        checkFall();
        
        
    }
    
    
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=14;
        
        
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=14;
        
        
        
        
            
        
        
        if(Greenfoot.isKeyDown("right") && canMoveRight()){
            if(animationCounter % 2 == 0)
                animateWalkA();        
            }
            else
            {
                 if(animationCounter % 6 == 0)
                 animateStand();
            }
        setLocation(x,y);
    }
    
    
    
   
    

    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2+30);
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Highway.class);
        
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
        
    }
    
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
        
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
        
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
            
            return false;
        }
        
    }
    
    
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
        
        
    }
    
    
   
    
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
       
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
       
       return canMoveLeft;
        
    }
    
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
       
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
       
        return canMoveRight;
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
        
        setLocation(getX(), newY);
        jumping = false;
        
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
            
        }
        else
        {
            fall();
        }

    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
        
        
       
    }
    
    
    
    
    
    
    public void animateStand()
    {
        if(frame == 1)
        {
            setImage(Stand1);
            
        }
        else if(frame == 2)
        {
            setImage(Stand2);
            
        }
        else if(frame == 3)
        {
            setImage(Stand3);
            
        }
        else if(frame == 4)
        {
            setImage(Stand4);
        }
        else if(frame == 5)
        {
            setImage(Stand5);
            
        }
        else if(frame == 6)
        {
            setImage(Stand6);
            
        }
        else if(frame == 7)
        {
            setImage(Stand7);
            
        }
        else if(frame == 8)
        {
            setImage(Stand8);
            
        }
        else if(frame == 9)
        {
            setImage(Stand9);
            frame = 1;
            return;
       }
       frame ++;
    }
    
    public void animateWalkA()
    {
        if(frame == 1)
        {
            setImage(WalkA1);
            
        }
        else if(frame == 2)
        {
            setImage(WalkA2);
            
        }
        else if(frame == 3)
        {
            setImage(WalkA3);
            
        }
        else if(frame == 4)
        {
            setImage(WalkA4);
        }
        else if(frame == 5)
        {
            setImage(WalkA5);
            
        }
        else if(frame == 6)
        {
            setImage(WalkA6);
            
        }
        else if(frame == 7)
        {
            setImage(WalkA7);
            
        }
         else if(frame == 8)
        {
            setImage(WalkA8);
            
        }
         else if(frame == 9)
        {
            setImage(WalkA9);
            
        }
         else if(frame == 10)
        {
            setImage(WalkA10);
            
        }
        else if(frame == 11)
        {
            setImage(WalkA11);
            frame = 3;
            return;
       }
       frame ++;
    }
    
    
}
 
danpost danpost

2018/12/26

#
To change jump height, just add or subtract a small value (maybe up to 10) when applying jumpstrength to vSpeed. Maybe adding a parameter to the jump method would help, to pass the jump strength to it:
private void jump(int strength)
{
    vSpeed = -strength; // strength should be set to vSpeed, not just subtracted from its value
    jumping = true;
    fall();
}
Then a normal jump would be coded:
jump(jumpStrength);
and a small jump might be:
jump(jumpStrength-10);
For better control on your animations, you might consider placing each animation image set in a separate array. Then you can add a field to indicate which array of images is currently in use. You could also add a field to indicate which animation set is "pending" to be used next.
Unlimited Unlimited

2018/12/26

#
Uh yes that entire last part, could you explain that a little more as in what exactly would i have to write? I am a Novice at this so im sorry for the troubles. I am yet not able to make up my own code, i can only look up whats been done before.
danpost danpost

2018/12/26

#
Unlimited wrote...
Uh yes that entire last part, could you explain that a little more as in what exactly would i have to write?
By placing the images of your animation sets into arrays:
private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] rightAnim; = new GreenfootImage[11];

static
{
    for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
    for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
}
they will be organized in groups. The 'static' modifier makes the members (fields declared in lines 1 and 2) class members, as opposed to instance members, allowing the arrays to be set up at compile time instead of during run time. This is allowed, as all instances of the class can use the same image sets. Next, you can add an instance field to indicate which image set is currently being used by an actor of the class:
private GreenfootImage[] animSet = standAnim;
Initially setting it to the standing image set makes sense as no key states are evaluated until the act method is executed on the actor. Finally, running the current animation needs to be done with a little care as the animation sets are not all of equal length. Also, your frame and animationCounter fields can be combined into one counter (animCount). However, because they do not all have equal frame rates, you will need another field for its value and it must be adjusted any time you change the current animation set:
// with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)

// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}
The animate method can be written as follows:
private void animate()
{
    animCount = (animCount+1)%(animSet.length*animDelay);
    if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay];
}
That is it. You can dispense with your animateStand and animateWalkA codes.
danpost danpost

2018/12/26

#
Sidenote: I was never much a fan of "if (onGround()) { ... } else fall();" code. This is akin to saying gravity does not exist unless you are up in the air. I prefer:
boolean onGround = false; // assume in air
vSpeed += acceleration; // apply gravity
setLocation(getX(), getY()+vSpeed); // fall
Actor actor = getOneIntersectingObject(Actor.class);
if (actor != null)
{ // colliding with another actor
    int vDir = (int)Math.signum(vSpeed); // vertical direction of movement
    vSpeed = 0; // kill vertical speed
    if (vDir > 0) onGround = true; // landing and not bopping head
    setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/2); // assigning stop location
}
if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
This does pretty much everything for vertical movement (initiating a jump, falling and colliding with other actors).
Unlimited Unlimited

2018/12/26

#
I really appreciate the effort but i have to say this is all a little complicated for me. All that code, i dont understand from where its coming. When i copy it i also get errors so i dont know what to do. I tried using a Counter for a work around as following.
public void Counter()
    {
        
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
        
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
public void animationCheckRight()
    {
        
        
        if(Greenfoot.isKeyDown("right") && canMoveRight() )
        {
            if(animationCounter % 4 == 0){
                animateWalkA();
                
            }
            }
            else
            {
                if(animationCounter % 6 == 0 && Counter==0){
                 animateStand();
                 
                }
            }
        
        
    }
    
    public void animationCheckLeft()
    {
        
        
        
        if(Greenfoot.isKeyDown("left") && canMoveRight() )
        {
            if(animationCounter % 4 == 0){
                animateWalkLeftA();
                
            }
        }
    }
And this seems to work i think, as in when i press left he does the proper animation. However now i have the problem after a couple of seconds if i stop running there is like a 50% chance that the animation breaks. This happens regartless of where i have the animation. It doesnt matter if i have it in move, act or in its own thing. Do you maybe a have a idea on why that happens?
Unlimited Unlimited

2018/12/26

#
I really have a hard time understanding what youre writing, is there maybe an easier way for the programm to not keep on breaking the animation or like telling it to restart?
danpost danpost

2018/12/27

#
Unlimited wrote...
I really appreciate the effort but i have to say this is all a little complicated for me. All that code, i dont understand from where its coming.
You can ask about any specific part of it for clarification (one part at a time).
When i copy it i also get errors so i dont know what to do. I tried using a Counter for a work around as following. << Codes Omitted >> And this seems to work i think, as in when i press left he does the proper animation. However now i have the problem after a couple of seconds if i stop running there is like a 50% chance that the animation breaks. This happens regartless of where i have the animation. It doesnt matter if i have it in move, act or in its own thing. Do you maybe a have a idea on why that happens?
You should not need a counter. Please show the current class codes (in its entirety). Also, if you are getting errors on it, copy/paste them here so we can better understand what might be going on.
Unlimited Unlimited

2018/12/27

#
This is the entire code. Im getting errors at: private static GreenfootImage rightAnim; = new GreenfootImage; But that is Probably because there is a ; to many. Then in the same line im getting errors at the . However if i remove the ; after rightAnim im getting errors at all these lines: private void setAnim(GreenfootImage anim, int frameRate) { animSet = anim; animCount = -1; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation } // with the following fields private int animCount; // counter for animations private int animDelay = 6; // initially set for standing animation (frame rate field) // this method might help private void setAnim(GreenfootImage anim, int frameRate) { animSet = anim; animCount = -1; // prepares to set first image when incremented animDelay = frameRate; animate(); // calls a method to set a frame of this newly set animation }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
    
    
    
    
 
    
    
    private GreenfootImage Stand1 = new GreenfootImage("Stand_01.png");
    private GreenfootImage Stand2 = new GreenfootImage("Stand_02.png");
    private GreenfootImage Stand3 = new GreenfootImage("Stand_03.png");
    private GreenfootImage Stand4 = new GreenfootImage("Stand_04.png");
    private GreenfootImage Stand5 = new GreenfootImage("Stand_05.png");
    private GreenfootImage Stand6 = new GreenfootImage("Stand_06.png");
    private GreenfootImage Stand7 = new GreenfootImage("Stand_07.png");
    private GreenfootImage Stand8 = new GreenfootImage("Stand_08.png");
    private GreenfootImage Stand9 = new GreenfootImage("Stand_09.png");
    
    private GreenfootImage StandLeft1 = new GreenfootImage("StandLeft_09.png");
    private GreenfootImage StandLeft2 = new GreenfootImage("StandLeft_08.png");
    private GreenfootImage StandLeft3 = new GreenfootImage("StandLeft_07.png");
    private GreenfootImage StandLeft4 = new GreenfootImage("StandLeft_06.png");
    private GreenfootImage StandLeft5 = new GreenfootImage("StandLeft_05.png");
    private GreenfootImage StandLeft6 = new GreenfootImage("StandLeft_04.png");
    private GreenfootImage StandLeft7 = new GreenfootImage("StandLeft_03.png");
    private GreenfootImage StandLeft8 = new GreenfootImage("StandLeft_02.png");
    private GreenfootImage StandLeft9 = new GreenfootImage("StandLeft_01.png");
    
    private GreenfootImage WalkA1 = new GreenfootImage("Walk-A_01.png");
    private GreenfootImage WalkA2 = new GreenfootImage("Walk-A_02.png");
    private GreenfootImage WalkA3 = new GreenfootImage("Walk-A_03.png");
    private GreenfootImage WalkA4 = new GreenfootImage("Walk-A_04.png");
    private GreenfootImage WalkA5 = new GreenfootImage("Walk-A_05.png");
    private GreenfootImage WalkA6 = new GreenfootImage("Walk-A_06.png");
    private GreenfootImage WalkA7 = new GreenfootImage("Walk-A_07.png");
    private GreenfootImage WalkA8 = new GreenfootImage("Walk-A_08.png");
    private GreenfootImage WalkA9 = new GreenfootImage("Walk-A_09.png");
    private GreenfootImage WalkA10 = new GreenfootImage("Walk-A_10.png");
    private GreenfootImage WalkA11 = new GreenfootImage("Walk-A_11.png");
    
    private GreenfootImage WalkLeftA1 = new GreenfootImage("WalkLeft-A_13.png");
    private GreenfootImage WalkLeftA2 = new GreenfootImage("WalkLeft-A_12.png");
    private GreenfootImage WalkLeftA3 = new GreenfootImage("WalkLeft-A_11.png");
    private GreenfootImage WalkLeftA4 = new GreenfootImage("WalkLeft-A_10.png");
    private GreenfootImage WalkLeftA5 = new GreenfootImage("WalkLeft-A_09.png");
    private GreenfootImage WalkLeftA6 = new GreenfootImage("WalkLeft-A_08.png");
    private GreenfootImage WalkLeftA7 = new GreenfootImage("WalkLeft-A_07.png");
    private GreenfootImage WalkLeftA8 = new GreenfootImage("WalkLeft-A_06.png");
    private GreenfootImage WalkLeftA9 = new GreenfootImage("WalkLeft-A_05.png");
    private GreenfootImage WalkLeftA10 = new GreenfootImage("WalkLeft-A_04.png");
    private GreenfootImage WalkLeftA11 = new GreenfootImage("WalkLeft-A_03.png");
    
    private int frame = 1;
    private int animationCounter = 0;
    private int Counter = 0;
    
    
    
    
    private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] rightAnim; = new GreenfootImage[11];
 
static
{
    for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
    for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
}

private GreenfootImage[] animSet = standAnim;

    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
        
        
        
        
        
        
        
        
        animationCounter ++;
        
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
            
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
            
        }
        
        
        
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
       
        move();
        Counter();
        canMoveRight();
        canMoveLeft();
        checkFall();
        ceiling();
        
    }
    
    
    // with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
 
// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}
    
 // with the following fields
private int animCount; // counter for animations
private int animDelay = 6; // initially set for standing animation (frame rate field)
 
// this method might help
private void setAnim(GreenfootImage[] anim, int frameRate)
{
    animSet = anim;
    animCount = -1; // prepares to set first image when incremented
    animDelay = frameRate;
    animate(); // calls a method to set a frame of this newly set animation
}   


    public void Counter()
    {
        
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
        
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
    
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=6;
        {
            WalkRight();
        }
        
        
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=6;
        {
            WalkLeft();
        }
        
        setLocation(x,y);
    }
    
    public void WalkRight()
    {
        if(Greenfoot.isKeyDown("right") )
        {
            if(animationCounter % 4 == 0){
                animateWalkA();
                
            }
            }
            else
            {
                if(animationCounter % 6 == 0 && Counter==0){
                 animateStand();
                 
                }
            }
    }
    
    public void WalkLeft()
    {
        if(Greenfoot.isKeyDown("left") )
        {
            if(animationCounter % 4 == 0){
                animateWalkLeftA();
                
            }
        }
        
            else
            {
                if(animationCounter % 6 == 0 && Counter==1){
                 animateStandLeft();
                 
                }
            }
    }
    
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int lookForGround = (int) (spriteHeight/2+30);
        
        Actor ground = getOneObjectAtOffset(0, lookForGround, Highway.class);
        
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
        
    }
    
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
        
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
        
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
            
            return false;
        }
        
    }
    
    
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
        
        
    }
    
    
   
    
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
       
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
       
       return canMoveLeft;
        
    }
    
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
       
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
       
        return canMoveRight;
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
        
        setLocation(getX(), newY);
        jumping = false;
        
    }
    
    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
            
        }
        else
        {
            fall();
        }

    }
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
        
        
       
    }
    
    
        
        
    
    
    
    
    public void animateStand()
    {
        if(frame == 1)
        {
            setImage(Stand1);
            
        }
        else if(frame == 2)
        {
            setImage(Stand2);
            
        }
        else if(frame == 3)
        {
            setImage(Stand3);
            
        }
        else if(frame == 4)
        {
            setImage(Stand4);
        }
        else if(frame == 5)
        {
            setImage(Stand5);
            
        }
        else if(frame == 6)
        {
            setImage(Stand6);
            
        }
        else if(frame == 7)
        {
            setImage(Stand7);
            
        }
        else if(frame == 8)
        {
            setImage(Stand8);
            
        }
        else if(frame == 9)
        {
            setImage(Stand9);
            frame = 1;
            return;
       }
       frame ++;
    }
    
    public void animateWalkA()
    {
        if(frame == 1)
        {
            setImage(WalkA1);
            
        }
        else if(frame == 2)
        {
            setImage(WalkA2);
            
        }
        else if(frame == 3)
        {
            setImage(WalkA3);
            
        }
        else if(frame == 4)
        {
            setImage(WalkA4);
        }
        else if(frame == 5)
        {
            setImage(WalkA5);
            
        }
        else if(frame == 6)
        {
            setImage(WalkA6);
            
        }
        else if(frame == 7)
        {
            setImage(WalkA7);
            
        }
         else if(frame == 8)
        {
            setImage(WalkA8);
            
        }
         else if(frame == 9)
        {
            setImage(WalkA9);
            
        }
         else if(frame == 10)
        {
            setImage(WalkA10);
            
        }
        else if(frame == 11)
        {
            setImage(WalkA11);
            frame = 3;
            return;
       }
       frame ++;
    }
    
    public void animateWalkLeftA()
    {
        if(frame == 1)
        {
            setImage(WalkLeftA1);
            
        }
        else if(frame == 2)
        {
            setImage(WalkLeftA2);
            
        }
        else if(frame == 3)
        {
            setImage(WalkLeftA3);
            
        }
        else if(frame == 4)
        {
            setImage(WalkLeftA4);
        }
        else if(frame == 5)
        {
            setImage(WalkLeftA5);
            
        }
        else if(frame == 6)
        {
            setImage(WalkLeftA6);
            
        }
        else if(frame == 7)
        {
            setImage(WalkLeftA7);
            
        }
         else if(frame == 8)
        {
            setImage(WalkLeftA8);
            
        }
         else if(frame == 9)
        {
            setImage(WalkLeftA9);
            
        }
         else if(frame == 10)
        {
            setImage(WalkLeftA10);
            
        }
        else if(frame == 11)
        {
            setImage(WalkLeftA11);
            frame = 3;
            return;
       }
       frame ++;
    }
    public void animateStandLeft()
    {
        if(frame == 1)
        {
            setImage(StandLeft1);
            
        }
        else if(frame == 2)
        {
            setImage(StandLeft2);
            
        }
        else if(frame == 3)
        {
            setImage(StandLeft3);
            
        }
        else if(frame == 4)
        {
            setImage(StandLeft4);
        }
        else if(frame == 5)
        {
            setImage(StandLeft5);
            
        }
        else if(frame == 6)
        {
            setImage(StandLeft6);
            
        }
        else if(frame == 7)
        {
            setImage(StandLeft7);
            
        }
        else if(frame == 8)
        {
            setImage(StandLeft8);
            
        }
        else if(frame == 9)
        {
            setImage(StandLeft9);
            frame = 1;
            return;
       }
       frame ++;
    }
}
 
Unlimited Unlimited

2018/12/27

#
Also ive added the walk left and stand left animations.
danpost danpost

2018/12/27

#
Unlimited wrote...
Im getting errors at: private static GreenfootImage rightAnim; = new GreenfootImage; But that is Probably because there is a ; to many.
Correct. The first one on that line should be removed.
However if i remove the ; after rightAnim im getting errors at all these lines: << Code Omitted >>
Not sure as to why that would be. Although, you are missing the animate method I provided which is called from the setAnim method.
danpost danpost

2018/12/27

#
Unlimited wrote...
Also ive added the walk left and stand left animations.
I see. However, you have not removed any of the lines that was to be replaced with the codes I provided, which was pretty much ALL your previously given animation codes. Your added animations should be done similar to what I provided, as well.
Unlimited Unlimited

2018/12/27

#
Im sorry i dont think i understand where to put all of this and what do with the errors. Im currently trying to add the gravity part you added and the animations but i dont know what parts need to be removed. I know this is asking for a lot but could you maybe give me the whole code of how you want it to be. As in to show me what needs to be where and what needs to be removed so i can use that as reference ?
danpost danpost

2018/12/27

#
Unlimited wrote...
Im sorry i dont think i understand where to put all of this and what do with the errors. Im currently trying to add the gravity part you added and the animations but i dont know what parts need to be removed.
Using the code that you originally gave (in your first posting), the parts being replaced (and that would need to be removed) are as follows: * lines 19 through 39 (replaced by static content) * line 42 (replaced by animCount) * lines 104 through 111 (to be replaced later) * lines 251 through 359 (replaced with animate method I provided, which you as yet have not added) The animate method will also need to be called from somewhere in the act method.
Unlimited Unlimited

2018/12/27

#
Ok thanks for that input, this is what i have now.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class is just an example. You can delete it or change the code.
 * It's not necessary for the scrolling system.
 */
public class Azure extends ScrollingActor
{
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = 20;
    
    
    
    
 
    
    
    private static GreenfootImage[] standAnim = new GreenfootImage[9];
    private static GreenfootImage[] rightAnim = new GreenfootImage[11];
 
    static
    {
        for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
        for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
        for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("WalkLeft-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
    }
    
    
    private int frame = 1;
    // with the following fields
    private int animCount; // counter for animations
    private int animDelay = 6; // initially set for standing animation (frame rate field)
 
    // this method might help
    private void setAnim(GreenfootImage[] anim, int frameRate)
    {
        animSet = anim;
        animCount = -1; // prepares to set first image when incremented
        animDelay = frameRate;
        animate(); // calls a method to set a frame of this newly set animation
    }
    private int Counter = 0;
    
    
    private GreenfootImage[] animSet = standAnim;
    
    
    
    
    /**
     * Here you can tell your actor what he has to do.
     */
    public void act() {
        
        
        
        
        
        
        
        
        
        
        if (Greenfoot.isKeyDown("space")) {
            setLocation(getX(), getY() - 20);
            
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() + 10);
            
        }
        
        
        
        if (Greenfoot.isKeyDown("left") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() - 15, getY());
        }
        if (Greenfoot.isKeyDown("right") && (Greenfoot.isKeyDown("s"))) {
            setLocation(getX() + 15, getY());
        }
        animate();
        move();
        Counter();
        canMoveRight();
        canMoveLeft();
        
        ceiling();
        
    }
    
    
   

    public void Counter()
    {
        
        if(Greenfoot.isKeyDown("right"))
        Counter = 0;
        
        if(Greenfoot.isKeyDown("left"))
        Counter = 1;
    }
    
    public void move()
    {
        int y = getY();
        int x = getX();
        if(Greenfoot.isKeyDown("right") && canMoveRight()) x+=6;
       
        
        
        if(Greenfoot.isKeyDown("left") && canMoveLeft()) x-=6;
       
        
        setLocation(x,y);
    }
    
    
    
    public void fall()
    {
        setLocation(getX(), getY() +vSpeed);
        
        {
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    
    public void onGround()
    {
         boolean onGround = false;
         // assume in air
       vSpeed += acceleration; // apply gravity
       setLocation(getX(), getY()+vSpeed); // fall
       Actor actor = getOneIntersectingObject(Actor.class);
       if (actor != null)
       { // colliding with another actor
           int vDir = (int)Math.signum(vSpeed); // vertical direction of movement
           vSpeed = 0; // kill vertical speed
           if (vDir > 0) onGround = true; // landing and not bopping head
           setLocation(getX(), actor.getY()-vDir*(actor.getImage().getHeight()+getImage().getHeight())/2+30); // assigning stop location
        }
        if (onGround && Greenfoot.isKeyDown("space")) vSpeed = -20; // initiate a jump
    }
        
        
        
        
        
    
    
    
    
    public boolean ceiling()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int) (spriteHeight/-2-30);
        
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Highway.class);
        
        if(ceiling != null)
        {
            vSpeed =1;
            hitHead(ceiling);
            return true;
        }
        else
        {
            
            return false;
        }
        
    }
    
    
    public void hitHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        
        setLocation(getX(), newY);
        
        
    }
    
    
   
    
    public boolean canMoveLeft()
    {
       boolean canMoveLeft = true;
       
       int imageWidth = getImage().getWidth();
       int imageHeight = getImage().getHeight();
       if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Border.class) !=null ||
       getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2, Border.class) !=null)
            canMoveLeft = false;
       
       return canMoveLeft;
        
    }
    
    public boolean canMoveRight()
    {
        boolean canMoveRight = true;
       
        int imageWidth = getImage().getWidth();
        int imageHeight = getImage().getHeight();
        if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Border.class) !=null ||
        getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2, Border.class) !=null)
            canMoveRight = false;
       
        return canMoveRight;
    }
    
    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
        
        setLocation(getX(), newY);
        jumping = false;
        
    }
    
    
    
    public void jump()
    {
        vSpeed = vSpeed - jumpStrength;
        jumping = true;
        fall();
        
        
       
    }
    
    
        
        
    
    
    
    
    private void animate()
    {
        animCount = (animCount+1)%(animSet.length*animDelay);
        if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
    }
}
 
There are more replies on the next page.
1
2
3
4