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

2017/10/25

Trouble Idling.

Beamo Beamo

2017/10/25

#
Hi! I'm trying to create an idle animation based on if the Hero class is facing a certain direction, and not moving. However, when idling, it would flicker between idle left and idle right. I can't seem to identify the problem so any help would be appreciated! Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Hero here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Hero extends Actor
 
{
    int health=5,lives=3;
    private final int GRAVITY = 1;
    private int velocity;
    public boolean lookingLeft;
    public boolean lookingRight;
    private int imageCounter;
    int span = 4;
    int imageTimer=0;
    int imageIdleTimer=0;
    int imageIdleTwoTimer=0;
    GreenfootImage runningOne = new GreenfootImage("running1.png");
    GreenfootImage runningTwo = new GreenfootImage("running2.png");
    GreenfootImage runningThree = new GreenfootImage("running3.png");
    GreenfootImage runningOneR = new GreenfootImage("running1r.png");
    GreenfootImage runningTwoR = new GreenfootImage("running2r.png");
    GreenfootImage runningThreeR = new GreenfootImage("running3r.png");
    /**
     * Act - do whatever the Hero wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Hero(){
        //setting Images
            //idle1
        GreenfootImage idleOneRight = new GreenfootImage("Idle1.png");
        GreenfootImage idleOneLeft = new GreenfootImage("Idle1.png");
        idleOneRight.mirrorHorizontally();
            //idle2
        GreenfootImage idleTwoRight = new GreenfootImage("Idle2.png");
        GreenfootImage idleTwoLeft = new GreenfootImage("Idle2.png");
        idleTwoRight.mirrorHorizontally();
            //shoot1
        GreenfootImage shootOneRight = new GreenfootImage("shoot1.png");
        GreenfootImage shootOneLeft = new GreenfootImage("shoot1.png");
        shootOneRight.mirrorHorizontally();
            //shoot2
        GreenfootImage shootTwoRight = new GreenfootImage("shoot2.png");
        GreenfootImage shootTwoLeft = new GreenfootImage("shoot2.png");
        shootTwoRight.mirrorHorizontally();
        //setting velocity
        velocity = 0;
        //setting Hero image
        setImage(idleOneLeft);
        getImage().scale(getImage().getWidth() + 75, getImage().getHeight() + 75);
        lookingLeft=true;
    }
    public void act()
    {
        fall();
        move();
    }   
    public void move(){
       if(Greenfoot.isKeyDown("left")){
            lookingLeft = true;
            setLocation(getX()-4,getY());
            //while(Greenfoot.isKeyDown("left")){leftWalk();}
            leftWalk();
       }
       if(Greenfoot.isKeyDown("right")){
            lookingRight = true;
            setLocation(getX()+4,getY());
            rightWalk();
        }
       if(Greenfoot.isKeyDown("left")==false&&Greenfoot.isKeyDown("up")==false&&Greenfoot.isKeyDown("right")==false&&checkOnFloor()==true&&lookingLeft==true){
            idleLeft();
        }
       if(Greenfoot.isKeyDown("left")==false&&Greenfoot.isKeyDown("up")==false&&Greenfoot.isKeyDown("right")==false&&checkOnFloor()==true&&lookingRight==true){
            idleRight();
        }
    }
    public boolean canMove(){
        boolean check;
        double spaceX = Math.floor(getImage().getHeight()/2)+1;
        double maxX = Math.floor(getWorld().getWidth())-spaceX;
        double minX = 0+spaceX;   
        int x = getX();
        int y = getY();
        if(x>minX){
            check=true;
        }
        else{check=false;}
        if(x<maxX){
            check=true;
        }
        else{check=false;}
        return check;
    }
    public void fall(){
        setLocation(getX(),getY()+velocity);
        if(checkOnFloor()==true){
            velocity=0;
            while(checkOnFloor()==true){
                setLocation(getX(),getY()-1);
            }
            setLocation(getX(),getY()+1);
        }
        else{velocity+=GRAVITY;}
    }
    public void jump(){
        velocity = -15;
    }
    public boolean checkOnFloor(){
        boolean floorcheck;
        if(getY()>getWorld().getHeight()-55){
            floorcheck=true;
        }
        else{floorcheck=false;}
        return floorcheck;
    }
     public void leftWalk(){
        imageTimer++;
        if(imageTimer==span){setImage(runningOne);getImage().scale(75,106);}
        if(imageTimer==span*2){setImage(runningTwo);getImage().scale(75,97);}
        if(imageTimer==span*3){setImage(runningThree); getImage().scale(75,106);imageTimer=0;}
   }
   public void rightWalk(){
       imageTimer++;
        if(imageTimer==span){setImage(runningOneR);getImage().scale(75,106);}
        if(imageTimer==span*2){setImage(runningTwoR);getImage().scale(75,97);}
        if(imageTimer==span*3){setImage(runningThreeR); getImage().scale(75,106);imageTimer=0;}
   }
   public void idleLeft(){
        GreenfootImage idleOneRight = new GreenfootImage("Idle1.png");
        GreenfootImage idleOneLeft = new GreenfootImage("Idle1.png");
        idleOneRight.mirrorHorizontally();
        //idle2
        GreenfootImage idleTwoRight = new GreenfootImage("Idle2.png");
        GreenfootImage idleTwoLeft = new GreenfootImage("Idle2.png");
        idleTwoRight.mirrorHorizontally();
       imageIdleTimer++;
        if(imageIdleTimer==span){setImage(idleOneLeft);getImage().scale(92,106);}
        if(imageIdleTimer==span*3){setImage(idleTwoLeft);getImage().scale(92,106);imageIdleTimer=0;}
   }
   public void idleRight(){
        GreenfootImage idleOneRight = new GreenfootImage("Idle1.png");
        idleOneRight.mirrorHorizontally();
        //idle2
        GreenfootImage idleTwoRight = new GreenfootImage("Idle2.png");
        idleTwoRight.mirrorHorizontally();
       imageIdleTwoTimer++;
       if(imageIdleTwoTimer==span){setImage(idleOneRight);getImage().scale(92,106);}
       if(imageIdleTwoTimer==span*5){setImage(idleTwoRight);getImage().scale(92,106);imageIdleTwoTimer=0;}
   }
}
Sorry for the messy set up. Thanks again!
danpost danpost

2017/10/25

#
I do not see any place where 'lookingLeft' or 'lookingRight' are being set back to 'false' -- and are not these two variables opposites of each other in that one variable would be sufficient? (hint) Why did you not create fields for the idle images like you did with the running ones? It would save a lot of CPU expense during run-time.
Beamo Beamo

2017/10/25

#
Oh! Thanks for the hint I got it now! In regards to creating fields for the idle images, that's a great idea thanks! Thanks for replying danpost!
You need to login to post a reply.