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

2013/5/24

rotate image (switch)

1
2
Nike.Sprite Nike.Sprite

2013/5/24

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pacman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pacman extends Actor
{
    int orientatie;

    private GreenfootImage pacmanopen = new GreenfootImage("pacmanopen.png");
    private GreenfootImage pacmantoe = new GreenfootImage("pacmantoe.png");

    public Pacman(String naam)
    {
        setImage(naam+".png");
    }

    public void act()
    {
        changeImage();
        checkFood();
        sides();

        if (canMove() == false)
        {
            move(1);
        }
    }

    public void changeImage()
    {
        if (getImage() == pacmanopen)
        {
            setImage(pacmantoe);
        }
        else
        {
            setImage(pacmanopen);
        }
    }

    public void getOrientatie(int degrees) // werkt nog niet
    {
        switch(orientatie) 
        {
            case 0:
            setRotation(getRotation());
            break;

            case 1:
            setRotation(getRotation()+90);
            break;

            case 2:
            setRotation(getRotation()+180);
            break;

            case 3:
            setRotation(getRotation()-90);
            break;
        }
    }

    public boolean canMove()
    {
        Actor wall = getOneIntersectingObject(Wall.class);
        if (wall == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }    

    public void checkFood()
    {
        Actor food = getOneIntersectingObject(Food.class);
        {
            getWorld().removeObject(food);
        }
    }

    public void Move()
    {
        if (Greenfoot.isKeyDown("down"))
        {
        getOrientatie() = 0; 
        }
    }

    public void sides()
    {
        if (getX()==0 && getY()==9)
        {
            setLocation(23,9);
        }
        
        if (getX()==24 && getY()==9)
        {
        setLocation(1,9);
        }
    }
}
I want to give an orientation to my pacman Actor (this is the Pacman class) How do I turn the image in the switch method (see case 0,1,2,3) ? Then I need to save this orientation to give it as a return to the method Move (see above) Hopefully someone can help me with this problem thanks in advance Nike Sprite ;)
danpost danpost

2013/5/25

#
Remove 'getRotation()+' or '-' from each and every case.
Nike.Sprite Nike.Sprite

2013/5/25

#
    public void getOrientatie(int degrees) // werkt nog niet
    {
        switch(orientatie) 
        {
            case 0:
            setRotation();
            break;

            case 1:
            setRotation(90);
            break;

            case 2:
            setRotation(180);
            break;

            case 3:
            setRotation(270);
            break;
        }
    }
thanks for your quick answer danpost ! now he says method setRotation in class greenfoot.Actor cannot be applied to given types
danpost danpost

2013/5/25

#
You are missing an argument in the first call to 'setRotation'. Place a zero inside the parenthesis of the call in the first case (case 0).
Nike.Sprite Nike.Sprite

2013/5/25

#
Ofcourse --' thanks And how do I save this orientation in the method Move (line 88) ?
danpost danpost

2013/5/25

#
It appears that you are trying to set a method to a value on line 88. This you cannot do. You just need to run the method with the proper argument:
getOrientatie(0);
Unless you need to make use of a switch statement (for school or something), you could just use 'setRotation' directly and remove the 'getOrientatie' method altogether.
Nike.Sprite Nike.Sprite

2013/5/25

#
yes I need to use the switch statement because we need to save the orientation in it. But now, my actor still doesn't rotate :( i tried different things in the act methode but it still doesn't work... he only moves 1 spot per act automatically (like in the act method). This is ok, but he won't turn when I use the function isKeyDown in the MOVE method (line 88).
Greenfoot.isKeyDown("down")
{
getOrientation(3);
}
danpost danpost

2013/5/25

#
You 'Move' method is never called (java is case-sensitive). Change the method declaration to
public void move()
Nike.Sprite Nike.Sprite

2013/5/25

#
I changed it, but it won't rotate if i press down key I have a pacmanactor that is moving automatically, i want turn it when i push the down, left, right, up button. Exemple when i push up, the actor needs to rotate with his mouth on top en then he must move up... Just saying: You're so great, thanks for helping me all the time, I have a lot of respect for people with so much patience.. (y)
danpost danpost

