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

2015/3/26

Help Needed! Skeleton movement tracking!

1
2
TheGoldenBucket TheGoldenBucket

2015/3/26

#
Here I will post all of the code that I am using for a movement system for my skeleton. PlayerChar is the player, and "skelly" is the world. This is in the skeleton's class.
public void act() 
    {
        directionCheck();
        move();
        healthCheck();
        attack();
        deadCheck();
    }    
    
    public void directionCheck()
    {
        if (player.getX() > this.getX())
        {
            directionX = (1);
        }
        if (player.getX() < this.getX())
        {
            directionX = (2);
        }
        if (player.getY() > this.getY())
        {
            directionY = (1);
        }
        if (player.getY() < this.getY())
        {
            directionY = (2);
        }
    }
This is in the "skelly" class.
public class skelly extends World
{
    public PlayerChar thePlayer;
    public Skeleton skeleton1;

    /**
     * Constructor for objects of class skelly.
     * 
     */
    public skelly()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        skeleton1 = new Skeleton();
        addObject(skeleton1, 300, 200);
        thePlayer = new PlayerChar();
        addObject(thePlayer, 5, 5);
    }
    
    public PlayerChar getPlayer()
    {
        return thePlayer;
    }
}
danpost danpost

2015/3/26

#
TheGoldenBucket wrote...
Here I will post all of the code that I am using for a movement system for my skeleton.
What you posted could not be all of your code for that. I see 'move' being called from the 'act' method; but, I do not see the 'move' method code.
Super_Hippo Super_Hippo

2015/3/26

#
@danpost: 'move' is a standard Actor method. @TheGoldenBucket: You change the values of directionX and directionY. Where do you use them?
danpost danpost

2015/3/26

#
Super_Hippo wrote...
@danpost: 'move' is a standard Actor method.
The standard 'move' method of the Actor class requires an int argument. The call to 'move' above does not have that.
Super_Hippo Super_Hippo

2015/3/26

#
Oh, I see. This method is provided with the Animal class.
danpost danpost

2015/3/26

#
Super_Hippo wrote...
Oh, I see. This method is provided with the Animal class.
The Animal class does indeed have a 'move' method with no argument. However, I, for one, am not going to presume that the Skeleton class is a subclass of the Animal class as a 'move' method with no argument could be written into any class defined within the project (and, in this case, directly into the Skeleton class or any class between it and the Actor class).
TheGoldenBucket TheGoldenBucket

2015/3/27

#
My "move" method calls directionX and directionY in order to specify the direction to move(up, down, left, right, or a combination) If you would like to see my "move" method, then just tell me. As far as I can see, the terminal is throwing an error at the code that I posted above.
TheGoldenBucket TheGoldenBucket

2015/3/27

#
I forgot to post this part: this shows how I am trying to reference the world within the Skeleton class to obtain the Player's coords.
skelly skellyWorld = (skelly) getWorld();
PlayerChar player = skellyWorld.getPlayer();
danpost danpost

2015/3/27

#
TheGoldenBucket wrote...
I forgot to post this part: this shows how I am trying to reference the world within the Skeleton class to obtain the Player's coords.
skelly skellyWorld = (skelly) getWorld();
PlayerChar player = skellyWorld.getPlayer();
Where did you place these two lines within the class? Please post the entire Skeleton class for (hopefully) a quick resolution to the issue.
TheGoldenBucket TheGoldenBucket

2015/3/27

#
Alright, here it is! PS: Anywhere where it says "collision code", I will tackle later. Right now I'm just trying to fix this.
import greenfoot.*;

/**
 * Write a description of class Skeleton here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Skeleton extends Actor
{
    private int health;
    private int directionY;
    private int directionX;
    skelly skellyWorld = (skelly) getWorld();
    PlayerChar player = skellyWorld.getPlayer();
    
    /**
     * Act - do whatever the Skeleton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        directionCheck();
        move();
        healthCheck();
        attack();
        deadCheck();
    }    
    
    public void directionCheck()
    {
        if (player.getX() > this.getX())
        {
            directionX = (1);
        }
        if (player.getX() < this.getX())
        {
            directionX = (2);
        }
        if (player.getY() > this.getY())
        {
            directionY = (1);
        }
        if (player.getY() < this.getY())
        {
            directionY = (2);
        }
    }
    
    public void move()
    {
        if (directionX == (1))
        {
            setLocation (1,0);
        }
        if (directionX == (2))
        {
            setLocation (-1,0);
        }
        if (directionY == (1))
        {
            setLocation (0,1);
        }
        if (directionY == (2))
        {
            setLocation (0,-1);
        }
    }
    
    public void healthCheck()
    {
        if (health == (0))
        {
            health = (3);
        }
        //Insert collision code here.
    }
    
    public void attack()
    {
        if (health == (4))//collision code with player
        {
            Main_World.playerHealth--;
        }
    }
    
    public void deadCheck()
    {
        if (health == (4))//collision code with sword
        {
            health--;
        }
        if (health == (4))//collision code with arrow
        {
            health--;
        }
        if (health == (1))
        {
            skellyWorld.removeObject(this);
        }
    }
}
fejfo fejfo

2015/3/27

#
in your move method u do :
public void move()
    {
        if (directionX == (1))
        {
            setLocation (1,0);
        }
        if (directionX == (2))
        {
            setLocation (-1,0);
        }
        if (directionY == (1))
        {
            setLocation (0,1);
        }
        if (directionY == (2))
        {
            setLocation (0,-1);
        }
    }
while u should set the location relative to the current location like this :
public void move()
    {
        if (directionX == (1))
        {
            setLocation (getX()+1,getY());
        }
        if (directionX == (2))
        {
            setLocation (getX()-1,getY());
        }
        if (directionY == (1))
        {
            setLocation (getX(),getY()+1);
        }
        if (directionY == (2))
        {
            setLocation (getX(),getY()-1);
        }
    }
TheGoldenBucket TheGoldenBucket

2015/3/27

#
Ok, I get that but thats not my problem right now.
fejfo fejfo

2015/3/27

#
what is wrong then ?
davmac davmac

2015/3/27

#
As far as I can see, the terminal is throwing an error at the code that I posted above.
I think you mean that the terminal is displaying a stack trace from an exception that the code you posted above is throwing - in which case, please post that stack trace (and in future if you need help because your code is throwing an exception, always post the stack trace!)
danpost danpost

2015/3/27

#
The problem is where you placed those lines (lines 14 and 15 of the Skeleton class. 'getWorld' cannot return any value other than null until the skeleton is past the creation stage and then entered into the world. Where you placed those lines, the 'skelly skellyWorld' field will be assigned a 'null' value and the 'PlayerChar player' field will cause an error because you are calling a method on a null value. Again, 'skellyWorld' cannot be assigned a value other than 'null' by the use of 'getWorld' until the skeleton is added into the world. Therefore, you should override the Actor class 'addedToWorld' method which is called automatically when the actor is added into the world and place the code within it:
protected void addedToWorld(World world)
{
    skellyWorld = (skelly) world;
    player = skellyWorld.getPlayer();
}
Change lines 14 and 15 to simply this:
private skelly skellyWorld;
private PlayerChar player;
There are more replies on the next page.
1
2