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

2014/1/5

Let an object follow another object

1
2
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
Gevater_Tod4711 wrote...
I think the problem concerning the wrong intersecting are occours because you use the getOneObjectAtOffset mehtod. This method check whether there is a object at the position of the container. But that means only in the middle position of the container. If you use the method getOneIntersectingObject instead of getOneObjectAtOffset it should work.
Didn't this fix the problem?
UNDEAD_DC UNDEAD_DC

2014/1/9

#
If i replace that i get the following error:
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
The problem is that the getOneIntersectingObject method needs only one parameter. If you delete the two ints it should work.
UNDEAD_DC UNDEAD_DC

2014/1/9

#
yea that worked but now i have the problem that the container gets removed even more above it like te hight of the front part but then on the rear
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
That probably is because the image is as high as the front part. You could try to use a smaler image. Or you could use Busch2207's Pixel-perfect collision detection.
UNDEAD_DC UNDEAD_DC

2014/1/9

#
the image is same hight as the lowest point of the wheels till the top of the flat line
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
Ok thats a bit strange. You could also try to check whether the Y-coordinate of the container is greater than a minimum value to remove the container when it touches the trucks back. Therefor your need to change your container code like this:
public class Container extends Actor  
{  
    /** 
     * Act - do whatever the ContainerRes wants to do. This method is called whenever 
     * the 'Act' or 'Run' button gets pressed in the environment. 
     */  
      
      
    public void act()   
    {  
          
        // this spawns the containers wich X seconds between 2 containers randomly spwawned between 2 points  
        setLocation(getX(), getY()+5);  
        Actor back;  
        if (getY() > minimumValue) {//instead of minimumValue you put the right value here;
            back = getOneObjectAtOffset(0, 0, TrucksBack.class);  
            if (back != null) {  
                int ran = Greenfoot.getRandomNumber ( 500 );  
                getWorld().addObject( new Container(), ran, 0);  
                Greenfoot.playSound("misc209.wav");  
                getWorld().removeObject(this);  
            }
        }      
        Actor front;  
        front = getOneObjectAtOffset (0, 0, TrucksFront.class);  
        if (front != null) {  
            int ran = Greenfoot.getRandomNumber ( 500 );  
            getWorld().addObject( new Container(), ran, 0);  
            Greenfoot.playSound("misc209.wav");  
            getWorld().removeObject(this);  
        }      
    }      
}  
UNDEAD_DC UNDEAD_DC

2014/1/9

#
tried this. first with minimumValue as 0 then 5 10 25 50 100 250 500 1000 10000 50000000 (50.000.000 without points but to show: yes i tried 50 milion) then i tried these -0 -5 -10 -25 -50 -100 -250 -500 -1000 -10000 -50000000 (50milion again) not even a litle bit of change EDIT: Here i uploaded the scenario: clickclickclickclick EDIT2: The left and right now do react better but still to high EDIT3: YES!!!!! discovered the problem... had to remove this:
Actor front;    
        front = getOneIntersectingObject ( TrucksFront.class);    
        if (front != null) {    
            int ran = Greenfoot.getRandomNumber ( 500 );    
            getWorld().addObject( new Container1(), ran, 0);    
            Greenfoot.playSound("misc209.wav");    
            getWorld().removeObject(this);    
        }    
because that lets it interact with the front to and i guess that the front is not good sized EDIT4: Updated the Scenario, TY so much for help it is now interacting right with the back of the truck and also with the front of the truck only the line on top of the truck is not interacting correctly it still interacts like a box but later il try to fix that
UNDEAD_DC UNDEAD_DC

2014/1/22

#
Hi i have a new problem with this, i now want the back of the truck to go away (get removed) when the front gets removed, and that there can only be one back in the game at once.
Gevater_Tod4711 Gevater_Tod4711

2014/1/22

#
Is the trucks front removed from another object or from some code within the trucks front when you want to remove the back of the truck? When the front removes itselves you just need to execute getWorld().removeObject(back); before getWorld().removeObject(this); If it is removed by another object you will have to override the removeObject method in the world class like this:
public void removeObject(Actor object) {
    if (object instanceof Front) {
        ((Front) object).removeTrucksBack();
    }
    super.removeObject(object);
}


//now you just need the method removeTrucksBack in the Front class:
public void removeTrucksBack() {
    getWorld().removeObject(back);
}
UNDEAD_DC wrote...
and that there can only be one back in the game at once.
I don't realy get what you mean by this.
danpost danpost

2014/1/22

#
You could just put this in the world act method:
if (getObjects(TrucksFront.class).isEmpty()) removeObjects(getObjects(TrucksBack.class));
When adding the objects, however, you must then always add the TrucksBack object into the world first during play. Then, in the TrucksBack class, add the following method:
public void addedToWorld(World w)
{
    if (w.getObjects(TrucksBack.class).size() > 1) w.removeObject(this);
}
However, better is to check this before adding a TrucksBack object into the world.
You need to login to post a reply.
1
2