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

2013/10/22

setDirection

ye-roon ye-roon

2013/10/22

#
Hello everyone, I'm quite new to all this and i dont know how to properly explain it, but i will try my best. Also, this would probably work best if i could upload the whole scenario somewhere, but i can not seem to find the upload button. My problem is as follows, i am currently creating a simple game, where a nerdy guy/loser needs to get to school. The main blueprint is done, and he can move and if he hits the wrong obstacle he fails. The powerups are in place but currently do not do anything, this was just done to see where they would be. Its for a school project and we were designated team L, hence the Loser ^^. The loser has 2 different kinds of moving enemies and 1 kind that is stationary(they are girls! so stationary is scary enough for our nerd). The moving enemies consist of a couple of Dogs and some Jocks that are always eager to make our Losers life misarable. So i was digging around in some code and i found that i could re-use some code from the wombat game to make our Enemies move. I made North 0, and South 1 and removed East and West since its a topdown game and the enemies do not have to move in another direction for now. Maybe if we expand on this project in the future, for now moving up and down is enough. So i changed the code from the wombat scenario in such a way that the enemy will turn around when he hits a piece of Furnature, we have put all our immovable objects as a subclass of Furnature(so walls, the doghouse, school tables). The reason for this is it makes it easier to program, for i can now just use: (Furnature.class), rather then having to make different statements for every piece of furnature. Our walls are set in a maze like pattern. The dogs move between their doghouses and the outerwall of the school. What it does now is: it will automaticly move towards the South. When it hits the the wall in the south, its set the direction to the North and they will happily move north. But then when they hit the north wall they get stuck in a loop and the only thing you can see is the image changing from setDirection(180) to setDirection(0). It doesnt move down anymore. I have tried this with both the isTouching() method and the getOneIntersectingObject() but both end up with the same problem. So i thought, well the images have different pixel sizes, what if i change one with a 30x30 image. But that didnt work either. I will paste the code below. So if anyone has an idea where this goes amiss, that will be gladly appreciated. Also, english is not my native language, so excuse my grammar/spelling mistakes, if applicable. Kind regards, ye-roon
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private static final int NORTH = 0;
    private static final int SOUTH = 1;
    
    private int direction;
    public Enemy()
    {
        setDirection(NORTH);
    }
    
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
    }
        public void move()
    {
        if (!canMove()) {
            return;
        }
        switch(direction) {
            case SOUTH :
                setLocation(getX(), getY() - 1);
                break;
            case NORTH :
                setLocation(getX(), getY() + 1);
                break;
        }
    }
        /**
     * Test if we can move forward. Return true if we can, false otherwise.
     */
    public boolean canMove()
    {
        int x = getX();
        int y = getY();
        switch(direction) {
            case SOUTH :
                y++;
                break;
            case NORTH :
                y--;
                break;
        }
        // test if we are touching a Meubel
        if (getOneIntersectingObject(Meubel.class) != null) {
            return false;
        }
//         if (isTouching(Meubel.class)) {
//             return false;
//         }
        return true;
    }
    public void turnAround()
    {
        switch(direction) {
            case SOUTH :
                setDirection(NORTH);
                break;
            case NORTH :
                setDirection(SOUTH);
                break;
        }
    }
        public void setDirection(int direction)
    {
        if ((direction >= 0) && (direction <= 1)) {
            this.direction = direction;
        }
        switch(direction) {
            case SOUTH :
                setRotation(180);
                break;
            case NORTH :
//                 setLocation(getX(), getY()+2);
                setRotation(0);
                break;
            default :
                break;
        }
    }
}
You need to login to post a reply.