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

2019/3/10

Need a little help with turning my image

JoRt JoRt

2019/3/10

#
I'm making a little zombie game. I made an animation for the zombies, but they're most of the time upside down...I don't really know how to fix it. The code below is the code of my zombie (it is called "Target").
import greenfoot.*;
import java.awt.Color;
import java.lang.Object;

/**
 * Write a description of class Target here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Target extends Actor
{
    public int Health = 999;
    Poppetje1 poppetje1;
    public int frame = 0;
    int aantalZombiesGedood = 0;
    /**
     * Act - do whatever the Target wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        gebruikHealth();
        setLocation();
        updateStatusBar();
        animatie();
    }    

    public void addHealth (int amountOfHealth)
    {
        Health += amountOfHealth-350;
        if (Health < 999) {
            gebruikHealth();
        }
        updateStatusBar();
    }

    private void gebruikHealth(){
        updateStatusBar();
        if (Health <= 0) {
            getWorld().removeObject(this);
        }
    }

    private void updateStatusBar()
    {
        if( frame == 0){
            GreenfootImage newImage = new GreenfootImage("ZombieWalk1.png");
            newImage.setColor (Color.RED);   
            newImage.fillRect(39-(Health/60 + 10), 3, Health/60 + 1,  5);
            setImage (newImage);
        }
        if( frame == 15){
            GreenfootImage newImage = new GreenfootImage("ZombieWalk2.png");
            newImage.setColor (Color.RED);   
            newImage.fillRect(39-(Health/60 + 10), 3, Health/60 + 1,  5);
            setImage (newImage);
        }
        if( frame == 30){
            GreenfootImage newImage = new GreenfootImage("ZombieWalk3.png");
            newImage.setColor (Color.RED);   
            newImage.fillRect(39-(Health/60 + 10), 3, Health/60 + 1,  5);
            setImage (newImage);
        }
        if( frame == 45){
            GreenfootImage newImage = new GreenfootImage("ZombieWalk4.png");
            newImage.setColor (Color.RED);   
            newImage.fillRect(39-(Health/60 + 10), 3, Health/60 + 1,  5);
            setImage (newImage);
        }
        if( frame == 60){
            GreenfootImage newImage = new GreenfootImage("ZombieWalk5.png");
            newImage.setColor (Color.RED);   
            newImage.fillRect(39-(Health/60 + 10), 3, Health/60 + 1,  5);
            setImage (newImage);
            frame = 0;
        }
    }

    public void setLocation()
    {
        turnTowards(Poppetje1.Poppetje1X, Poppetje1.Poppetje1Y);
        move(1);
    }

    public void animatie()
    {
        frame++;
    }
}

danpost danpost

2019/3/11

#
JoRt wrote...
I'm making a little zombie game. I made an animation for the zombies, but they're most of the time upside down...I don't really know how to fix it. << Code Omitted >>
Insert at line 84, the following line:
setRotation(0);
JoRt JoRt

2019/3/11

#
That is not exactly what I mean. It kinda solved the problem, but on the other side, it doesn't. With the setRotation(0), if my zombies are on the right side of my player, the animation of my zombies won't turn to the player anymore. They still walk towards the player but the animation keeps staring to the right side of the screen (whitout rotation). I want to let the zombies walk toward the player WITH the animation turned towards the player. It is hard to explain... Personally I think I need to use the "mirrorVertically" method, but I'm not completely sure. If I use this method, my zombies keep turnig vertically every second.
danpost danpost

2019/3/11

#
JoRt wrote...
I want to let the zombies walk toward the player WITH the animation turned towards the player. It is hard to explain... Personally I think I need to use the "mirrorVertically" method, but I'm not completely sure. If I use this method, my zombies keep turnig vertically every second.
You will need two images for the actor. It would be best to set up fields for these images. For example:
private Greenfootimage imgR, imgL;
Use a constructor block to assign the field values:
public Target()
{
    imgR = getImage();
    imgL = new GreenfootImage(imgR);
    imgL.mirrorHorizontally();
}
Now, instead of the 'setRotation(0);' line, use:
setImage((getRotation()+90)%180 == 0 ? imgR : imgL);
JoRt JoRt

2019/3/11

#
danpost wrote...
JoRt wrote...
I want to let the zombies walk toward the player WITH the animation turned towards the player. It is hard to explain... Personally I think I need to use the "mirrorVertically" method, but I'm not completely sure. If I use this method, my zombies keep turnig vertically every second.
You will need two images for the actor. It would be best to set up fields for these images. For example:
private Greenfootimage imgR, imgL;
Use a constructor block to assign the field values:
public Target()
{
    imgR = getImage();
    imgL = new GreenfootImage(imgR);
    imgL.mirrorHorizontally();
}
Now, instead of the 'setRotation(0);' line, use:
setImage((getRotation()+90)%180 == 0 ? imgR : imgL);
It doesn't work perfectly, but fine for me :)... I do have another question. I made a subworld in my (main) world, but every time I get in the second world, my character respawns at the middle of the world. Is there an possibility for keeping his position if he gets in the second world?
danpost danpost

2019/3/11

#
JoRt wrote...
I made a subworld in my (main) world, but every time I get in the second world, my character respawns at the middle of the world. Is there an possibility for keeping his position if he gets in the second world?
First, I would have to question whether your subworld should extend your main world or the World class itself. I would then need to see your codes around which you create a subworld and set it active.
Super_Hippo Super_Hippo

2019/3/11

#
You could pass a reference to the Actor to the next world. Or you pass a reference of the old world to the next world (not recommended for only the actor). Or you could save the actor in a static field in one world and you can access it from everywhere.
danpost danpost

2019/3/11

#
Or, you could just set the location of the actor in the new world from the old world between creating that new world and setting it active.
JoRt JoRt

2019/3/11

#
danpost wrote...
JoRt wrote...
I made a subworld in my (main) world, but every time I get in the second world, my character respawns at the middle of the world. Is there an possibility for keeping his position if he gets in the second world?
First, I would have to question whether your subworld should extend your main world or the World class itself. I would then need to see your codes around which you create a subworld and set it active.
My second world extends my main world. this is my main world:
import greenfoot.*;

/**
 * Write a description of class BoardWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BoardWorld extends World
{
    int Tijd = 0;
    int aantalZombiesSpawn = 0;
    int aantalZombiesInWereld = 0;
    int tijdTotVolgendeRonde = 0;
    /**
     * Constructor for objects of class BoardWorld.
     * 
     */
    public BoardWorld()
    {    
        super(800, 550, 1, false);
        addObject (new Poppetje1(), 400, 275);
        addObject (new Target(), 700, 600);
    }

    public void act(){        
        aantalZombiesInWereld();
        spawn();
        tijd(); 
        aantalZombiesSpawn();
        volgendeRonde();
    }    

    public void tijd(){
        Tijd++;        
    }

    public void aantalZombiesSpawn(){
        if (Tijd == 50){
            aantalZombiesSpawn++;
        }
        if (Tijd == 100){
            aantalZombiesSpawn++;
        }
        if (Tijd == 150){
            aantalZombiesSpawn++;
        }
        if (Tijd == 200){
            aantalZombiesSpawn++;
        }
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */   
    public void spawn()
    {
        if(Tijd == 50)
        {            
            addObject(new Target(), (Greenfoot.getRandomNumber(800)), 0);
        } 
        if(Tijd == 100)        
        {            
            addObject(new Target(), 0, (Greenfoot.getRandomNumber(550)));
        }
        if(Tijd == 150)
        {            
            addObject(new Target(), 800, (Greenfoot.getRandomNumber(550)));
        }
        if(Tijd == 200)
        {            
            addObject(new Target(), (Greenfoot.getRandomNumber(800)), 550);
            Tijd = 0;
        }
        if (aantalZombiesSpawn >= 9){
            Tijd = 201;
        }
        if (aantalZombiesInWereld > 5){
            Tijd = 51;
        }
    }

    public void aantalZombiesInWereld()
    {
        aantalZombiesInWereld = getObjects(Target.class).size();
    }

    public void volgendeRonde()
    {
        if (aantalZombiesInWereld == 0){      
            tijdTotVolgendeRonde++;
            if(tijdTotVolgendeRonde == 200){
                Greenfoot.setWorld(new World2());
            }
        }
    }
}
I tried using the "getX()" and "getY()" in multiple ways to get the coördinates, but nothing works...apparently I'm doing something wrong. thanks for helping me out :)
danpost danpost

2019/3/11

#
You need to split line 94 up into individual lines so that you can make a reference variable to the new world:
World w2 = new World2();
Greenfoot.setWorld(w2);
Now you can add to this to add the actor where it is in "this" world:
Actor po = (Actor) getObjects(Poppetje1.class).get(0);
w2.addObject(po, po.getX(), po.getY());
It seems to me like all you really needed to do, instead of creating a whole new world, was to use a field to indicate the level and do things based on its value.
You need to login to post a reply.