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

2014/1/16

Grab and hold objects

1
2
3
danpost danpost

2014/1/19

#
Well, the 'ship' field in the container being picked up needs to be set to null in the 'if' block that has 'Greenfoot.isKeyDown("p")'. That would be the 'if' block that deals with picking up a container. The 'container' field of the crane object is set to that container being picked up within that 'if' block. Just add a statement to change the value of the 'ship' field of that container to 'null' there. Make sure that there is actually a container there before trying to set the field (it could be 'null' itself).
y0us y0us

2014/1/19

#
Can you please provide me a code of which you are trying to explain I'm completely lost at this point
danpost danpost

2014/1/19

#
if (container == null && Greenfoot.isKeyDown("p"))
{
    move(getImage().getWidth()/2);
    container = (Container7) getOneObjectAtOffset(0, 0, Container7.class);
    move(-getImage().getWidth()/2);
    if (container != null) container.ship = null; // add this line here
}
There. Now you have the word 'ship' in your Crane class.
y0us y0us

2014/1/19

#
Thanks a lot for your effort
y0us y0us

2014/1/19

#
Can you please help me out with another thing? Now the Crane just picks up Container 7, (which was just for an testing purpose of the picking up mechanism), but I want the crane to be able to pick up all the (12) containers on the ship. Also, the crane is going under the truck despite the addObject of the Crane is above the one of the truck in the World class. Thanks in advance
danpost danpost

2014/1/19

#
What class is it that the 12 container classes extend? Use the 'setPaintOrder' method of the 'World' class.
y0us y0us

2014/1/19

#
All the containers come from the superclass "Container1".
danpost danpost

2014/1/19

#
Then change all occurrences of 'Container7' to 'Container1'. That way all containers will be detected (because all objects belonging to subclasses of a class are considered objects of that class). However, properly coded, the Container1 class should be a separate class, siblings (at the same level) with the other container classes.
y0us y0us

2014/1/19

#
But then I get the error message: "A class or package with this name already exists in this project. You cannot have two classes or packages with the same name"
danpost danpost

2014/1/19

#
Maybe I should have qualified where to make the changes -- in the Crane class when picking up a container.
y0us y0us

2014/1/19

#
public void pickUpandOff()
{
        if (container == null && Greenfoot.isKeyDown("p"))                            
    {  
        move(getImage().getWidth()/2);  
        container = (Container) getOneObjectAtOffset(0, 0, Container.class);  
        move(-getImage().getWidth()/2);  
        if (container != null) container.ship = null;
    }  
        
        if (container != null && Greenfoot.isKeyDown("d")) {  
            
            container = null;    
        }    
        
            
    }
If I do it like this, and add if(ship != null) { to the act method of every container, still only the top left container (7) works well and the rest moves back to the ship. Does it have to do with the line: container = (Container) getOneObjectAtOffset(0, 0, Container.class); because its about only one object ? (Container.class)
danpost danpost

2014/1/20

#
Since all the containers use the same controls and they all inherit the state (fields) and behavior (methods) from the Container1 class, just move the act method to the Container1 class (and remove them from all the other. NOTE: You should be able to remove all code from all container classes (except declaring them) and just use the Container1 class code for all the containers. For example:
// if I had the following class
import greenfoot.*;

public class Enemy extends Actor
{
    boolean active;

    public void act()
    {
        if (active) setLocation(getX()-1, getY());
        if (getX() == 0) getWorld().removeObject(this);
    }

    public void setActive()
    {
        active = true;
    }

    public void setInactive()
    {
        active = false;
    }
}
// with these subclasses
public class Enemy1 extends Enemy
{
}
// and
public class Enemy2 extends Enemy
{
}
// and
public class Enemy3 extends Enemy
{
}
Then any enemy created from any of the classes will move left across the screen when active is true and you can call 'setActive' and 'setInactive' on any of those enemys. The method and the fields from the Enemy class are given to use to all the actors created from any of the enemy subclasses (as well as those created from the Enemy class itself, of course).
y0us y0us

2014/1/20

#
But the act methods of the containers are not the same, all the containers have different X and Y coordinates that they hold on on the ship. For example, Container1 has:
public void act() 
    {
         if(ship != null)
       { 
       int x = ship.getX()-18;
       int y = ship.getY()-38;
       setLocation(x, y);
    }
and container2:
 public void act() 
    {
       if(ship != null)
       { 
        int x = ship.getX()-18;
       int y = ship.getY()+3;
       setLocation(x, y);
    }    
}
You need to login to post a reply.
1
2
3