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

2013/11/7

How to make object dissapear upon entering a certain location & more

Arrinao Arrinao

2013/11/7

#
Hello, I'm pretty much a fresh beginner to Greenfoot and recently have come across some problems which I seem not to be able to tackle on my own. First of all I have a problem with the turn() method. I don't want it to rotate my object in the direction of the move. I've tried the solutions written here http://www.greenfoot.org/topics/2318 but these methods seem to supersede the move method, i.e. the objects are made to go randomly around, and setRotation makes them go in certain direction:/ Secondly I'd like certain objects to dissapear at certain locations. The project I'm working on is a simulation of a restaurant in which waiters are moving and attend the tables. Waiter 0 looks for table 0 - if it reaches a table 0 (the square around it) it should dissapear (and change the table number to 1 in the process) and if it reaches table 1 or 2, it won't dissapear, but will have to avoid steping into that table. It should also be able to avoid another waiter (not step into him) Here's my code. I figured out the random movement in the world and the colision detection with it's borders, but the turning and dissapearing problem is just impenetrable for me atm.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;  // Potrebujeme pracovat se seznamem (java.util.List)

/*
 * Trida, predstavujici cisniky
 */
public class Waiter extends Actor
{    
    //Celkovy plat, reprezentovany jako pocet kol vsech cisniku
    private static Integer wages = 0;
    
    /*
     * Vynulovani statistik (pri vytvareni nove inhstance sveta)
     */
    public static void resetStatistics() {
        wages = 0;
    }
    
    /*
     * Vraci celkovy plat vsech cisniku
     */
    public static Integer getWages() {
        return wages;
    }

    //Typ cisnika
    private Integer type;

    // Cisnik je vytvaren primo z restaurace, ktera rozhoduje o jeho typu
    public Waiter(Integer type) {
        this.type = type;
        //Na disku jsou soubory waiter_0.png, waiter_1.png a waiter_2.png
        setImage("waiter_"+type+".png");
    }
 

    public void act(){
        move (1);
if (Greenfoot.getRandomNumber(100)<65)
{
    turn(Greenfoot.getRandomNumber(180)-90);
    } 
if (getX() <= 1 || getX() >= getWorld().getWidth() - 1)
{
    turn(45);
} 
if (getY() <= 1 || getY() >= getWorld().getHeight() - 1)
{ 
    turn (45);
} 
if ((getX() == 1) && (getY() == 1)){
    turn (180);
} 


if (Greenfoot.getRandomNumber(1000)==0) {
            ((BlueWorld)getWorld()).removeWaiter();
            getWorld().removeObject(this);
        } else {
           //TODO: Doplnit interakci s ostatnimi objekty (podle zadani)
        }
        
    wages++;        //penize za kazde kolo aktivniho cisnika
    moveAndTurn();
        //TODO: Doplnit pohyb cisnika
}
    
} 
    public void eat(){

        Actor waiter;
waiter = getOneObjectAtOffset(0,0, Actor.class);
if (waiter != null)
{World world=getWorld();
world.removeObject(waiter);
} 
}
}
davmac davmac

2013/11/8

#
I've tried the solutions written here http://www.greenfoot.org/topics/2318 but these methods seem to supersede the move method, i.e. the objects are made to go randomly around, and setRotation makes them go in certain direction:/
This is contradictory. Either "the objects go randomly around" or they "go in certain direction" (specified by setRotation) - it can't be both! I think what you're saying is that you want to be able to set the rotation of the graphic and the direction of movement independently. How about this:
    public void move(int distance)  
    {  
        int rot = getRotation();
        setRotation(myRotation);  
        super.move(distance);  
        setRotation(rot);      
    }

    public void setMoveDirection(int dir)
    {
        myRotation = dir;
    }
You need to declare 'myRotation' (an int) as well of course.
Arrinao Arrinao

2013/11/9

#
Hmm. But I still need to make them move randomly. How can I adjust it so they move random directions and yet keep the same image the whole time?
danpost danpost

2013/11/9

#
You can save the current 'rotation' in an instance field and adjust it randomly in the act. Then, when moving, rotate the actor to the current rotation, move, and return the rotation back to zero, thereby, keeping the original state of the image.
Arrinao Arrinao

2013/11/9

#
Example please? Keep in mind I'm pretty much a "noob" here and am yet quite bad at understanding what you are saying.
You need to login to post a reply.