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

2014/12/10

Getting a game to move actor from one level to the next?

tcoale tcoale

2014/12/10

#
Hi, I am currently making a platformer game and am trying to move my actor from the level he's on to Level 2 when he reaches a certain coordinate. So far, I've succeeded in getting the game to move him to the next level when he reaches a certain location, but as soon as he enters the level 2 map, I get the error "java.lang.ClassCastException: Level2 cannot be cast to GameManager at Creature.act(Creature.java:91)" GameManager is where my first level is, and Creature is an actor class that has a subclass (Wolf) that which contains information for the enemies in the game, which obviously, are wolves. What's going on?
davmac davmac

2014/12/10

#
"java.lang.ClassCastException: Level2 cannot be cast to GameManager at Creature.act(Creature.java:91)"
What's going on?
On line 91 of your Creature class, you're casting an object (presumably your world) that's an instance of Level2 to GameManager, which is an illegal cast, because the object is not an instance of GameManager.
tcoale tcoale

2014/12/10

#
Thank you for the quick reply. This is the line you mentioned in my Creature class; how would I go about fixing it? Simply removing it?
GameManager world = (GameManager) getWorld();
davmac davmac

2014/12/10

#
This is the line you mentioned in my Creature class; how would I go about fixing it? Simply removing it?
That would depend entirely on what the purpose of this line is, i.e. why it was written in the first place. That is impossible for me to ascertain, unless you provide an explanation and/or much more of the relevant source code.
tcoale tcoale

2014/12/10

