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
danpost danpost

2013/5/26

#
One way to process the keys and move is with the following:
public void move()
{
    int dx=0, dy=0; // to hold the offsets
    if (Greenfoot.isKeyDown("up")) dy--;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("right")) dx++;
At this point dx and dy will both contain either zero, one, or negative one. In order for the input to be valid and the object to turn, one of the two offsets must be zero, one of the two offsets must not be zero, and a wall cannot be present at the offset location; so, we continue with:
if (dx*dy == 0 && // one is zero if true
    dx+dy != 0 && // one is not zero if true (since one is zero)
    getOneObjectAtOffset(dx, dy, Wall.class) == null) // wall not at offset location if true
        setRotation(((1-dx)*(dx*dx)+(2-dy)*(dy*dy))*90); // turn appropriate degrees
At this point, we have completed the 'turn' part of the method; now, we can perform the 'move' part.
    move(1); // move forward
    if (getOneIntersectingObject(Wall.class) != null) move(-1); // if hit wall, move back
}
I think everything was quite clearly explained, except for the calculation of the degrees in turning in the 'setRotation' call. The second part on both sides of the expression (the dx*dx and dy*dy) changes any negative ones to ones, and will zero out that side if the offset was zero. The first part on both sides evaluates to the 90 degree increment, provided the offset is not zero.
Nike.Sprite Nike.Sprite

2013/5/26

#
It's because we may not use the dx & dy (school orders) that's why i'm searching another method ;s
danpost danpost

2013/5/26

#
Then you will have to qualify each case individually for turning. Starting something like this:
if ((Greenfoot.isKeyDown("up") && !Greenfoot.isKeyDown("down") &&
    (Greenfoot.isKeyDown("right") == Greenfoot.isKeyDown("left"))
{
    if (getOneObjectAtOffset(getX(), getY()-1, Wall.class) != null) setRotation(270);
}
// etc. (for the other three directions
Nike.Sprite Nike.Sprite

2013/5/26

#
Why do you say NOT down, and right is equal as left ?
Nike.Sprite Nike.Sprite

2013/5/26

#
this is what's in my books: We need to move the pacman actor with the keyboard. e.g.: when I'm moving to the right and I push up key, then a method CanTurnn(orientation for up) must check if pacman actor can get this new orientation or not. The rule here is that keyboard interaction cannot take care of that the pacman actor is facing with open mouth to a Wall(). If i can get the new orientation, give it and after that give the moving direction to pacman actor with the use of method setRoation(). (sorry for bad english)
danpost danpost

2013/5/26

#
By adding those conditions, we are eliminating conflicting input. For 'NOT down', if opposite arrow keys are being pressed at the same time (opposing input), we do not want to accept either as a way to go. For 'right equal to left', if we have more than one valid direction to go (conflicting input), again, we do accept either.
danpost danpost

2013/5/26

#
If I am not mistaken, all you would need to do is rearrange what I just gave you
if (Greenfoot.isKeyDown("up")) // I would add the other conditions as I did above here
{
    if (canTurn(270)) setRotation(270);
}
// the above done for each key
// with the following method
private boolean canTurn(int direction)
{
    if (direction == 270)
    {
        return getOneObjectAtOffset(Wall.class) == null;
    }
    // other directions
}
Nike.Sprite Nike.Sprite

2013/5/26

#
you missed with the getOneObjectAtOffset, you also need an integer.. getOneObjectAtOffset (int, int, java.lang.class)
Nike.Sprite Nike.Sprite

2013/5/26

#
to show hof far I am now, I only need a canTurn method as you can see ;)
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(orientatie) == true)
        {
            move(1);
        }
    }

    public void changeImage() // open mond, sluit mond, open mond, sluit mond, ...
    {
        if (getImage() == pacmanopen)
        {
            setImage(pacmantoe);
        }
        else
        {
            setImage(pacmanopen);
        }
    }

    public void getOrientatie(int graden) // geef een orientatie mee aan de pacman
    {
        orientatie = graden;
        switch(orientatie) 
        {
            case 0: // right
            setRotation(0);
            break;

            case 1: // down
            setRotation(90);
            break;

            case 2: // left
            setRotation(180);
            break;

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

    private boolean canMove(int orientatie) // kijk of er geen wall in de weg staat
    {

        switch(orientatie)
        {
            case 0: // right
            Actor wall = getOneObjectAtOffset(1,0, Wall.class);
            if (wall == null)
            {
                return true;
            }
            else
            {
                return false;
            }

            case 1: // down
            wall = getOneObjectAtOffset(0,1, Wall.class);
            if (wall == null)
            {
                return true;
            }
            else
            {
                return false;
            }

            case 2: // left
            wall = getOneObjectAtOffset(-1,0, Wall.class);
            if (wall == null)
            {
                return true;
            }
            else
            {
                return false;
            }

            case 3: // up
            wall = getOneObjectAtOffset(0,-1, Wall.class);
            if (wall == null)
            {
                return true;
            }
        }
        return false;
    }

    public void checkFood() // kijk of pacman iets kan opeten, zo ja, eet op.
    {
        Actor food = getOneIntersectingObject(Food.class);
        {
            getWorld().removeObject(food);
        }
    }

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

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

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

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

    public void sides() // als pacman aan de linkerkant/rechterkant van het veld zit, dan moet hij langs de rechterkant/linkerkant erweer uitkomen
    {
        if (getX()==0 && getY()==9)
        {
            setLocation(23,9);
        }

        if (getX()==24 && getY()==9)
        {
            setLocation(1,9);
        }
    }
}
danpost danpost

2013/5/26

#
Nike.Sprite wrote...
you missed with the getOneObjectAtOffset, you also need an integer.. getOneObjectAtOffset (int, int, java.lang.class)
Yeah, that should have been 'getOneObjectAtOffset(0, -1, Wall.class)'. Or does that also fall in the category of dx and dy usage :? Ok, now for canTurn. 1) save current rotation 2) turn given direction 3) move 4) check intersecting wall 5) move back 6) reset rotation 7) return whether it intersected a wall or not
Nike.Sprite Nike.Sprite

2013/5/26

#
Ok I think I can work a bit further now, Thank you so much ! (btw, you got a new follower, you're a great greenfoot programmer) good night Nike Sprite
You need to login to post a reply.
1
2