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/18

#
1
2
3
4
5
6
if (container == null && Greenfoot.isKeyDown("p"))
{
    move(getImage().getWidth()/2);
    container = (Container7) getOneObjectAtOffset(0, 0, Container7.class);
    move(-getImage().getWidth()/2);
}
This should get the container at the edge of the image in the direction it is facing.
y0us y0us

2014/1/18

#
Thanks a lot , it works fine now. But when i drop it I want the container to stay on that position, now it returns back to the place on the ship. How could I fix that?
danpost danpost

2014/1/18

#
Once the 'container' field of the crane object becomes 'null', the crane should no longer control the container in question. Therefore, the answer most probably resides in your Container7 class (or a superclass of it). That is, of course, unless you have something else altering the position of the containers (whether on purpose or not)..
y0us y0us

2014/1/19

#
I dont think you understand my problem, its if you can help me with a code that drops the container on that particular position when I press "d" . I want the containers to stack on the truck so it can drive away to the storage, not to return to the place it was picked from.
danpost danpost

2014/1/19

#
I understand completely. Show the Container7 class code.
y0us y0us

2014/1/19

#
I already posted the The Container7 class code and it is unmodified,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import greenfoot.*;  
   
   
public class Container7 extends Container1 
     Ship1 ship; 
       
    public Container7() 
    
           
   
           
    
       
    public Container7 (Ship1 s) 
    {  
           
        ship = s; 
           
           
    
       
    public void act()  
       
    
        int x = ship.getX()-18
        int y = ship.getY()-38
       setLocation(x, y); 
    }     
        
       
}
danpost danpost

2014/1/19

#
The act method in the Container7 class is moving the container back to the ship. You need to put a condition on that codes execution. Maybe set 'ship' to 'null' when the container is picked up and have the condition be if 'ship' is not 'null'.
y0us y0us

2014/1/19

#
I dont really get your point. Do you mean something like this? if(schip != null) { schip = null; } Im a beginner so please bear with me :)
danpost danpost

2014/1/19

#
It is the code within the act method of the Container7 class that needs to be conditionally executed. The setting of the 'ship' field to 'null' should be done in the Crane class when the container is picked up.
y0us y0us

2014/1/19

#
1
2
3
4
5
6
7
8
9
10
public void act()
     
    {
       if(ship != null)
       {
           int x = ship.getX()-18;
           int y = ship.getY()-38;
       setLocation(x, y);
    }
      
Like this? And excuse me for my noobness but there isnt a single word 'ship' in the Crane class.
y0us y0us

2014/1/19

#
Also, does it have something to do with this part of the Crane class?
1
2
3
4
5
if (container != null && Greenfoot.isKeyDown("d")) { 
   
            container = null;   
        }   
        
danpost danpost

2014/1/19

#
y0us wrote...
1
2
3
4
5
6
7
8
9
10
public void act()
     
    {
       if(ship != null)
       {
           int x = ship.getX()-18;
           int y = ship.getY()-38;
       setLocation(x, y);
    }
      
Like this? And excuse me for my noobness but there isnt a single word 'ship' in the Crane class.
Yes.
danpost danpost

2014/1/19

#
y0us wrote...
Also, does it have something to do with this part of the Crane class?
1
2
3
4
5
if (container != null && Greenfoot.isKeyDown("d")) { 
   
            container = null;   
        }   
        
No. But close. The "d" code is for dropping off, not for picking up.
danpost danpost

2014/1/19

#
And as far as your Crane class not having a single world 'ship' in it is not relevant. Your Container7 class has a 'ship' field which you just used to conditionally execute the act method of that class. Now the value of that field needs to be made 'null' at the appropriate time -- when the crane picks up a container. Again, the container object that the crane picks up will have the 'ship' field set to the ship and needs to be set to 'null' at that time.
y0us y0us

2014/1/19

#
1
2
3
4
5
6
7
8
9
public void act()
     
    {
       if(schip != null)
       {
       int x = schip.getX()-18;
       int y = schip.getY()-38;
       setLocation(x, y);
    }
Now I adjusted the code correctly, why doesnt it work yet? The container still goes to the ship's given X and Y coordinates. And the last code I posted was indeed about dropping it of with "d". Actually I meant if it has something to do with the body of the if statement "container = null"
There are more replies on the next page.
1
2
3