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

2019/7/2

Food Movement

AdiBak AdiBak

2019/7/2

#
Hello, I'm making the agar.io game, and I got the player to follow the mouse with some delay, and the food to spawn, but I'm confused on how to make the food move with the player movement, similar to screen scrolling. Can someone please help? Here's the Player class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    /**
     * Act - do whatever the Player 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.
        MouseInfo mou = Greenfoot.getMouseInfo();
        if (mou != null){
            turnTowards(mou.getX(), mou.getY());
            move(3);
        }
    }    
}
Here's the MyWorld code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

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

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

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Player player = new Player();
        addObject(player,301,247);
    }
    public void act(){
        if (Greenfoot.getRandomNumber(100) < 3){
            Food f = new Food();
            addObject(f, Greenfoot.getRandomNumber(getWidth()), Greenfoot.getRandomNumber(getHeight()));
        }
    }
}
Here's the Food class code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Food here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Food extends Actor
{
    public Food(){
        getImage().scale(30, 30);
    }
    /**
     * Act - do whatever the Food 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.
        Player p = getWorld().getObjects(Player.class).get(0);
        
    }    
}
danpost danpost

2019/7/2

#
AdiBak wrote...
I'm making the agar.io game, and I got the player to follow the mouse with some delay, and the food to spawn, but I'm confused on how to make the food move with the player movement, similar to screen scrolling.
From what you gave, you make it sound like the food and player will both move in a cordinated fashion (in unison). As such, the player will never be able to reach the food. I think what you mean to say is that the player is to be positioned center screen and appear to move by scrolling the actors. Please check out my Scrolling Tutorial scenario.
You need to login to post a reply.