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

2012/9/5

How to disappear a Coin?

valaji_hetal valaji_hetal

2012/9/5

#
I am currently working on a Gumball Machine code wherein a coin should be inserted into the machine i.e a coin should be moved over the coin insertion area of the machine and as soon as the coin comes over that area, it should disappear. The problem I'm facing is that as soon as the coin comes over the area, it doesn't disappear rather it doesn't move from that point. I'm posting my code in the Coin class for reference: public class Coin extends Actor { public void act() { int mouseX, mouseY ; if(Greenfoot.mouseDragged(this)) { MouseInfo mouse = Greenfoot.getMouseInfo(); mouseX=mouse.getX(); mouseY=mouse.getY(); setLocation(mouseX, mouseY); } //method to disappear the coin disappear(); } public void disappear() { Actor rcvCoin; rcvCoin = getOneObjectAtOffset(380,280,Coin.class); if (rcvCoin != null) { World world; world = getWorld(); world.removeObject(rcvCoin); world.removeObject(this); return; } } } Also, I'm posting the image of the application for the reference:
danpost danpost

2012/9/6

#
First, let us restrict when the 'disappear()' method is called from the 'act()' method
if (getX() > 377 && getX() < 383 && getY() > 277 && getY() < 283) disappear();
Given it a small range, but it can be adjusted to suit. Now, in the 'disappear()' method, all you need to do is (1) play whatever sounds you might be using (maybe a coin insertion sound, a cranking sound with a shuffling gumball sound; none required though), (2) remove the coin from the world (if you need the denomination of the coin, save it in a variable first (or use it as needed first)): use the code 'getWorld().removeObject(this);'. (3) create a gumball object before removing 'this' (while you have a reference to the world with 'getWorld()'). The gumball could have a built in delay before appearing. Or, if you have an object for the front plate of the gumball machine, use 'setPaintOrder' to have the plate painted over the gumball, and the gumball can drop slowly, appearing out the opening. Of course, make sure the coins gets painted over the plate. I have no clue why you are trying to get a reference to 'rcvCoin' at that offset, which (if you are dragging 'this' toward (380, 280), would be looking for 'rcvCoin' at (760, 560) (probably off the grid, or real close to the lower right corner of the view frame).
valaji_hetal valaji_hetal

2012/9/6

#
Hey danpost, Thanks for your quick reply. I understood your first and second point, but I couldn't understand your third point. Moreover, for disappearing the coin, I tried exactly what you said. But, the same problem retains i.e the coin gets stucked in that area and it doesn't disappear. So, after moving the coin over that area, we can't move it to any other place. Moreover, the application is not returning any type of error. Also, I will tell you how the entire application works: There are three coins on the screen, one penny, one quarter and one fake quarter. And also there are three aliens which out of which one is coin inspector, the other one is a gumball picker for a particular color of a gumball and the third one is the random gumball picker. So, now when we drag a penny over the machine, the coin disappears and the coin inspector alien will tell that sorry I need a quarter and you inserted a penny. So, again, If we drag a quarter over the machine, the coin disappears and the machine will say that i got the coin. now if we click on the crank then, any of the two aliens will pick up a gumball and a gumball will appear on the screen. Moreover, after inserting a coin, if we insert another coin, then the machine should not accept that coin and that coin should be sent back to its original position. Also, if we insert a fake quarter then the coin inspector should say that the coin is fake. You can suggest some improvement in this application. Right now, I'm in the initial phase of the application. So, I'm just unable to get ahead of this coin disappearing part. Waiting eagerly for your reply and thanking you in anticipation.
danpost danpost

2012/9/6

#
Do not have time to answer your questions, now. Will respond later this evening if noone has helped you by then. Best regards.
You need to login to post a reply.