This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
B4Programming
B4Programming wrote ...

4 days ago

trouble need help

B4Programming B4Programming

4 days ago

#
well I'm using this code to shoot out a cherries form a pie but the cherries just stays on the pie and it doesn't move like it is meant to do. Please help public void fireOnCommand() { if(Greenfoot.isKeyDown("f")) { World myWorld = getWorld(); myWorld.addObject(cherries, 0, 0); cherries.setLocation(getX(), getY()); cherries.setRotation(getRotation()); } }
Yehuda Yehuda

4 days ago

#
The code for moving a Cherry should be in the cherry class (as opposed to non-existing). You don't need to add an object then change it's location on the next line, just add it at getX() and getY() in the first place.
getWorld().addObject(new Cherry(), getX(), getY()); // whatever the name of the class is
danpost danpost

4 days ago

#
With the initial code given, the variable 'cherries' seems to just appear out of nowhere. If it declared as a field and assigned an object at that time, then only that one will ever be in your world (if declared, remove the field declaration line). Creating a new Cherry object each time the pie fires is what you need to do (as shown by Yehuda). However, the way Yehuda has shown it will not allow you to set the rotation of the Cherry object created. Create the cherry first, then add it into the world and set its rotation:
Actor cherry = new Cherry();
getWorld().addObject(cherry, getX(), getY());
cherry.setRotation(getRotation());
Yehuda Yehuda

4 days ago

#
danpost wrote...
However, the way Yehuda has shown it will not allow you to set the rotation of the Cherry object created.
I was just saying that adding an Object into the World then right afterwards changing the location is unnecessary. (But I guess I did have in mind that a new cherry should be created each time, forgetting about the rotation).
You need to login to post a reply.