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

2015/12/30

moving a character between 2-points

davemib123 davemib123

2015/12/30

#
I'm trying to get a character to move between two points only (a bit like a victory pose), I have this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Toad here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Toad extends ScrollingActor
{
    protected static final GreenfootImage Toad = new GreenfootImage("toad.gif");
    
    public Toad()
    {
        shared.speed = 1;
        shared.animationDelay = 50;

        setImage(Toad);

        Toad.scale(23,35);
    }

    /**
     * Act - do whatever the Toad wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveUpDown();
    }    
    
    private void moveUpDown()
    {
        shared.frame ++;  
        if(shared.frame < 1 *  shared.animationDelay)  
        {  
            shared.moveUp(this);
        }  
        else if(shared.frame < 2 *  shared.animationDelay)
        {
            shared.moveDown(this);
        }
        if(shared.frame > 2 *  shared.animationDelay)
        {
            shared.frame = 0;
        }
    }
}
But when if it continues running the character slowly moves down further then it should. Any ideas?
danpost danpost

2015/12/30

#
Okay. I think what you were trying to say is that each time it completes an up/down cycle, it ends up a little lower than before. This may be because it moves up 49 times and then moves down 50 times each cycle. I think if you change the first two comparison operators to '<=' and change the last one to '==' that is should fix the issue.
davemib123 davemib123

2015/12/30

#
thanks that works :)
You need to login to post a reply.