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

2014/1/5

#
For school i am making a game and i have the following problem: I have a truck that is made out of 2 parts the front and the back Rear part Front part And this is how i want it to look like in greenfoot: in the game there will fall objects from above there will be containers wich you must catch with the rear of your truck if you "catch" it with the front part of the truck you will loose one of 3 lives Now i want to know: how do i make the rear end follow the front or the front follow the rear? If the truck catches a container or gets hit by a container on the front the container disapears same for containers that get missed and fall on the ground
danpost danpost

2014/1/5

#
Look at my Horiz/Vert/Univ Scroller which has a buggy and wheels that must stay with the buggy. The same principle can be applied here.
UNDEAD_DC UNDEAD_DC

2014/1/5

#
Sorry I dont understand what i need to put where :( i was hoping i could just put TruckPlayerCab X = TruckPlayerBase X TruckPlayerCab Y = TruckPlayerBase Y+5 I know it is not good code and wont work if i put this in but i want to show what I mean
UNDEAD_DC UNDEAD_DC

2014/1/6

#
Please i really don't know what to do.... I feel that there is a very easy solution but I cannot code it. I just want to let the front have the same y and x+5 as the back

Gevater_Tod4711 Gevater_Tod4711

2014/1/6

#
If your truck is just moving left and right and not up and down hills the solution should be easy. You just need to have a reference in the front of the truck that referes to the back. Now you add the moving code in the front part of the truck so that only the front is controlled by the player. Then you just have to override the setLocation method to make the back of the truck follow the front:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//in the front part class:
private TrucksBack back;
 
public TrucksFront() {
    back = new TrucksBack();//construct a new back and keep the reference;
}
 
public void addedToWorld(World world) {
    getWorld().addObject(back, getX() - 15, getY() + 5);//add the back of the truck to the world;
    //the values -15 and +5 are just guessed; that will probably not be the right values;
    //you'll have to try what is the right value so that the image looks like a complete truck;
}
 
public void setLocation(int x, int y) {
    super.setLocation(x, y);
    back.setLocation(x - 15, y + 5);//again just guessed;
}
Using this code the trucks back will always be moved when the front is moved. And because of the different coordinates (-15|+5) it should look like the truck is only one object. Edit: Oh I realy should read more precise what you said. Instead of the -15 and +5 you can use -5 and 0.
UNDEAD_DC UNDEAD_DC

2014/1/6

#
Gevater_Tod4711 wrote...
If your truck is just moving left and right and not up and down hills the solution should be easy. You just need to have a reference in the front of the truck that referes to the back. Now you add the moving code in the front part of the truck so that only the front is controlled by the player. Then you just have to override the setLocation method to make the back of the truck follow the front:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//in the front part class:
private TrucksBack back;
 
public TrucksFront() {
    back = new TrucksBack();//construct a new back and keep the reference;
}
 
public void addedToWorld(World world) {
    getWorld().addObject(back, getX() - 15, getY() + 5);//add the back of the truck to the world;
    //the values -15 and +5 are just guessed; that will probably not be the right values;
    //you'll have to try what is the right value so that the image looks like a complete truck;
}
 
public void setLocation(int x, int y) {
    super.setLocation(x, y);
    back.setLocation(x - 15, y + 5);//again just guessed;
}
Using this code the trucks back will always be moved when the front is moved. And because of the different coordinates (-15|+5) it should look like the truck is only one object. Edit: Oh I realy should read more precise what you said. Instead of the -15 and +5 you can use -5 and 0.
Thank you so much and about the -15 -5 or 0 that was just a example I will try later this day when i am on my Computer again (i am now working on my phone)
UNDEAD_DC UNDEAD_DC

2014/1/7

#
Ok Thanks i have this working now, but now i have the following problem, when a container drops if just falls down but when it touches the truck it does not interact the way i want it. it does not interact on the left side and on the right side it interacts to fast. Maybe this is more clear:
Gevater_Tod4711 Gevater_Tod4711

2014/1/7

#
Could you post the code of your truck and the container? Without knowing the code it's difficult to find the problem.
UNDEAD_DC UNDEAD_DC

2014/1/7

#
code of the TrucksBack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class TrucksBack here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TrucksBack extends Truck
 
{
    /**
     * Act - do whatever the TrucksBack wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
}
Code of the TrucksFront
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class TrucksFront here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class TrucksFront extends Truck
{
    /**
     * Act - do whatever the TrucksFront wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
         //sets button for movement of truck and speed and direction
        if (Greenfoot.isKeyDown("left") ) {
            setLocation (getX() - 2, getY() );
        }
        if (Greenfoot.isKeyDown("right") ) {
            setLocation (getX() + 2, getY() );
        }
    
        //in the front part class: 
    public TrucksBack back; 
       
    public TrucksFront() { 
        back = new TrucksBack();//construct a new back and keep the reference; 
    
       
    public void addedToWorld(World world) { 
        getWorld().addObject(back, getX() - 5, getY() - 1);//add the back of the truck to the world; 
        //the values -15 and +5 are just guessed; that will probably not be the right values; 
        //you'll have to try what is the right value so that the image looks like a complete truck; 
    
       
    public void setLocation(int x, int y) { 
        super.setLocation(x, y); 
        back.setLocation(x - 1, y - 1);//again just guessed; 
    
}
and code of the container
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class ContainerRes here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
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;
        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);
        }   
    }   
}
I also have another problem, when i use the code like above it gives terminal window with this error java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Container.act(Container.java:31) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) but when i change code to this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void act()
    {
         
        // this spawns the containers wich X seconds between 2 containers randomly spwawned between 2 points
        setLocation(getX(), getY()+5);
        Actor back;
        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);
        }   
    }   
it interacts with back but when i change the disable so like this
1
2
3
4
5
6
7
8
//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);
        //}
Then it does the same as the one above this how can i make it that when the container touches the back it adds a point and when it touches the ground or the front of the truck you loose a life.
Gevater_Tod4711 Gevater_Tod4711

2014/1/7

#
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. The IllegalStateException occours because when your container finds the trucks back it gets removed (line 28) and after it is removed from the world you try to get information about the position in the world (because the getOneObjectAtOffset method needs the position of the object in the world). But because the object is not in the world and it has no position in the world you get the exception. You can solve this problem by changing the act method of your container like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public void act()  
    
           
        // this spawns the containers wich X seconds between 2 containers randomly spwawned between 2 points 
        setLocation(getX(), getY()+5); 
        Actor back; 
        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); 
        }     
        if (getWorld() != null) {
            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); 
            }
        }     
    }
Now the container checks if it is still in the world before executing the getOneObjectAtOffset method. To add points to a score or make the truck loose a life you need a score counter and a health counter. I think both should be added in the world class. The score counter is a default class in Greenfoot and can be included in your scenario. Therefore you have to click on the menu item Edit in your scenario, select Import class and import the counter class. You can also use this counter for the health of your truck. You just need to increatse or decrease the health or the score of this counters when your container touches the trucks front or the back. (To check whether the container touches the earth you can just check it's y coordinate).
UNDEAD_DC UNDEAD_DC

2014/1/9

#
I got the container working now. Only still the hitbox is not correct yet. How do i fix it? dont know if it is because of the container or because of the Truck Front or the truck back. havent changed any code beside what you posted I also have the following new 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
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MovingObjects here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MovingObjects extends Actor
{
    int speed = 5;
    int fallSpeed = 5;
    int ranCont = Greenfoot.getRandomNumber ( 10 );
    int score;
    public void driveSpeedLeft()
    {
        move(-speed);
    }
    public void driveSpeedRight()
    {
        move(speed);
    }
    public void dropSpeed()
    {
        move(fallSpeed);
    }
    public void selectDropObject()
    {
        if (ranCont != 10)
        {
            int ran = Greenfoot.getRandomNumber ( 500 );
            getWorld().addObject( new Container2(), ran, 0);
            getWorld().removeObject(this);
        }
        if (ranCont <9)
        {
            int ran = Greenfoot.getRandomNumber ( 500 );
            getWorld().addObject( new Container(), ran, 0);
            getWorld().removeObject(this);
        }
    }
    public void Container()
    {
        setLocation(getX(), getY()+5);
        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);
            selectDropObject();
        }   
    }
    public void Container2()
    {
        setLocation(getX(), getY()+5);
        Actor front;
        front = getOneObjectAtOffset (0, 0, TrucksFront.class);
        if (front != null) {
            //int ran = Greenfoot.getRandomNumber ( 500 );
            //getWorld().addObject( new Container2(), ran, 0);
            Greenfoot.playSound("misc209.wav");
            //getWorld().removeObject(this);
            selectDropObject();
        }   
    }
    /**
     * Act - do whatever the MovingObjects wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }   
     
}
problem is i want a random generated number wich can be 1/2/3/4/5/6/7/8/9/10 when random number is 10 it must spawn Container2 and when not 10 it must spawn Container later Container2 will be "evil" containers that makes you loose a live when you catch and Container will be good Container. but when i run it i get this error
1
2
3
4
5
6
7
8
java.lang.NullPointerException
    at MovingObjects.selectDropObject(MovingObjects.java:38)
    at MovingObjects.Container2(MovingObjects.java:65)
    at Container2.act(Container2.java:18)
    at greenfoot.core.Simulation.actActor(Simulation.java:568)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
    at greenfoot.core.Simulation.runContent(Simulation.java:215)
    at greenfoot.core.Simulation.run(Simulation.java:205)
in the Container and Container2 only stands
1
2
3
4
5
6
7
8
9
10
11
12
public class Container2 extends MovingObjects
{
    /**
     * Act - do whatever the Container2 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
        Container2();
    }   
}
with Container(); on Container and Container2(); with Container2
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
The problem is that the value of the variable ranCont will always be different from 10. Greenfoot.getRandomNumber(10) returns a value from 0 - 9 not from 1 - 10. So the code in lines 31 to 33 is always executed when you call the method selectDropObject(). If ranCont is less than 9 the second if clause is also executed and the getWorld method is called in line 38. But because your object was removed getWorld returns null and you get the NullPointerException. To fix this you could change line 35 of your moving objects class to: if (ranCont < 9 && getWorld() != null)
UNDEAD_DC UNDEAD_DC

2014/1/9

#
And how can I fix the problem with the hitbox of the truck?
Gevater_Tod4711 Gevater_Tod4711

2014/1/9

#
Could you explain the problem again? Because I don't realy know what you mean by hitboy problem.
UNDEAD_DC UNDEAD_DC

2014/1/9

#
Hitbox not hitboy Here in this message i explained it:
UNDEAD_DC wrote...
Ok Thanks i have this working now, but now i have the following problem, when a container drops if just falls down but when it touches the truck it does not interact the way i want it. it does not interact on the left side and on the right side it interacts to fast. Maybe this is more clear:
There are more replies on the next page.
1
2