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/4/7

#
@davmac - Thanks, but the console's copy button did not work at the time that I posted this thread. @danpost - I am going to try that method soon. I'll post my results back here on this thread. Thank you. -TheGoldenBucket
TheGoldenBucket TheGoldenBucket

2015/4/8

#
Still getting errors... here is the modified code of the Skeleton class
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;
    private skelly skellyWorld;
    private PlayerChar player;
    
    protected void addedToWorld(World world)
    {
        skellyWorld = (skelly) world;
        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() > 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);
        }
    }
}
And here is the stacktrace: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:681) at greenfoot.Actor.getX(Actor.java:157) at Skeleton.directionCheck(Skeleton.java:38) at Skeleton.act(Skeleton.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2015/4/8

#
You need to ensure the player is in the world before calling 'player.getX()'. As the first line in the 'directionCheck' method, use this:
if (player.getWorld() == null) return;
This will prevent the rest of the method from executing if the player is not in the world.
TheGoldenBucket TheGoldenBucket

2015/4/9

#
Well... the skeleton just jumped right to the player's location. I'll check the skeleton's movement code to see if I can slow it down.
TheGoldenBucket TheGoldenBucket

2015/4/9

#
Well... I just changed a bit of the code and the player now jumps off the screen as soon as the world is run. Suggestions?
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;
    private skelly skellyWorld;
    private PlayerChar player;
    
    protected void addedToWorld(World world)
    {
        skellyWorld = (skelly) world;
        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.getWorld() == null) return;
        if (player.getX() > 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 (this.getX()+1,this.getY());
        }
        if (directionX == (2))
        {
            setLocation (this.getX()-1,this.getY());
        }
        if (directionY == (1))
        {
            setLocation (this.getX(),this.getY()+1);
        }
        if (directionY == (2))
        {
            setLocation (this.getX(),this.getY()-1);
        }
    }
    
    public void healthCheck()
    {
        if (health == (0))
        {
            health = (3);
        }
        //Insert collision code here.
    }
    
    public void attack()
    {
        if (health == (4))//collision code with player
        {
            
        }
    }
    
    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);
        }
    }
}
danpost danpost

2015/4/9

#
TheGoldenBucket wrote...
Well... I just changed a bit of the code and the player now jumps off the screen as soon as the world is run. Suggestions? < Code Omitted >
My suggestion is that you post the code for the player (if it is the one not acting properly).
TheGoldenBucket TheGoldenBucket

2015/4/10

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

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

    

    /**
     * Act - do whatever the PlayerChar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public void act() 
    {
        movement();
        meleeAttack();
        rangedAttack();
        staminaRegen();
        hurtCheck();
        healthCheck();
    }    

    public void movement()
    {
        GreenfootImage charImage = null;
        if (Greenfoot.isKeyDown("a"))
        {
            int x = getX();
            int y = getY();
            int ny = getX()-2;
            setLocation(ny,y);
            if (getOneIntersectingObject(null) != null)
            {
                setLocation(getX()+2, getY());
            }
            charImage = new GreenfootImage("IMG_1193.PNG");
            Main_World.playerTurn = (1);
        }
        if (Greenfoot.isKeyDown("d"))
        {
            int x = getX();
            int y = getY();
            int ny = getX()+2;
            setLocation(ny,y) ;
            if (getOneIntersectingObject(null) != null)
            {
                setLocation(getX()-2, getY());
            }
            charImage = new GreenfootImage("IMG_1192.PNG");
            Main_World.playerTurn = (2);
        }
        if (Greenfoot.isKeyDown("w"))
        {
            int x = getX();
            int y = getY();
            int ny = getY()-2;
            setLocation(x,ny) ;
            if (getOneIntersectingObject(null) != null)
            {
                setLocation(getX(), getY()+2);
            }
            charImage = new GreenfootImage("IMG_1191.PNG");
            Main_World.playerTurn = (3);
        }
        if (Greenfoot.isKeyDown("s"))
        {
            int x = getX();
            int y = getY();
            int ny = getY()+2;
            setLocation(x,ny) ;
            if (getOneIntersectingObject(null) != null)
            {
                setLocation(getX(), getY()-2);
            }
            charImage = new GreenfootImage("IMG_1190.PNG");
            Main_World.playerTurn = (4);
        }
        if (charImage != null) setImage(charImage);
    }

    public void meleeAttack()
    {
        if (Greenfoot.isKeyDown("i"))
        {
            Melee attack = new Melee();
            if (Main_World.playerTurn == (1))
            {
                getWorld().addObject(attack, getX()-25, getY());
            }
            if (Main_World.playerTurn == (2))
            {
                getWorld().addObject(attack, getX()+25, getY());
            }
            if (Main_World.playerTurn == (3))
            {
                getWorld().addObject(attack, getX(), getY()-25);
            }
            if (Main_World.playerTurn == (4))
            {
                getWorld().addObject(attack, getX(), getY()+25);
            }
        }
    }

    public void rangedAttack()
    {
        if (Greenfoot.isKeyDown("o") && Main_World.playerStamina > -1 && getWorld().getObjects(Ranged.class).isEmpty())
        {
            getWorld().addObject(new Ranged(), getX(), getY());
            Main_World.playerStamina--;
        }
    }

    public void staminaRegen()
    {
        age++;
        if (age > 20 && Main_World.playerStamina < (3))
        {
            age = (0);
            Main_World.playerStamina++;
        }
    }

    public void hurtCheck()
    {
        Actor slimeu = getOneObjectAtOffset(0, 0, SlimeballUp.class);
        Actor slimed = getOneObjectAtOffset(0, 0, SlimeballDown.class);
        Actor slimel = getOneObjectAtOffset(0, 0, SlimeballLeft.class);
        Actor slimer = getOneObjectAtOffset(0, 0, SlimeballRight.class);
        if (slimeu != null)
        {
            Main_World.playerHealth--;
            getWorld().removeObject(slimeu);
        }
        if (slimed != null)
        {
            Main_World.playerHealth--;
            getWorld().removeObject(slimed);
        }
        if (slimel != null)
        {
            Main_World.playerHealth--;
            getWorld().removeObject(slimel);
        }
        if (slimer != null)
        {
            Main_World.playerHealth--;
            getWorld().removeObject(slimer);
        }
    }

    public void healthCheck()
    {
        if (Main_World.playerHealth == (0))
        {
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2015/4/10

#
Two questions: (1) is this in a bounded world (or just show your World constructor 'super' call); (2) do you have scrolling implemented in the world; Well, a third one: (3) is there any other code, anywhere in the project, that could possibly change the location of the PlayerChar actor; The third question was more general of the idea of why I asked the second one.
TheGoldenBucket TheGoldenBucket

2015/4/14

#
1. Yes, if that is the default. 2. No. 3. No, unless you count the creation of the world.
davmac davmac

2015/4/14

#
the player now jumps off the screen as soon as the world is run. Suggestions?
By 'jumps off the screen' you mean it disappears from the world? The culprit then is then probably line 164 of the Player class (unless another class removes the player?). So perhaps Main_World.playerHealth is never being set to anything but 0 - and because it's zero health, the PlayerChar removes itself.
TheGoldenBucket TheGoldenBucket

2015/4/14

#
Ahh... its most likely not getting set to 0 because it is in a world other than Main_World. I'll make a change to that, giving line 162 an "&&" and then "skelly.playerHealth == (0)"
TheGoldenBucket TheGoldenBucket

2015/4/14

#
Thanks! My skeleton now works(other than the collision detection, which I can do myself)! -TheGoldenBucket
You need to login to post a reply.
1
2