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

2021/3/18

How to get an actor to move up to a certain point in the word

oofitsme oofitsme

2021/3/18

#
Hello! I'd be grateful for any replies I can get. I am making a restaurant game where people walk up and order their food. So far I have made the walking animation for an actor but I need the actor to move to a certain point in the world and stop. I am extremely new to this platform so please bear with me :> The dimensions of my world are: (700, 460, 1) Here is my code for the actor:
public class RedCustomer extends Actor
{
    private GreenfootImage redWalk = new GreenfootImage("RedCustomerWalkingFinal.png");
    private GreenfootImage redStill = new GreenfootImage("RedCustomerStillFinal.png");

    public void act() 
    {
      walkinganimation();
  }
    public void walkinganimation()
    {
         Greenfoot.setSpeed(25);
      
         setImage(redWalk);
         move(5);
         Greenfoot.delay(1);
         setImage(redStill);
         move(5);
         Greenfoot.delay(1);

         if (getX() == 130 && getY == 120) {
    move(0) }
     
         
    }
}
danpost danpost

2021/3/18

#
First problem: "move(0);" does absolutely nothing. An actor will not move unless commanded to move some non-zero amount. That is, you do not have to tell it not to move because that is its natural state. It is like telling a red rose to turn red. Second problem: you are programming the act method as a series of steps to take. You are using Greenfoot.delay as a crutch to perform the series of steps. Using delay like this will freeze all other actors and the world until the full series of steps is completed. The act method should be programmed to perform a specific action or actions for any one instance in time. Normal scenario speed (50) is recommended. You might try this:
public void act()
{
    if (getX() < 130) // walking up
    {
        move(1); // moving
        if (getX()%5 == 0) // animating
        {
            if ((getX() == 130) setImage(redStill); // finish walk
            else if (getX()/5)%2 == 0) setImage(redWalk); else setImage(redStill); // continue walk
        }
        return; // do nothing else when walking up
    }
    // order food
}
oofitsme oofitsme

2021/3/20

#
So decided scrap the customer walking and just do this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * When the player clicks on this customer it will spawn two order tickets (one is correct and the other is wrong). It delays for 10 so the player can view the images. After the delay, the user is asked "Which order ticket is correct?" If they respond "right", the customer is removed from the world. If they respond wrong, they must try again.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RedCustomer extends Actor
{
    private GreenfootImage redWalk = new GreenfootImage("RedCustomerWalkingFinal.png");
    private GreenfootImage redStill = new GreenfootImage("RedCustomerStillFinal.png");
    // private GreenfootImage redHappy = new GreenfootImage("RedCustomerHappyFinal.png");
    private String answer;
   /**
     * Act - do whatever the RedCustomer wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         
       // Greenfoot.delay(1); This part is supposed to change images customer's animation
       // setImage(redWalk);   from walking to still but is commented out.
       // Greenfoot.delay(1);   
       // setImage(redStill);
       
     if (Greenfoot.mousePressed(this) == true) { // player presses on customer
         getWorld().addObject(new OrderSlip1(), 180,200); //spawns wrong order ticket
         getWorld().addObject(new OrderSlip2(), 500, 200); //spawns wrong order ticket
         Greenfoot.delay(10); // allows player to look at tickets from a couple seconds
         answer = Greenfoot.ask("Which order ticket is right?"); // player input
         if (answer == "right") { //correct answer
           getWorld().removeObject(RedCustomer); //this customer is removed from world
         }
       } 
  }
}
I am getting an error that says "variable RedCustomer cannot be resolved" for the line trying to remove the object. Am I still using delay as a crutch if it is only an image for the player to see?
danpost danpost

2021/3/20

#
A class name is not an Actor object (instance of Actor), which is required by removeObject. Use:
getWorld().removeObjects(getWorld().getObjects(RedCustomer.class));
You need to login to post a reply.