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

2018/4/14

Changing pictures when moving other directions.

Daanfaverey Daanfaverey

2018/4/14

#
Hello I'm making a sonic styled game. My sonic can move 4 different directions which are up, down, left and right. Using the arrows you can move sonic around. Now i only have a picture for sonic running from left to right. I want to have 4 different pictures, one picture for each direction. How to do this?
import greenfoot.*;

/**
 * Write a description of class sonic here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class sonic extends dier
{
    private int muntjeGegeten = 0;
    /**
     * Act - do whatever the sonic wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         if (Greenfoot.isKeyDown("left")) {
            setRotation(180);
            move(3);
            
        }
        if (Greenfoot.isKeyDown("up")) {
            setRotation(270);
            move(3);
        }
        if (Greenfoot.isKeyDown("right")) {
            setRotation(0);
            move(3);
        }
        if (Greenfoot.isKeyDown("down")) {
            setRotation(90);
            move(3);
        }        
             tryToEat();
        if (Greenfoot.isKeyDown("space")) {
            World world = getWorld();
            Lazer Lazer = new Lazer();
            Lazer.setsonic(this);
            world.addObject(Lazer, getX(), getY());
        }
     }
     public void tryToEat()
     {
         if (canSee(muntje.class) )
         {
             eat(muntje.class);
             muntjeGegeten++;
             Greenfoot.playSound ("Ooh he stealing!.mp3");
             World1 world = (World1)getWorld(); //casting
             world.updateScore();
            
         }
        
    }    
    public void checkVijand1()
    {
        if (canSee(Vijand1.class) )
        {
             Greenfoot.playSound("game-over.wav");
             Greenfoot.stop();
        }
    } 
}
danpost danpost

2018/4/14

#
Daanfaverey wrote...
Hello I'm making a sonic styled game. My sonic can move 4 different directions which are up, down, left and right. Using the arrows you can move sonic around. Now i only have a picture for sonic running from left to right. I want to have 4 different pictures, one picture for each direction. How to do this? << Code Omitted >>
You can mirror the image you have to get the left running sonic; however, you will still be out of luck with the up and down images (unless you do not mind rotating the image you have (causing left to be upside down).
You need to login to post a reply.