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

2014/2/8

Place Crate Relative to Rotation

Osidy Osidy

2014/2/8

#
I have a truck that will pick up and place crates, but when placing them I want it to always place it behind the truck, how would I do this? This is my current code for placing the crates:
1
2
3
4
5
6
7
8
9
10
if (crateGrab>0)
            {
                if (Greenfoot.isKeyDown("space")==true)
                {
                    Crate newCrate = new Crate();
                    getWorld().addObject(newCrate, getX()-50, getY());
                    setImage("truck.png");
                    crateGrab=crateGrab-1;
                }
            }
The "-50" is so it does not pick it back up after placing it, I would need this to be some how implemented into the rotation placement as well. Thanks ahead of time!
danpost danpost

2014/2/8

#
Replace line 6 with the following lines:
1
2
3
4
getWorld().addObject(newCrate, getX(), getY());
newCrate.setRotation(getRotation());
newCrate.move(-50);
newCrate.setRotation(0); // this line optional
Line 1 adds the crate into the world at the same location the truck is at. Line 2 gives the crate the same rotation as the truck. Line 3 moves the crate backward (behind the truck). Line 4 (optional) resets the rotation of the crate to zero.
Osidy Osidy

2014/2/8

#
Thanks a ton! That was exactly what I wanted to do!
You need to login to post a reply.