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

2017/3/16

Picking Things Up

bluebananas bluebananas

2017/3/16

#
Hi, I need to make a game for a school assignment (by tomorrow O.O), and I'm having a bit of trouble with my character. So the player, Bob, is supposed to pick up a lifesaver to save drowning people. There are two types of lifesavers: for kids and for adults. I'll have three of each type on each side of Bob. I want him to be able to pick up and drop the lifesavers with a single press of the spacebar (not hold, which is isKeyDown), and throw it at a drowning victim with a single press of the up button. I can manage the throwing part, but I can't make him pick things up without getting a terminal window with the error, "hasToRun = false". I need to finish this, please help!!!
danpost danpost

2017/3/16

#
Need to see the terminal error output with the code that created it (with no modifications after the output was created).
bluebananas bluebananas

2017/3/16

#
it's okay, I got it after a few hours of reasearch hahahah. My problem now is throwing it, because what I had in mind isn't exactly working out. What i need is for it to move up the screen continuously after a single press of the spacebar (so, not isKeyDown) until it hits something, or the edge. And here's the catch: the types of lifesavers can only save their respective victims, that is, adults and kids. How can I make the buoys move continuously upward, and detect what it touches? Sorry, if I seem dense, I'm desperate at this point hahah. Thanks!!
danpost danpost

2017/3/16

#
You will need to show the class codes of the buoys with attempted code at detecting adult or kid actors.
bluebananas bluebananas

2017/3/16

#
I already have an idea on the detection of touch. isTouching, right? My main problem is moving continuously. For Bob:
public int objectCarrying = 0;
    public void walk()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            move(-3);
        }
    }
    

    public void pickUpBuoyKids()
    {
		Actor buoykids = getOneObjectAtOffset(0, 0, BuoyKids.class);
		String key = Greenfoot.getKey();
		BuoyKids b = (BuoyKids) getOneIntersectingObject(BuoyKids.class);
		Actor buoykidstwo = getOneObjectAtOffset(0, 0, BuoyKidsTwo.class);
		BuoyKidsTwo btwo = (BuoyKidsTwo) getOneIntersectingObject(BuoyKidsTwo.class);
		if (buoykids != null)
		{
				if(key != null && key.equals("up") && objectCarrying == 0){
					getWorld().removeObject(buoykids);
					getWorld().removeObject(buoykidstwo);
					objectCarrying = 2;
				}			
		}
		if(key != null && key.equals("down") && objectCarrying == 2)
		{
			getWorld().addObject(new BuoyKids(), getX(), getY());
			getWorld().addObject(new BuoyKidsTwo(), getX(), getY());
			objectCarrying = 0;
		}
		if(key != null && key.equals("space") && objectCarrying == 2)
		{
			getWorld().addObject(new BuoyKidsTwo(), getX(), getY());
			objectCarrying = 1;
		}
	}
	
	public void throwBuoy()
	{
	    String key = Greenfoot.getKey();
	    if (key != null && key.equals("space") && objectCarrying ==1)
	    {
	        getWorld().addObject(new BuoyKidsTwo(), getX(), getY());
	        objectCarrying = 0;
	    }
	}

    public void act() 
    {
        walk();
        pickUpBuoyKids();
        walk();
        throwBuoy();
    }    
BuoyKids is the image being picked up and dropped, so there's nothing in it. I made it separate from the object going to be moved. for BuoyKidsTwo which is the image that is supposed to move:
	public void hagis() //hagis is filipino for throw
	{
		if(objectCarrying == 1)
		{
			getX();
			getY();
			setLocation(getX(), getY()+10); //it's supposed to move up
		}
	}

    public void act() 
    {
        hagis();
    }    
I need help ASAP, thank you!!!!!!!!!!!
Super_Hippo Super_Hippo

2017/3/17

#
Have an int field.
private int direction = 0;
Then, when you press a key, you set direction to +1 or -1 (depends on which key). At the end, when moving, you multiply with direction. Maybe you also need a way to set direction back to 0.
bluebananas bluebananas

2017/3/17

#
I'm sorry I seem stupid, but what do you mean?? I'm sorry, I only started recently, I can't grasp so many things going on at once. I just need to make the buoy move up once, without having to hold a key. A single press of the spacebar should make the buoy move, and I've sort of tried using
 private int speed = 5;
        private void move()
        {
            if ((speed > 0 && getX() == getWorld().getWidth()-1) ||
            (speed < 0 && getX() == 0)) speed = -speed;
            move(speed);
        }   
        public void act()
        {
            move();
        }
    
which I used for the drowning victims, but even if I changed the getY() it won't work.
Super_Hippo Super_Hippo

2017/3/17

#
private int direction=0, speed=5;

public void act()
{
    if (Greenfoot.isKeyDown("space")) direction = -1;
    setLocation(getX(), getY()+direction*speed);
}
bluebananas bluebananas

2017/3/17

#
IT WORKED!!!! THANK YOU SO MUCH YOU JUST SAVED MY GRADES BLESS ALL YOUR SOULS
bluebananas bluebananas

2017/3/17

#
HI! So as I said earlier, it worked. However, now I only want it to get thrown AFTER it is picked up. With these codes: Bob:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bob here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bob extends Actor
{
    /**
     * Act - do whatever the Bob wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void walk()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            move(3);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            move(-3);
        }
    }
    
	public int objectCarrying = 0;
    public void pickUpBuoyKids()
    {
		Actor buoykids = getOneObjectAtOffset(0, 0, BuoyKids.class);
		String key = Greenfoot.getKey();
		BuoyKids b = (BuoyKids) getOneIntersectingObject(BuoyKids.class);
		if (buoykids != null)
		{
				if(key != null && key.equals("space") && objectCarrying == 0){
					getWorld().removeObject(buoykids);
					objectCarrying = 1;
				}			
		}
		if(key != null && key.equals("up") && objectCarrying == 1){
			getWorld().addObject(new BuoyKids(), getX(), getY());
			objectCarrying = 0;
		}
	}

    public void act() 
    {
        walk();
        pickUpBuoyKids();
        walk();
    }    
}
BuoyKids:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BuoyKids here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BuoyKids extends Bob
{
    /**
     * Act - do whatever the BuoyKids wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
	
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    private int direction=0, speed=5;
	public void hagis()
	{
		{
    		if (Greenfoot.isKeyDown("up")) direction = -1;
    		setLocation(getX(), getY()+direction*speed);
		}
		if(atWorldEdge())
		{
			getWorld().removeObject(this);
		}
		else if (isTouching(DrowningKids.class))
		{
			getWorld().removeObject(this);
		}
	}

    public void act() 
    {
		hagis();
    }    
}
If I press the "up" for throw before even picking it up, the buoy moves, which I don't want. Thanks again for helping!!!
Super_Hippo Super_Hippo

2017/3/17

#
I think you either have to control the BuoyKids from the Bob which is throwing it or change a variable as a condition for the hagis method in the BuoyKids object when releasing it. I doubt that BuoyKids should be a subclass of Bob.
You need to login to post a reply.