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

2013/9/17

Gumball Machine problem

codingjava codingjava

2013/9/17

#
Scenario - There is one gumball machine and 3 type of coins (quarter , fake quarter and dime) . Objective - When i bring the coin on the gumball machine it should disappear Problem - Coin is not disappearing , confused about how to match (x,y ) coordinate of gumball machine and coin. i tried writing a code for gumball machine for quarter let me know whats the mistake i am doing and why coin is not disappearing. Also there is no gridlines in this scenario as compared to examples such as wombat and other scenarious mentioned in greenfoot. public void act() { if(foundQuarter()) { removeQuarter(); } } public void removeQuarter() { Actor quarter = getOneObjectAtOffset(0, 0, Quarter.class); // eat the leaf... getWorld().removeObject(quarter); } public boolean foundQuarter() { Actor quarter = getOneObjectAtOffset(0, 0, Quarter.class); if(quarter != null) { return true; } else { return false; } } }
Zamoht Zamoht

2013/9/17

#
Instead of the two places where you wrote Actor quarter = getOneObjectAtOffset(0, 0, Quarter.class); You should try this instead Actor quarter = getOneIntersectingObject(Quarter.class); The hitbox of the gumball machine may be too big and you may not get the result you want. If this is the case you should make a "hitbox". To do that you should create a new actor with a transparent image of the size you want. You should keep the reference and then you can check if the quater is intersecting the hitbox. If this doesn't make any sense please let me know.
danpost danpost

2013/9/17

#
When using 'getOneObjectAtOffset', the object you are looking for must be exactly at (not just intersecting) the coordinates (gumballMachine.getX(), gumballMachine.getY()). That would be at the exact center of the image of the gumball machine. You can increase the range of the area to check by using 'getObjectsInRange' instead. If the coin insert point is not positioned in the center of the machine then you should probably do as Zamoht has mentioned; or, use a little programming trickery with:
// to check for quarter at coin insert in class of gumball machine
private boolean foundQuarter()
{
    setLocation(getX(), getY()+80); // move center of object to coin insert location
    Actor quarter = getOneObjectInRange(20, Quarter.class);
    setLocation(getX(), getY()-80); // return to original location
    return quarter != null;
}
Of course, the distances and range should be adjusted and there may be an offset to move along the x axis also.
Zamoht Zamoht

2013/9/17

#
Good suggestion as always danpost. The trickery you are talking about is probably a bit easier to use and understand for new programmers compared to my hitbox suggestion.
codingjava codingjava

2013/9/18

#
Hi Dan & Zamoht- Thanks for your quick reply @Dan - when i am using your code , it gives me an error " cannot find symbol - method getOneObjectInRange(int,java.lang.Class<Quarter>) @Zamoht :- gumball machine image has a place where we need to insert the coin ..how can i make that position as the hitbox...
codingjava codingjava

2013/9/18

#
Also @Dan & @Zamoht i am using my my mouse to drag my coin on the gumball machine will that make difference..
danpost danpost

2013/9/18

#
Sorry, that is the one intersection-checking method that does not have both a single object return and a multiple objects return (like 'getOneIntersectingObject' and 'getIntersectingObjects', for example). Instead, try this:
// to check for quarter at coin insert in class of gumball machine
private boolean foundQuarter()
{
    setLocation(getX(), getY()+80); // move center of object to coin insert location
    boolean  quarterPresent = !getObjectsInRange(20, Quarter.class).isEmpty();
   setLocation(getX(), getY()-80); // return to original location
    return quarterPresent;
}
danpost danpost

2013/9/18

#
codingjava wrote...
Also @Dan & @Zamoht i am using my my mouse to drag my coin on the gumball machine will that make difference..
You might encounter some bugs if the dragging code is not conditioned properly. See how far you can get without help and if you do find that you cannot fix any bugs that arise, post up again showing the code you are using, indicate where the problem is, and if there is an error message, copy/paste it to your post. That is the best way to get the appropriate help with the least difficulty.
codingjava codingjava

2013/9/18

#
Thanks @Danpost: now i can achieve my scenario where my Gumball machine is able to find and remove the coin . But the problem i am facing is that i want to disappear my coin exactly on the gumball machine where the insert coin image is there and also i want to add the scenario where i will drag the coin using my mouse on the gumball machine or the place where in need to insert the coin in gumball machine and it should display some message " . I am using the below code please let me know what are the changes needs to be made to display the actual result public void act() { if(foundQuarter()) { removeQuarter(); } } public void removeQuarter() { Actor quarter = getOneObjectAtOffset(0, 0, Quarter.class); if(quarter != null) { getWorld().removeObject(quarter); } } public boolean foundQuarter() { setLocation(getX()+ 100, getY()+80); // move center of object to coin insert location boolean quarterPresent = !getObjectsInRange(1000, Quarter.class).isEmpty(); setLocation(getX(), getY()-80); // return to original location return quarterPresent; } }
danpost danpost

2013/9/18

#
Do you have an actor to display the messages? if so, please show its code.
codingjava codingjava

2013/9/18

#
Coin would be the actor for me , when i will be taking draging my coin on the Gumball machine it should display some message
danpost danpost

2013/9/18

#
You did not understand. Do you have an actor to be used for displaying the messages? if so, please show its code. You need an object whose image will have the message drawn on it. It is the class of THAT object I am asking about.
You need to login to post a reply.