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

2021/4/19

Actors associated with other objects.

oofitsme oofitsme

2021/4/19

#
Hello! I am programming a click-based diner game in which the player clicks the customer and their associated order tickets pop up. Based on if the player clicks the right ticket, it will award them points and remove the objects from the world. Everything was fine until I realized that when running the game, the tickets show up randomly and not with their specific customer. For instance, I programmed one customer to order a burger but when you run the game they instead order a pizza. Any help would be appreciated, thank you. This is my code for one of the customers and their order slips:
import greenfoot.*;  

public class Burger extends OrderSlip1
{
    public void act() 
    {
        correctAnswer();
    }
    
    public void correctAnswer()
    {
           if (Greenfoot.mousePressed(this) == true) {
            getWorld().removeObjects(getWorld().getObjects(OrderSlip2.class));
            getWorld().removeObject(this);
            Diner.tips+=100;
            Diner.customer_visits++;
        }
    }
}
import greenfoot.*;  
public class BurgerWrong extends OrderSlip2
{
    MonkeyDiner diner;
    public void act() 
    {
        wrongAnswer();
    } 
    
    public void wrongAnswer() {
         if (Greenfoot.mousePressed(this) == true) {
            //still deciding if I want to have it do nothing or display text
      } 
    }
}
import greenfoot.*; public class RedCustomer extends Customer { public void act() { spawnOrderSlip(); } public void spawnOrderSlip() { if (Greenfoot.mousePressed(this) == true) { // player presses on customer getWorld().addObject(new Burger(), 180,200); //spawns right order ticket getWorld().addObject(new BurgerWrong(), 500, 200); //spawns wrong order ticket } } }
danpost danpost

2021/4/19

#
Please show Customer, OrderSlip1 and OrderSlip2 class codes. Also, any classes they extend (unless they extend Actor).
oofitsme oofitsme

2021/4/19

#
danpost wrote...
Please show Customer, OrderSlip1 and OrderSlip2 class codes. Also, any classes they extend (unless they extend Actor).
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Customer extends Actor
{
    public void act() 
    {
        //Just to define method
    }
    
    public void spawnOrderSlip() {
         if (Greenfoot.mousePressed(Customer.class) == true) { // player presses on customer
         getWorld().addObject(new OrderSlip1(), 180,200); //spawns right order ticket
         getWorld().addObject(new OrderSlip2(), 500, 200); //spawns wrong order ticket
     }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class OrderSlip1 extends Actor
{  
  MonkeyDiner Diner;
    public void act() 
    { 
     correctAnswer();
  }
  
  public void correctAnswer() {
         if (Greenfoot.mousePressed(OrderSlip1.class) == true) {
            getWorld().removeObjects(getWorld().getObjects(OrderSlip2.class));
            getWorld().removeObject(this);
            Diner.tips+=100;
            Diner.customer_visits++;
     }
  }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class OrderSlip2 extends Actor
{
    public void act() 
    {
      wrongAnswer();
 }
 
    public void wrongAnswer() 
    {
           if (Greenfoot.mousePressed(OrderSlip2.class) == true) {
            getWorld().showText("That's wrong!", 100,100);
    } 
 }
}
danpost danpost

2021/4/19

#
Problem 1 -- improper use of mousePressed: it requires a World or Actor instance for its parameter, not a class. Change "???.class" in line 11 of Customer class and line 12 of both OrderSlip1 and OrderSlip2 classes, to "this". Problem 2 -- inheritance: you do not have to re-code the same code in an extending class. That is, you can have the following class codes: BURGER (complete class codes):
public class Burger extends OrderSlip1 {}
BURGERWRONG (complete class codes):
public class BurgerWrong extends OrderSlip2 {}
REDCUSTOMER (complete class codes):
public class RedCustomer extends Customer {}
calling spawnOrderSlip from act method of Customer class. At line 11 of Customer class, you will probably want to add the additional condition that no order slips are yet in the world. I do not yet see where pizza is getting involved.
oofitsme oofitsme

2021/4/19

#
danpost wrote...
Problem 1 -- improper use of mousePressed: it requires a World or Actor instance for its parameter, not a class. Change "???.class" in line 11 of Customer class and line 12 of both OrderSlip1 and OrderSlip2 classes, to "this". Problem 2 -- inheritance: you do not have to re-code the same code in an extending class. That is, you can have the following class codes: BURGER (complete class codes):
public class Burger extends OrderSlip1 {}
BURGERWRONG (complete class codes):
public class BurgerWrong extends OrderSlip2 {}
REDCUSTOMER (complete class codes):
public class RedCustomer extends Customer {}
calling spawnOrderSlip from act method of Customer class. At line 11 of Customer class, you will probably want to add the additional condition that no order slips are yet in the world. I do not yet see where pizza is getting involved.
Using pizza was just part of the example because I have multiple actors under the class OrderSlip1 and one of them happens to be pizza. I originally used "this" for the Greenfoot mouse-click method but where it gets confusing is I hardcoded each actor from the OrderSlip1 and 2 class to be associated with a specific person from the customer class (if that makes sense). And when I run the game, instead of showing up the way that I hardcoded it to be, the order tickets show up with different customers than intended. For example: The Red Customer is hardcoded to order a burger but instead when happens is when I click the customer, a different order slip from the class shows up, such as Pizza or whatever food. Here are some pictures if that will help explain it: https://photos.app.goo.gl/GJwRddKUXUC9saTQA My theory is that each time mouse-click is initiated, it just picks the first available script?
danpost danpost

2021/4/20

#
oofitsme wrote...
Using pizza was just part of the example because I have multiple actors under the class OrderSlip1 and one of them happens to be pizza. I originally used "this" for the Greenfoot mouse-click method but where it gets confusing is I hardcoded each actor from the OrderSlip1 and 2 class to be associated with a specific person from the customer class (if that makes sense). And when I run the game, instead of showing up the way that I hardcoded it to be, the order tickets show up with different customers than intended.
I suggest you use fields for the slips of a customer. That is, in Customer class:
protected OrderSlip1 rightSlip;
protected OrderSlip2 wrongSlip;
then, assign them in the extending classes. For example:
public class RedCustomer extends Customer
{
    public RedCustomer()
    {
        rightSlip = new Burger();
        wrongSlip = new BurgerWrong();
    }
}
Now, back in Customer class, change "new OrderSlip1()" to "rightSlip" and "new OrderSlip2()" to "wrongSlip".
My theory is that each time mouse-click is initiated, it just picks the first available script?
No sure what "first available" entails.
You need to login to post a reply.