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

2014/4/10

Change Images permanently causes massive lag

Midas97 Midas97

2014/4/10

#
Hi, I want to replace the player image with 2 other images which alternate with each other every 1 sec, when the players lifepoints reachs a quarter of it. Everytime this is true, the game stops itself (moving objects are freezed) and the run/stop button appears at the top left corner. Only the 2 images are flashing. I've included all the methods that belong into this class in the act-method, so they can be played.
    public void changeStatus(){
        while(HP<=HPMax/4){ 
            setImage("Player_2.png");
            Greenfoot.delay(25);
            setImage("Player_3.png");
            Greenfoot.delay(25);
        }
    }
bourne bourne

2014/4/10

#
You are stopping the program from running,
Greenfoot API wrote...
delay(int time) Delay the current execution by a number of time steps.
elias.groll elias.groll

2014/4/10

#
"setImage()" requires a GreenfootImage object so u maybe should setImage(new GreenfootImage("whaterver.png")) :)
Midas97 Midas97

2014/4/10

#
Ok, how I can avoid this stopping process? The Images must be delayed to see clearly that they're switching.
bourne bourne

2014/4/10

#
@elias.groll, actually there seems to be a setImage() method that takes a filename String. (I have only just recently noticed this method as well)
Greenfoot API wrote...
setImage(java.lang.String filename) Set an image for this actor from an image file.
bourne bourne

2014/4/10

#
@Midas97, you need to switch between images concerning time which can be checked within the act method. Something like so:
private boolean firstImage;
private long lastSwitch;

@Override
public void act() {
    if (HP<=HPMax/4) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastSwitch >= 1000) // For 1 second swapping
        {
            setImage(firstImage ? "Image1.png" : "Image2.png");
            firstImage = !firstImage;
            lastSwitch = currentTime;
        }
    }
}
elias.groll elias.groll

2014/4/11

#
In my projects i normally animate like this:
int frame = 1;


 
   GreenfootImage[] flapAnimation = new GreenfootImage[]
        {
            new GreenfootImage("Fly1.png"),new GreenfootImage("Fly2.png"),
            new GreenfootImage("Fly3.png"),new GreenfootImage("Fly4.png"),
            new GreenfootImage("Fly5.png"),new GreenfootImage("Fly4.png"),
            new GreenfootImage("Fly3.png"),new GreenfootImage("Fly2.png"),
            new GreenfootImage("Fly1.png"),new GreenfootImage("Fly6.png"),
            new GreenfootImage("Fly7.png"),new GreenfootImage("Fly8.png"),
            new GreenfootImage("Fly9.png"),new GreenfootImage("Fly10.png"),
            new GreenfootImage("Fly9.png"),new GreenfootImage("Fly8.png"),
            new GreenfootImage("Fly7.png"),new GreenfootImage("Fly6.png")
        };


  public GreenfootImage animation(GreenfootImage[] animation){
        if (frame == animation.length - 1){
            frame = -1;
        }
        frame++;
        return animation[frame];
    }
Midas97 Midas97

2014/4/11

#
Thanks. It worked! Now my game is almost complete but the HPBar doesnt work when the player looses or gets HP: Player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Player extends Actor
{
    public int HPMax;
    public int HP;
    private boolean OnLaser;  
    private long lastSwitch;  
    public Player(){
        HPMax = 100;
        HP = HPMax;
    }

    public void act() 
    {
        moveOnCommand();
        changeStatus();
        hitLaser();
    }    

    public void moveOnCommand(){
        if(Greenfoot.isKeyDown("up")||Greenfoot.isKeyDown("W")){
            this.setLocation(getX(), getY() - 5);
        }
        if(Greenfoot.isKeyDown("down")||Greenfoot.isKeyDown("S")){
            this.setLocation(getX(), getY() + 5);
        }
        if(Greenfoot.isKeyDown("left")||Greenfoot.isKeyDown("A")){
            this.setLocation(getX() - 5, getY());
        }
        if(Greenfoot.isKeyDown("right")||Greenfoot.isKeyDown("D")){
            this.setLocation(getX() + 5, getY());
        }
    }

    public void changeStatus(){   
        if (HP<=HPMax/4){  
            long currentTime = System.currentTimeMillis();  
            if (currentTime-lastSwitch>=1000) // For 1 second swapping  
            {  
                setImage(OnLaser ? "Player_1.png" : "Player_2.png");  
                OnLaser = !OnLaser;  
                lastSwitch = currentTime;  
            }  
        }    
    }

    public void hitLaser(){
        World myWorld = getWorld();
        Laserfield lf = (Laserfield)myWorld;
        HPCount hpc = lf.getHPCount();
        Actor Laser = getOneIntersectingObject(Laser.class);
        Actor Laser2 = getOneIntersectingObject(Laser2.class);
        if (Laser!=null){
            hpc.looseHealth();
            HP--;
            setImage("Player_2.png");
            if(hpc.HPBar==0){
                getWorld().removeObject(this);
            }
            Greenfoot.delay(1);
        }
        if(HP!=HPMax){
            if (Laser2 != null){                  
                hpc.winHealth();
                HP++;
                setImage("Player_3.png");
                Greenfoot.delay(1);
            }
        }
        setImage("Player_1.png");
    }
    
    public int HP(){
        return HP;
    }
}
HPCount:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
public class HPCount extends Actor
{
    public int HPBar = 100;
    /**
     * Act - do whatever the HPCount wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage(102,18));
        GreenfootImage myImage = getImage();
        myImage.setColor(Color.BLACK);
        myImage.drawRect(0, 0, 101, 17);
        myImage.setColor(Color.GREEN);
        int rectWidth = HPBar;
        myImage.fillRect(1, 1, rectWidth, 16);
    }    

    public void looseHealth()
    {
        HPBar--;
    }
    
    public void winHealth()
    {
        HPBar++;
    }
}
World:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Laserfield extends World
{
    HPCount value = new HPCount();
    GreenfootSound musicloop = new GreenfootSound("Xenith800SunGod.mp3");
    private boolean music;
    /**
     * Constructor for objects of class Laserfield.
     * 
     */
    public Laserfield()
    {    
        super(640, 448, 1); 
        setBackground("Ground_LaserField.png");
        setPaintOrder(ScoreCount.class,Player.class,Laser.class,PickUps.class);
        musicOn();
        addingObjects();
        Greenfoot.setSpeed(50);
        music=false;
    }

    public void act()
    {
        ScoreCount.add(1);
        if(Greenfoot.getRandomNumber(100) < 25){ //100=MaxRandom; < 25=Wenn von MaxRandom 0-24 ist 
            addObject(new Laser(), Greenfoot.getRandomNumber(getWidth()), 640);   //getWidth()=MaxRandom, dessen zufälliger Wert die X-Koordinate repräsentiert; 640=Y-Koordinate
        }
        if(Greenfoot.getRandomNumber(100) < 2){ //100=MaxRandom; < 2=Wenn von MaxRandom 0-1 ist 
            addObject(new Laser2(), Greenfoot.getRandomNumber(getWidth()), 640);   //getWidth()=MaxRandom, dessen zufälliger Wert die X-Koordinate repräsentiert; 640=Y-Koordinate
        }
    }

    public void addingObjects(){
        addObject(new Player(),getWidth()/2,getHeight()/2);
        addObject(new HPCount(),53,11);
        addObject(new ScoreCount("Score: "),550,20);
    }

    public void musicOn(){
        if(Greenfoot.isKeyDown("F1")){
            music=true;
        }
        if(music==true){
            musicloop.playLoop();
        }
    }

    public HPCount getHPCount(){
        return value;
    }
}
You need to login to post a reply.