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

2013/10/20

setLocation problem

ddvink ddvink

2013/10/20

#
Here I have a class that should play an animation.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Char here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LevelUp extends Actor
{
    //Verticale snelheid en acceleratie
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = -15;
    //Loop variabelen
    private int speed = 4;
    //Frame float
    private float frame = 0;
    private long lastAdded = System.currentTimeMillis(); 
    // init sequence
    private int sequence = 0;
    //richting
    public int richting = 2;

    //CHAR Eigenschappen
    private int mesos = 0;
    public int level = 1;

    private int newYvalue;

    private GreenfootImage levelUp[] = {
            new GreenfootImage("LevelUp_0.png"),
            new GreenfootImage("LevelUp_1.png"),
            new GreenfootImage("LevelUp_2.png"),
            new GreenfootImage("LevelUp_3.png"),
            new GreenfootImage("LevelUp_4.png"),
            new GreenfootImage("LevelUp_5.png"),
            new GreenfootImage("LevelUp_6.png"),
            new GreenfootImage("LevelUp_7.png"),
            new GreenfootImage("LevelUp_8.png"),
            new GreenfootImage("LevelUp_9.png"),
            new GreenfootImage("LevelUp_10.png"),
            new GreenfootImage("LevelUp_11.png"),
            new GreenfootImage("LevelUp_12.png"),
            new GreenfootImage("LevelUp_13.png"),
            new GreenfootImage("LevelUp_14.png"),
            new GreenfootImage("LevelUp_15.png"),
            new GreenfootImage("LevelUp_16.png"),
            new GreenfootImage("LevelUp_17.png"),
            new GreenfootImage("LevelUp_18.png"),
            new GreenfootImage("LevelUp_19.png"),
            new GreenfootImage("LevelUp_20.png")
        };

    // LevelUp constructor
    public LevelUp(){
        animateLevelUp();
      
    }

    public LevelUp(int xValue, int yValue){
        animateLevelUp();
    }

    public void act(){
        animateLevelUp();
        timer();

    }

    public void timer()
    {  
        long curTime  = System.currentTimeMillis();  
        if (curTime >= lastAdded + 150) //5000ms = 5s  
        {  
            frame++;
            lastAdded  = curTime;  
        }  
    }  

    public void animateLevelUp()
    {
        //System.out.println("animateLevel up is aangeroepen.....");

        if(frame == 1)
        {
            setImage( levelUp [0]);
            
        }
        if(frame == 2)
        {
            setImage( levelUp [1]);

        }
        if(frame == 3)
        {
            setImage( levelUp [2]);

        }
        if(frame == 4)
        {
            setImage( levelUp [3]);

        }
        if(frame == 5)
        {
            setImage( levelUp [4]);

        }
        if(frame == 6)
        {
            setImage( levelUp [5]);

        }
        if(frame == 7)
        {
            setImage( levelUp [6]);

        }
        if(frame == 8)
        {
            setImage( levelUp [7]);

        }
        if(frame == 9)
        {
            setImage( levelUp [8]);

        }
        if(frame == 10)
        {
            setImage( levelUp [9]);

        }
        if(frame == 11)
        {
            setImage( levelUp [10]);          

        }
        if(frame == 12)
        {
            setImage( levelUp [11]);
            System.out.println(getY()-(getImage().getHeight()/2));
            System.out.println(getImage().getHeight()/2);
            System.out.println(getY());
        }
        if(frame == 13)
        {
            setImage( levelUp [12]);

        }
        if(frame == 14)
        {
            setImage( levelUp [13]);

        }
        if(frame == 15)
        {
            setImage( levelUp [14]);

        }
        if(frame == 16)
        {
            setImage( levelUp [15]);

        }
        if(frame == 17)
        {
            setImage( levelUp [16]);

        }
        if(frame == 18)
        {
            setImage( levelUp [17]);

        }
        if(frame == 19)
        {
            setImage( levelUp [18]);
        }
        if(frame == 20)
        {
            setImage( levelUp [19]);
        }
        if(frame == 21)
        {
            setImage( levelUp [20]);
            frame = 0;
            sequence = 0;
            getWorld().removeObject(this);
            return;
        } 
    }

    //     public void levelUp()
    //     {
    //         level ++;
    //         Greenfoot.playSound("LevelUp.wav");
    //     } 
} 
It's playing the animation well. The animation is following the char, where it has to play, as well. The only thing is that I want it to play half his height higher. If I use the setLocation method, like this --> setLocation (getX(), getY()); nothing strange is happening. If (for every image) I do --> setLocation (getX(), getY()-getImage().getHeight()/2); the animation constantly plays somewhere above in the screen. Some help appreciated. Yours, Dennis
danpost danpost

2013/10/20

#
Using 'setLocation(getX(), getY()-getImage().getHeight()/2' every act cycle will indeed make your character go up the screen half its height every act cycle. You need to get a reference to that actor it is following (I will call it 'followed') and use 'followed.getX()' and 'followed.getY()-getImage().getHeight()/2'.
You need to login to post a reply.