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

2013/4/30

Tracking objects & adding new objects

ColonelCaboose ColonelCaboose

2013/4/30

#
I need help with two things: First trying to figure out how to make a bullet follow a certain object. I have the bullet class made and is able to shoot fine, but how would I make that it heads towards the nearest enemy. and Second, I have this spaceship that flies across the screeen and drops rare items, but I would only like him to drop them in two locations. How do I tell him to only drop at these X and Y coordinates?
Solringolt Solringolt

2013/4/30

#
for your second question you just have to do a if condition. Something like this: if (getX() == locationXyouWant && getY() == locationYyouWant) { dropItem(); }
ColonelCaboose ColonelCaboose

2013/5/1

#
Thanks for the help on that part!
danpost danpost

2013/5/1

#
You will need to use a 'while' loop using conditions that while no enemy actor is yet found and while the range being checked is less than the greater of the width of your world and the height of your world. Before the loop, declare two local fields; one to hold an Enemy object initialized to null and the other to hold the current distance that you are checking for enemy in initially set to a small number. In the loop, set the Enemy object field to what is returned by using 'getObjectsInRange' with the current distance value; then add a small amount to the distance. After the loop, you will either have 'null' or an Enemy object in your Enemy object field. If it is not null, use its location to 'turn' the newly created bullet in that direction.
ColonelCaboose ColonelCaboose

2013/5/1

#
Thank so much I'll get right to work on it!
You need to login to post a reply.