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

2013/11/22

I need help with movement

Stylow Stylow

2013/11/22

#
I just added a score counter to my game, finally got it working and now when I run the game my characters can't seem to move, none of them, they just freeze in place. As I'm not sure what code you guys need to see what's wrong I'll give you the codes of the World, Counter & Characters. World Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class crumpled here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class crumpled extends World
{

    /**
     * Constructor for objects of class crumpled.
     * 
     */
    public crumpled()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
        Greenfoot.setSpeed(50);
    }
    public void act() 
    {
        
        if (Greenfoot.isKeyDown("enter")) 
        {
            Greenfoot.setWorld(new crumpled());
        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Zombie zombie = new Zombie();
        addObject(zombie, 61, 211);
        BrainCounter braincounter = new BrainCounter(zombie);
        addObject(braincounter, 84, 33);
        enemy enemy = new enemy();
        addObject(enemy, 494, 105);
        enemy enemy2 = new enemy();
        addObject(enemy2, 442, 197);
        enemy enemy3 = new enemy();
        addObject(enemy3, 346, 337);
        enemy enemy4 = new enemy();
        addObject(enemy4, 349, 336);
        enemy enemy5 = new enemy();
        addObject(enemy5, 36, 125);
        enemy enemy6 = new enemy();
        addObject(enemy6, 249, 155);
        enemy enemy7 = new enemy();
        addObject(enemy7, 344, 115);
        enemy enemy8 = new enemy();
        addObject(enemy8, 361, 141);
        enemy4.setLocation(409, 331);
        removeObject(enemy4);
        brain brain = new brain();
        addObject(brain, 511, 303);
        brain brain2 = new brain();
        addObject(brain2, 418, 286);
        brain brain3 = new brain();
        addObject(brain3, 280, 110);
        brain brain4 = new brain();
        addObject(brain4, 423, 78);
    }
}
Counter code:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

public class BrainCounter extends Actor
{
    private static final Color TEXT_COLOR = new Color(200, 0, 0);
    private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);

    private Zombie zombie;

    public BrainCounter(Zombie zombie)
    {
        this.zombie = zombie;

        updateImage();
    }

    public void act() {
        updateImage();
        
        if (zombie.getScore() == 0)
        {
            Greenfoot.setWorld(new crumpled());
        }
    }

    private void updateImage()
    {
        String text = "Brains eaten: " + zombie.getScore();
        GreenfootImage image = new GreenfootImage(text, 20, TEXT_COLOR, 
                TRANSPARENT_COLOR);
        setImage(image);
    }
}
Character code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class awesome here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Zombie extends Animal
{
    public int brainsNibbled=0;
    /**
     * Act - do whatever the awesome wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
        public void act() 
    {
        moveAndTurn();
        brainsNibbled();
        getScore();

    }

    public void moveAndTurn()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }

        if (Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }

        if (Greenfoot.isKeyDown("up"))
        {
            move(4);
        }

        if (Greenfoot.isKeyDown("down"))
        {
            move(-4);
        }
    }

    public void brainsNibbled()
    { 
        Actor brain;
        brain =  getOneObjectAtOffset(0, 0, brain.class);
        if (canSee (brain.class) )
        {
            brainsNibbled++;
            World world;
            world = getWorld();
            world.removeObject(brain);
            getWorld().addObject(new brain(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
        }

    }

    public int getScore()
    {
        return brainsNibbled;
    }
}
If you could help me I'd really appreciate it.
danpost danpost

2013/11/22

#
Lines 21 through 24 in the counter class says 'if the score of the zombie is zero, create a new world'. But the score of the zombie is zero to start with, so you program is repeatedly creating new worlds (stuck in a loop).
Gevater_Tod4711 Gevater_Tod4711

2013/11/22

#
I think the problem is in line 21 to 24 of your BrainCounter class. If the score of your zombie is 0 you end the game end create a new world. And because the starting value of the zombie's score is 0 you create a new world every act so it seems that your characters can't move. You prabably wanted to start a new game when your zombie has eaten some brains so you can just change the 0 in line 21 of your BrainCounter class to another value. Some other improvements: Line 20 in your Zombie class is not neccessary. You can delete this line. In your brains Nibbled method in your Zombie class you could change line 51 to: if (brain != null) That would not change the funktion of the method but it would be easyer and would perform a bit faster (It's realy only a little bit but that would be better coding).
Stylow Stylow

2013/11/22

#
Thanks. It works now.
shahparacha shahparacha

2013/11/22

#
ok try it again it will work \
shahparacha shahparacha

2013/11/22

#
I thing u have to do it again and do it other way :)
You need to login to post a reply.