2013/5/25

#
Please post what you now have in the Pacman class.
Nike.Sprite Nike.Sprite

2013/5/25

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Pacman here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Pacman extends Actor
{
    int orientatie;

    private GreenfootImage pacmanopen = new GreenfootImage("pacmanopen.png");
    private GreenfootImage pacmantoe = new GreenfootImage("pacmantoe.png");

    public Pacman(String naam)
    {
        setImage(naam+".png");
    }

    public void act()
    {
        changeImage();
        checkFood();
        sides();
        move(); 

        if (canMove() == false)
        {
            move(1);
        }
    }

    public void changeImage()
    {
        if (getImage() == pacmanopen)
        {
            setImage(pacmantoe);
        }
        else
        {
            setImage(pacmanopen);
        }
    }

    public void getOrientatie(int degrees) // werkt nog niet
    {
        switch(orientatie) 
        {
            case 0:
            setRotation(0);
            break;

            case 1:
            setRotation(90);
            break;

            case 2:
            setRotation(180);
            break;

            case 3:
            setRotation(270);
            break;
        }
    }

    public boolean canMove()
    {
        Actor wall = getOneIntersectingObject(Wall.class);
        if (wall == null)
        {
            return false;
        }
        else
        {
            return true;
        }
    }    

    public void checkFood()
    {
        Actor food = getOneIntersectingObject(Food.class);
        {
            getWorld().removeObject(food);
        }
    }

    public void move() // klopt nog niet
    {
        if (Greenfoot.isKeyDown("down"))
        {
            getOrientatie(3); 
        }
    }

    public void sides() // orientatie moet hier nog aan toegevoegd worden
    {
        if (getX()==0 && getY()==9)
        {
            setLocation(23,9);
        }

        if (getX()==24 && getY()==9)
        {
            setLocation(1,9);
        }
    }
}
here you are ;) Here you can see a picture of my world at this moment:
danpost danpost

2013/5/25

#
Add the following statement before the 'switch' statement in the 'getOrientatie' method:
orientatie = degrees;
Nike.Sprite Nike.Sprite

2013/5/25

#
I also have another problem... my pacman actor is moving but he doesn't stop at a wall, I used the method getOneIntersectingObject(Wall.class); but then he moves on untill he stays on a wall, then he stops moving. I need to have canMove() and a canTurn(for up, down, right, left orientation). I don't know how to do this...
 public void act()
    {
        changeImage();
        checkFood();
        sides();
        move(); 

        if (canMove() == true)
        {
            move(1);
        }
    }

 public void getOrientatie(int graden) // werkt nog niet
    {
        orientatie = graden;
        switch(orientatie) 
        {
            case 0:
            setRotation(0);
            break;

            case 1:
            setRotation(90);
            break;

            case 2:
            setRotation(180);
            break;

            case 3:
            setRotation(270);
            break;
        }
    }

public boolean canMove()
    {
        Actor wall = getOneIntersectingObject(Wall.class);
        
        if (wall == null)
        {
            return true;
        }
        else
        {

            return false;
        }
    }    

public void move()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            getOrientatie(0);
        }

        if (Greenfoot.isKeyDown("down"))
        {
            getOrientatie(1);
        }

        if (Greenfoot.isKeyDown("left"))
        {
            getOrientatie(2);
        }

        if (Greenfoot.isKeyDown("up"))
        {
            getOrientatie(3); 
        }
    }
danpost danpost

2013/5/26

#
Your canMove method checks whether it is intersecting a wall or not. Your code is telling the pacman object to stop once it IS intersecting a wall (too late). What you need to do, is (1) move pacman (2) check for intersecting wall (3) if intersecting, move back. As an alternative (but this would take a little calculation or coding), you could check for wall at an offset dependent on the rotation of the pacman before moving the pacman.
Nike.Sprite Nike.Sprite

2013/5/26

#
when I'm moving back my pacman, he's jumping back and for against the wall. How does the offset works, I mean to put the pacman back without jumping against a wall ? Kind regards Nike Sprite
There are more replies on the next page.
1
2