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

2014/1/20

How to apply a method to all the subclasses

1
2
y0us y0us

2014/1/20

#
Hi, I want the Crane class to drop ALL the containers (they are numbered, and Container.class is the superclass) on the direction the crane is facing. Right now only one (Container.class) works correctly. All the other containers return to their place after I hit "d" cuz they have different act methods (they stay on different X and Y coordinates on the ship). Can somebody help me out so that the drop method doesnt only apply to one container but the other subclasses as well? This is the pickUpAndDrop method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void pickUpandDrop() 
        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;     
        }     
           
               
    }
danpost danpost

2014/1/21

#
You have something like the following act method in each of your container classes:
1
2
3
4
5
6
7
8
9
public void act()
{
    if (ship != null)
    {
    int x = ship.getX()-18;
    int y = ship.getY()-38;
    setLocation(x, y);
    }
}
By adjusting the code so that you use fields for the offsets from ship's center to where the container needs to be, like so (this would be done in all the container classes; but, do not actually do this):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int shipXoff, shipYoff;
 
public Container7()
{
    shipXoff = -18;
    shipYoff = -38;
}
 
public void act()
{
    int x = ship.getX()+shipXoff;
    int y = ship.getY()+shipYoff;
    setLocation(x, y);
}
then,the act methods will have the exact same code. The act methods with the field declarations for the offsets can then be removed from all the container classes except the Container class (which is super to all the others). All in all, what needs done is (1) add the constructors (lines 3 through 7 above, using the appropriate class names)to all the container classes, removing all other code from all Container sub-classes (do not remove any code from the Container class), (2) add the fields into the Container class and change the hardcoded offsets in its act method to the appropriate field names.
y0us y0us

2014/1/21

#
Ok,now I have changed all the act methods of all the subclasses to :
1
2
3
4
5
6
7
8
9
  public void act() 
    if(ship != null
       
    int x = ship.getX()+shipXoff; 
    int y = ship.getY()+shipYoff; 
    setLocation(x, y);
}
}
Now, the crane method only picks up the Container.class (the superclass). How can I change it so the crane picks up all the containers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  public Container container; 
 
public void pickUpandDrop()   
{
            
        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;       
        }       
             
                 
    }   
danpost danpost

2014/1/21

#
y0us wrote...
Ok,now I have changed all the act methods of all the subclasses to :
1
2
3
4
5
6
7
8
9
  public void act() 
    if(ship != null
       
    int x = ship.getX()+shipXoff; 
    int y = ship.getY()+shipYoff; 
    setLocation(x, y);
}
}
danpost wrote...
All in all, what needs done is (1) add the constructors (lines 3 through 7 above, using the appropriate class names)to all the container classes, removing all other code from all Container sub-classes (do not remove any code from the Container class), (2) add the fields into the Container class and change the hardcoded offsets in its act method to the appropriate field names.
danpost danpost

2014/1/21

#
An example container subclass should look like this:
1
2
3
4
5
6
7
8
public class Container7 extends Container
{
    public Container7()
    {
        shipXoff = -18;
        shipYoff = -38;
    }
}
All the other subclasses should look similar.
y0us y0us

2014/1/21

#
I get this error then:
danpost danpost

2014/1/21

#
Somewhere in your project you are creating a new Container7 object using 'new Container7()' -- without a Schip1 object given.
danpost danpost

2014/1/21

#
danpost wrote...
An example container subclass should look like this:
1
2
3
4
5
6
7
8
public class Container7 extends Container
{
    public Container7()
    {
        shipXoff = -18;
        shipYoff = -38;
    }
}
All the other subclasses should look similar.
Forgot about the 'Schip1 object. And, by the way, this should be the entire class:
1
2
3
4
5
6
7
8
9
public class Container7 extends Container
{
    public Container7(Schip1 s)
    {
        schip = s;
        shipXoff = -18;
        shipYoff = -38;
    }
}
with all other subclasses similar.
y0us y0us

2014/1/21

#
Hero
y0us y0us

2014/1/22

#
Can you help me out with another problem, about that the container stays on the truck when it drives away.
danpost danpost

2014/1/23

#
You need to create a link between the containers and the truck; similar to the link created between the containers and the ship. From what I can tell, the ship does not rotate any; and probably neither does the truck. If that is true, then a more simple way can be used to link them provided there is no chance that an unloaded container could intersect the truck as it rolls off.
1
2
3
4
5
6
7
8
9
if (moving) // whatever the condition is that says that the truck is moving
{
    setLocation(getX(), getY()-1); // move truck whichever direction
    for (Object obj : getIntersectingObjects(Container.class))
    {
        Actor actor = (Actor)obj;
        actor.setLocation(actor.getX(), actor.getY()-1); // same as truck
    }
}
This should move all containers that intersect the truck the same way the truck moves.
y0us y0us

2014/1/23

#
It works now but it shoots very fast to the left, and I want it to go a bit slower. How can I adjust the speed of both of them?
danpost danpost

2014/1/23

#
y0us wrote...
It works now but it shoots very fast to the left, and I want it to go a bit slower. How can I adjust the speed of both of them?
Hard to say without the Truck class code (I presume that is one of 'both' since that is what was last worked on).
y0us y0us

2014/1/23

#
There is nothing in the Truck class yet, everything is defined in the container class.
1
2
3
4
5
6
7
8
9
10
import greenfoot.*; 
 
public class Truck extends Actor
{
     
    public void act()
    {
        // Add your action code here.
    }   
}
This is the container class:
1
2
3
4
5
6
7
8
9
10
11
public void act()
    {
        if ( hitTruck() && Greenfoot.isKeyDown("d"))
        
            setLocation(getX()-30, getY());  
            for (Object obj : getIntersectingObjects(Truck.class)) 
            
                Actor actor = (Actor)obj; 
                actor.setLocation(actor.getX()-30, actor.getY());
            
        }
davmac davmac

2014/1/23

#
danpost wrote...
Somewhere in your project you are creating a new Container7 object using 'new Container7()' -- without a Schip1 object given.
I believe what the error is actually saying is that the Container class has a constructor taking a Schip1 argument, but the Container7 constructor isn't calling that constructor. The correct code for the Container7 class might be, for example:
1
2
3
4
5
6
7
8
9
public class Container7 extends Container 
    public Container7(Schip1 s) 
    
        super(s);
        shipXoff = -18
        shipYoff = -38
    
It is difficult to be certain without seeing the code for the Container class, however.
There are more replies on the next page.
1
2