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
y0us y0us

2014/1/16

#
Dear fellow greenfooters, I want a container, to get fixed at the top of the crane when I click on a container. I've been trying many codes and Im really stuck. This is what I (with the help of another post on this forum) got by now. Whats wrong with this code?
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
public class Crane extends Actor
{
 
    private Container7 container; 
 
    public void act() { 
        if (!getIntersectingObjects(Container7.class).isEmpty()) { 
        
        if (container == null && Greenfoot.isKeyDown("p") && !getObjectsInRange(25, Container.class).isEmpty()) {
            container = (Container7) getObjectsInRange(25, Container7.class).get(0); 
        
        if (container != null && Greenfoot.isKeyDown("d")) {
 
            container = null
        
    
 
    public void setLocation(int x, int y) { 
 
        super.setLocation(x, y); 
        if (container != null) { 
            container.setLocation(x, y+5); 
 
        
    }
This screenshot might help in understanding the scenario: Thanks in advance
y0us y0us

2014/1/16

#
The purpose is to grab a container and rotate 180 ° to set it on the truck
Gevater_Tod4711 Gevater_Tod4711

2014/1/16

#
I think the problem is that when your crane rotates it doesn't move and therefor the setLocation method is not executed. So the container also is not moved. To make the containers move arround a rotationpoint you can use this method:
1
2
3
4
5
6
7
8
9
10
11
public static void rotateArroundPoint(Point rotationPoint, Actor rotatedActor, double angle) {
    double distance = getPointsDistance(rotationPoint, new Point(rotatedActor.getX(), rotatedActor.getY()));
    double[] newCoordinates = move(rotationPoint.getX(), rotationPoint.getY(), angle, (int) distance);
    rotatedActor.setRotation((int) angle);
    rotatedActor.setLocation((int) newCoordinates[0], (int) newCoordinates[1]);
}
 
 
public static double getPointsDistance(Point p1, Point p2) {
    return Math.hypot(p1.getX() - p2.getX(), p1.getY() - p2.getY());
}
You can use the methods like this:
1
2
3
4
5
6
7
8
9
//first you need to import java.awt.Point;
import java.awt.Point;
 
 
//in your crane class you need to call the method every time the crane is turned so the easyest way would be to override the setRotation method:
public void setRotation(int rotation) {
    super.setRotation(rotation);
    rotateArroundPoint(new Point(getX(), getY()), container, rotation);
}
y0us y0us

2014/1/16

#
Thanks for your reply, but I get this error:
y0us y0us

2014/1/16

#
Whoops, didnt add the error message in the screenshot. Also, you can rotate the crane (with the mouse) and move the crane to the left and right using the arrow keys at the same time.
Gevater_Tod4711 Gevater_Tod4711

2014/1/16

#
Oh sorry I forgot to post this method. Here is the move method you need:
1
2
3
4
5
6
7
public static double[] move(double currentX, double currentY, double angle, double distance) {
    double[] coordinates = new double[2];
    angle = Math.toRadians(angle);
    coordinates[0] = currentX + Math.cos(angle) * distance;
    coordinates[1] = currentY + Math.sin(angle) * distance;
    return coordinates;
}
When you also want to move the crane you should change the setLocation method like this:
1
2
3
4
5
6
public void setLocation(int x, int y) {
    int dx = x - getX() ;
    int dy = y - getY();
    super.setLocation(x, y);
    container.setLocation(container.getX() + dx, container.getY() + dy);
}
I think this should work.
y0us y0us

2014/1/16

#
Thanks for your help, although it compiles cleanly I wont get to see my world. I found when I comment the setLocation method it will show me the world. Also, can you tell me what should be put in act() en what not?
y0us y0us

2014/1/16

#
I get this message: java.lang.NullPointerException at Kraan.setLocation(Kraan.java:39) at greenfoot.Actor.addToWorld(Actor.java:486) at greenfoot.World.addObject(World.java:405) at Achtergrond.prepare(Achtergrond.java:86) at Achtergrond.<init>(Achtergrond.java:25) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at greenfoot.core.Simulation.newInstance(Simulation.java:581) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:409) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:468) at greenfoot.core.Simulation.maybePause(Simulation.java:281) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
danpost danpost

2014/1/17

#
Would it not be easier to just adjust to the position of the container (if picked up) at the end of each act method (instead of adjusting it when moved and when rotated). Also, there is a simple way to keep the held container at the correct location:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
{
    // previous code
    adjustContainerPosition()
}
 
private void adjustContainerPosition()
{
    if (container == null) return;
    container.setLocation(geX(), getY());
    container.setRotation(getRotation());
    container.move(getImage().getWidth()/2);
    container.setRotation(0);
}
This should keep the center of the container at the front edge of the image of the crane (in the direction the crane is facing). Also, all the non-act methods above can be eliminated.
y0us y0us

2014/1/17

#
Hi Danpost, why isnt this pick-up method (in the crane class) working?
1
2
3
4
5
6
7
8
9
10
11
12
public void pickUp()
{
         if (!getIntersectingObjects(Container7.class).isEmpty()) {   
        }   
        if (container == null && Greenfoot.isKeyDown("p") && !getObjectsInRange(25, Container7.class).isEmpty()) { 
            container = (Container7) getObjectsInRange(25, Container7.class).get(0);   
        }   
        if (container != null && Greenfoot.isKeyDown("d")) { 
   
            container = null;   
        }   
    }
danpost danpost

2014/1/17

#
Well, from what I can tell, you can remove lines 3 and 4 (it has an empty block, so will not do anything). Then, you named the method 'pickUp', but it does both 'pickUp' and 'dropOff'. Other than that, it looks ok. The problem must be elsewhere in the class (or in the container class). First show the entire Crane class as you have it now. Then if the problem cannot be found there, we can look at the Container7 class. Finally, if still at loss, we may have to look at the world class. You could either post those classes here or upload your scenario (with the 'Publish source code' checkbox checked) so that we can fully understand what is involved and come up with (hopefully) a solution quickly.
y0us y0us

2014/1/17

#
This is my Crane class:
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Point;
 
 
public class Crane extends Actor
{
     
   
 
  
      
   
 
    public void act()
     {
                 
         
    moveOnKeyPress();
    checkWall();
adjustContainerPosition();
    pickUpandOff();
    rotateCrane();
}
 
     
     private Container7 container; 
 
 public void pickUpandOff()
{
        if (container == null && Greenfoot.isKeyDown("p") && !getObjectsInRange(25, Container7.class).isEmpty()) { 
            container = (Container7) getObjectsInRange(25, Container7.class).get(1);   
        }   
        if (container != null && Greenfoot.isKeyDown("d")) { 
   
            container = null;   
        }   
    }
          
private void rotateCrane()
{
    MouseInfo mouse = Greenfoot.getMouseInfo(); 
    if (mouse != null) { 
        turnTowards(mouse.getX(), mouse.getY()+1); 
    
}
     
private void adjustContainerPosition() 
    if (container == null) return
    container.setLocation(getX(), getY()); 
    container.setRotation(getRotation()); 
    container.move(getImage().getWidth()/2); 
    container.setRotation(0); 
   
     
 
  public void checkWall(){
         if(getX() == 295) {
            setLocation(getX() -1, getY());
    else if(getX() == 244) {
            setLocation(getX() +1, getY());
    }
    }
     
 
 
    public void moveRight()
    {
        int x = (getX() + 1);
        int y = (getY());
         
        setLocation(x,y);
    }
    
    public void moveLeft()
    {
        int x = (getX() - 1);
        int y = (getY());
         
        setLocation(x,y);
    }
    
     
       public void moveOnKeyPress()
{
    if(Greenfoot.isKeyDown("left")) {
        moveLeft();
    }
     
    if(Greenfoot.isKeyDown("right")) {
        moveRight();
    }
     
   
}
}
danpost danpost

2014/1/18

#
Move line 20 to the end of the act method. In line 31, change 'get(1)' to 'get(0)'. Other than that, it appears to be ok. If you still cannot pickup/dropoff containers, we might need to look at a different class that may be influencing your results.
y0us y0us

2014/1/18

#
This is the Container 7 (the top left container, they are stacked on top of eachother)
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);
    }   
      
     
}
y0us y0us

2014/1/18

#
Oh, I know why it isnt working. Its the purpose that the crane stays on the dock, and with the end of the crane it should be picking up the container. Maybe thats why "p" isnt working now, cuz the middle point of the crane isnt "touching" the container. So, how can I make it so that the end of the crane, (the same point as where the container goes), has to touch the container in order to pick it up?
There are more replies on the next page.
1
2
3