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

2015/6/1

returning strings and setting them as strings in other classes?

h123n h123n

2015/6/1

#
Hi, basically I want to return the inventory of whoever you walk into (person) and then save that inventory into the class that touched it. (toby) but I don't really understand how to return strings. Where do they get saved once returned? How do I save them as another String? My code for Toby is bellow:
import greenfoot.*;

/**
 * An demo class which is meant to be a camera follower.
 * It moves to face your mouse cursor, and it can move
 * back and forward.
 * 
 * @author Sven van Nigtevecht
 * @version 1.0
 */
public class Toby extends ScrollActor
{
    /** The number of cells we move forward and backword */
    int speed = 5;
    int lastkey = 1;
    Guard guard;
    String inventory1 = "Empty";
    String inventory2 = "Empty";
    
    /**
     * Move to face the mouse,
     * and listen to the up and down keys.
     */
    public void act()
    {
        if (Greenfoot.isKeyDown("a")) {
           Block block = (Block)getOneIntersectingObject(Block.class);
           if (block != null)
           {
                Collide();
           }
           else
           {
                lastkey = 2;
                StopCollide();
                getWorld().setCameraLocation(getWorld().getCameraX() -speed,getWorld().getCameraY());
           }
        }
        
        if (Greenfoot.isKeyDown("d")) {
            Block block = (Block)getOneIntersectingObject(Block.class);
            if (block != null)
            {
                Collide();
            }
            else
            {
                lastkey = 4;
                StopCollide();
                getWorld().setCameraLocation(getWorld().getCameraX() +speed,getWorld().getCameraY());
            }
        }
            
         
        if (Greenfoot.isKeyDown("w")) {
            Block block = (Block)getOneIntersectingObject(Block.class);
            if (block != null)
            {
                Collide();
            }
            else
            {
                lastkey = 1;
                StopCollide();
                getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() -speed);
            }
        }
        if (Greenfoot.isKeyDown("s")) {
            Block block = (Block)getOneIntersectingObject(Block.class);
            if (block != null)
            {
                Collide();
            }
            else
            {
                lastkey = 3;
                StopCollide();
                getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() +speed);
            }
        }
        Guard findGuard = (Guard)getOneIntersectingObject(Guard.class);
        if (findGuard != null)
        {
            guard = findGuard;
            guard.follow(this);
        }
        if (guard != null)
        {
            //guard.setRotation(this.getRotation());
            //guard.move(3);
        }
        Person person = (Person)getOneIntersectingObject(Person.class);
        if (person != null)
        {
                Collide();
        }
    }
    public void Collide()
    {
        speed = -1;
        if (lastkey == 1)
        {
            getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() -speed);
        }
        else if (lastkey == 2) 
        {
            getWorld().setCameraLocation(getWorld().getCameraX() -speed,getWorld().getCameraY());
        }
        else if (lastkey == 3)
        {
            getWorld().setCameraLocation(getWorld().getCameraX(),getWorld().getCameraY() +speed);
        }
        else
        {
            getWorld().setCameraLocation(getWorld().getCameraX() +speed,getWorld().getCameraY());
        }
    }
    public void InventoryShow()
    {
        Person person = (Person) getOneIntersectingObject(Enemy.class);
        if (enemy != null)
        {
            getWorld().person.
        }
        
    }
    public void StopCollide()
    {
        speed = 5;
    }
}
(By the way, My method for returning is in InventoryShow) Here is my Person code:
import greenfoot.*;

/**
 * Write a description of class Person here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Person extends NPC
{
    String inventory1 = "Empty";
    String inventory2 = "Empty";
    /**
     * Act - do whatever the Person 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.
    }    
}
All help is appreciated.
h123n h123n

2015/6/1

#
Never mind, I had some help and it's done now, thanks to anyone reading this.
You need to login to post a reply.