#
Oh, alright. Most of this is code that was already present in the game when I got it (I'm simply editing it to have multiple levels and different enemies), so instead, here is the entirety of the Creature class, if that helps at all.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * A creature superclass.
 *
 * @author Ed Parrish
 * @version 1.0  6/27/2011
 */
public abstract class Creature extends Sprite
{
    public static final int RIGHT = 0;
    public static final int LEFT = 1;
    public static final int STATE_NORMAL = 0;
    public static final int STATE_DYING = 1;
    public static final int STATE_DEAD = 2;
    private static final int DEFAULT_DYING_TIME = 30;
    private static final double DEFAULT_MOVE_SPEED = 2.5;
    private int direction = LEFT;
    private int dyingTime = DEFAULT_DYING_TIME;
    private int dyingCount;
    private int leftX;
    private int rightX;
    // Core animations
    private Animation moveLeft;
    private Animation moveRight;
    private Animation dyingLeft;
    private Animation dyingRight;
    private boolean soundPlaying = false;

    /**
     * Constructs a Creature with no animations. Use another constructor
     * if you want animations.
     */
    public Creature()
    {
        setVelocityX(-DEFAULT_MOVE_SPEED);
    }

    /**
     * Construct a new creature using the specified animations.
     *
     * @param leftMove Move to the left.
     * @param rightMove Move to the right.
     * @param leftDying Die when facing left.
     * @param rightDying Die when facing right.
     */
    public Creature(Animation leftMove, Animation rightMove,
    Animation leftDying, Animation rightDying)
    {
        setAnimations(leftMove, rightMove, leftDying, rightDying);
        setVelocityX(-DEFAULT_MOVE_SPEED);
    }

    /**
     * Set the core animations for this creature.
     *
     * @param leftMove Move to the left.
     * @param rightMove Move to the right.
     * @param leftDying Die when facing left.
     * @param rightDying Die when facing right.
     */
    protected void setAnimations(Animation leftMove, Animation rightMove,
    Animation leftDying, Animation rightDying)
    {
        moveLeft = leftMove;
        moveRight = rightMove;
        dyingLeft = leftDying;
        dyingRight = rightDying;
        setAnimation(moveLeft);
        setVelocityX(-DEFAULT_MOVE_SPEED);
        if (dyingLeft != null)
        {
            dyingTime = dyingLeft.getImageCount()
                * dyingLeft.getImageDuration();
        }
    }

    /**
     * Update and then move this sprite.
     */
    public void act()
    {
        if (getX() <= leftX)
        {
            moveRight();
        }
        else if (getX() >= rightX)
        {
            moveLeft();
        }
        GameManager world = (GameManager) getWorld();
        Player player = world.getPlayer();
        if (intersects(player))
        {
            processCollision(player);
            if(soundPlaying == false){
                soundPlaying = true;
                Greenfoot.playSound("wolf.wav");
            }
        }
        if (getState() == STATE_DYING)
        {
            updateDyingState();
        }
        if (getState() == STATE_DEAD)
        {
            GameManager.scoreCounter.setValue(GameManager.scoreCounter.getValue() + 100);
        }
        move();
    }

    /**
     * Move to the left.
     */
    public void moveLeft()
    {
        setVelocityX(-Math.abs(getVelocityX()));
        setAnimation(moveLeft);
        direction = LEFT;
    }

    /**
     * Move to the right.
     */
    public void moveRight()
    {
        setVelocityX(Math.abs(getVelocityX()));
        setAnimation(moveRight);
        direction = RIGHT;
    }

    /**
     * Set the movement range from leftmost to rightmost x-coordinate.
     *
     * @param leftmost The lowest x-coordinate in pixels.
     * @param rightmost The highest x-coordinate in pixels
     */
    public void setRangeX(int leftmost, int rightmost)
    {
        leftX = leftmost;
        rightX = rightmost;
    }

    /**
     * Returns this creatures leftmost x-coordinate for movement.
     *
     * @return The leftmost x-coordinate for movement in pixels.
     */
    public int getLeftX()
    {
        return leftX;
    }

    /**
     * Returns this creatures rightmost x-coordinate for movement.
     *
     * @return The rightmost x-coordinate for movement in pixels.
     */
    public int getRightX()
    {
        return rightX;
    }

    /**
     * Decide effects of a collision with the player and process accordingly.
     */
    public void processCollision(Player player) {
        if (!player.isAlive()) {
            return;
        }
        int playerBottom = player.getY() + player.getHeight() / 2;
        int creatureTop = getY() - getHeight() / 2;
        int maxOverlap = (int)(player.getVelocityY() + player.GRAVITY + 0.5);
        if (playerBottom - maxOverlap <= creatureTop
        || getState() == STATE_DYING)
        {
            // player wins
            int h2 = (getHeight() + player.getHeight()) / 2;
            int backoffY = getY() - player.getY() - h2;
            player.setLocation(player.getX(), player.getY() + backoffY);
            player.setVelocityY(-player.getVelocityY()); // bounce
            if (getState() != STATE_DYING)
            {
                Greenfoot.playSound("squish.wav");
                startDying();
            }
        } else {
            // creature wins
            player.startDying();
        }
    }

    /**
     * Set the number of act() method calls the dying animation is displayed.
     *
     * @param newDyingTime The time to die.
     */
    public void setDyingTime(int newDyingTime)
    {
        dyingTime = newDyingTime;
    }

    /**
     * Start the dying sequence.
     */
    public void startDying()
    {
        setState(STATE_DYING);
        setVelocityX(0.0);
        setVelocityY(0.0);
        if (direction == LEFT)
        {
            setAnimation(dyingLeft);
        }
        else
        {
            setAnimation(dyingRight);
        }
    }

    /**
     * Handle the process of dying.
     */
    private void updateDyingState()
    {
        dyingCount++;
        if (dyingCount > dyingTime)
        {
            setState(STATE_DEAD);
            removeFromWorld();
        }
    }
}
tcoale tcoale

2014/12/10

#
Is the game trying to set the creatures to the level in GameManager but can't because my character isn't in the level in GameManager any longer? I'm really new at this, so I'm not sure how to fix this at all.
danpost danpost

2014/12/11

#
The game is trying to set the Level2 world that the character is in to a reference that is to hold a GameManager object. It is like trying to set 'boolean onGround = 3;'. You cannot set a field to a value that is not the same type as the field is declared to hold.
tcoale tcoale

2014/12/11

#
danpost wrote...
The game is trying to set the Level2 world that the character is in to a reference that is to hold a GameManager object. It is like trying to set 'boolean onGround = 3;'. You cannot set a field to a value that is not the same type as the field is declared to hold.
Thank you for responding to help, this has been a thorn in my side for several hours now. I really apologize though, because I still don't really understand how to fix it. I'm very, very new to coding so even though you tell me what the problem is, I'm not sure how to rewrite my code so that it's fixed, and I apologize for that.
danpost danpost

2014/12/11

#
You may have to use the 'instanceof' keyword to determine which world the character is in, so you can get the player from the world. Although, it may be easier, if only one player is in the world at any time, to just use '(Player)getWorld().getObjects(Player.class).get(0)'.
tcoale tcoale

2014/12/11

#
So where would I put that? Within the act method of the player class?
danpost danpost

2014/12/11

#
Remove line 91 and replace what you are setting 'player' to in line 92 with that code.
tcoale tcoale

2014/12/11

#
Thank you very much! Finally, it works, haha. Will this also be fine when the player proceeds to Level3?
danpost danpost

2014/12/11

#
The only reason you were accessing the GameManager world was to get a reference to the player. What you are using now will get that reference no matter what world this actor is in (provided there is a player to get).
tcoale tcoale

2014/12/11

#
Thanks again, you're a real lifesaver.
You need to login to post a reply.