Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2007, 06:41 PM
Member
 
Join Date: Nov 2007
Posts: 5
Boyee is on a distinguished road
New boy in need of some help!
Hi all,

Me and a friend are currently working on a programme, and for the life of us we cant get it to do what it's being told to! (I mean we just have no idea )

We both are not very strong in coding so any help in the right direction will be very much appreciated. We both don't expect and want the coding to be simply given to us as then we will never learn!

The scenario:

At the moment, we want a charecter to pick up items from the world by simply moving over it. Then to drop it into a container.



Great, we have that working, but! We only want the charecter to only pick up only one item at a time this is where we are strugelling.

Not sure what to give you guys so i will chuck it all in there!

The interface we are using is Greenfoot.

Code:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * Write a description of class Prat here. * * @author (your name) * @version (a version number or a date) */ public class Prat extends Actor { private int X; private int Y; private boolean carryRubish = false; private int pocket2; private int total; public Prat() { pocket2 = 0; total = 0; } /** * Act - do whatever the Prat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(foundrubish()) { pickuprubish(); } if(foundBin()) { putinBin(); } move(); } /** * Check whether there is a rubish in the same cell as we are. */ public boolean foundrubish() { Actor rubish = getOneObjectAtOffset(0, 0, rubish.class); if(rubish != null) { return true; } else { return false; } } /** * Pick up rubish. */ public void pickuprubish() { Actor rubish = getOneObjectAtOffset(0, 0, rubish.class); if(rubish != null) { //if(pocket2 = 0) { // eat the leaf... getWorld().removeObject(rubish); pocket2 = pocket2 + 1; carryRubish = true; } //} } public boolean foundBin() { Actor Bin = getOneObjectAtOffset(0, 0, Bin.class); if(Bin != null) { return true; } else { return false; } } public void putinBin() { Actor Bin = getOneObjectAtOffset(0, 0, Bin.class); if(Bin !=null) { if (carryRubish = true) { total = total + 1; carryRubish = false; pocket2 = pocket2 - 1; } } } public void move() { X = getX(); Y = getY(); if(Greenfoot.isKeyDown("up")) { setLocation(X, Y - 1); setImage("up.jpg"); } if(Greenfoot.isKeyDown("left")) { setLocation(X - 1, Y); setImage("left.jpg"); } if(Greenfoot.isKeyDown("right")) { setLocation(X + 1, Y); setImage("right.jpg"); } if(Greenfoot.isKeyDown("down")) { setLocation(X, Y + 1); setImage("down.jpg"); } } }
Boyee
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-06-2007, 07:42 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
what does getOneObjectAtOffset() return?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-07-2007, 12:22 AM
Member
 
Join Date: Nov 2007
Posts: 5
Boyee is on a distinguished road
getOneObjectAtOffset() is the code for checking if another actor is located in the same location as the actor the code is used by
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 12:47 AM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Are you putting the code that calls the pickuprubish method in a loop?

I assume that the variables that are being passed to it are the coordinates from the character (0, 0 for the same location as the character) and the type of Actor that it should be looking for. From what you have written, it only seems that the character would pickup more than one piece of rubish from the world if:
1. The getOneObjectAtOffset method returned more than one object. From the name and the return type, I would think that it wouldn't. Or,
2. The pickuprubish method is called more than once (inside a loop of some sort.)

This might not be too helpful but it's all that I can see with the code that is given.

Good luck!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-07-2007, 07:29 AM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
I too agree with ShoeNinja. Clearly, the work horse here is the getOneObjectAtOffset() method. If possible post the method definition of it so we can get a better understanding. Also check how you are calling the pickuprubbish() method. If you are calling it within a loop or using conditional statements where multiple conditions are true at the same time then that is probably the cause of this issue.

Regards,
Hiranya
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-07-2007, 08:42 PM
Member
 
Join Date: Nov 2007
Posts: 5
Boyee is on a distinguished road
hey guys,

The problem is sorted now, thanks for the help!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 11:20 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org