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

2014/9/20

Animated Charecter

genfy genfy

2014/9/20

#
So this is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private static GreenfootImage walk1 = new GreenfootImage("Walk1.png");
    private static GreenfootImage walk2 = new GreenfootImage("Walk2.png");
    private static GreenfootImage walk3 = new GreenfootImage("Walk3.png");
    private static GreenfootImage walk4 = new GreenfootImage("Walk4.png");
    private static GreenfootImage walk5 = new GreenfootImage("Walk5.png");
    private static GreenfootImage walk6 = new GreenfootImage("Walk6.png");
    private static GreenfootImage walk7 = new GreenfootImage("Walk7.png");
    private static GreenfootImage walk8 = new GreenfootImage("Walk8.png");
    private static GreenfootImage walk9 = new GreenfootImage("Walk9.png");
    private static int frame = 1;
    
    private static int timer = 1;
    public void act() 
    {
        move();
    }    
    
    public void move(){
        if(Greenfoot.isKeyDown("d")){
            timer = timer - 1;
            frame++;
            if(timer == 0){
                
                switch(frame){
                    case 1: this.setImage(walk1);
                            break;
                    case 2: this.setImage(walk2);
                            break;
                    case 3: this.setImage(walk3);
                            break;
                    case 4: this.setImage(walk4);
                            break;
                    case 5: this.setImage(walk5);
                            break;
                    case 6: this.setImage(walk6);
                            break;
                    case 7: this.setImage(walk7);
                            break;
                    case 8: this.setImage(walk8);
                            break;
                    case 9: this.setImage(walk9);
                            break;
                    case 10: this.setImage(walk1);
                             frame = 2;
                             break;
                }
                timer = 2;
            }
        }
    }
    
}
This code is for animating a character, but whenever I set my timer To anything other than 1 or 2, weird stuff begins to happen, such as the animation only working a couple of frames or not animating at all. Please help.
danpost danpost

2014/9/20

#
The main problem is you are using two fields, timer and frame, the frame field is incremented every act and the timer passes control to the switch statement every other act; this will cause every other frame to show during one cycle through the images. I would suggest eliminating one of the fields -- the frame field, maybe, and increment the timer using the remainder (mod) operator to create the cycle and to determine when to change images. Something like this:
timer = (timer+1)%18 /* imagesCount * 2 */;
if (timer%2 /* every other act */ == 0) {
    switch (timer/2) {
        // etc.
There are three occurrences of '2' in the code above. All these refer to the total number of frames each image is to be displayed. NOTE: the cases will be from zero to eight -- not from 1 to 10.
genfy genfy

2014/9/20

#
So, going by your advice I'm going to have the switch be based around timer instead of frame, and have 60 be frame 1, 40 be frame 2 and etc. But thanks!
danpost danpost

2014/9/20

#
genfy wrote...
So, going by your advice I'm going to have the switch be based around timer instead of frame, and have 60 be frame 1, 40 be frame 2 and etc. But thanks!
I do not know from where you are getting values of 60, 40, etc. :?
You need to login to post a reply.