Results 1 to 6 of 6
  1. #1
    Boyee is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Unhappy 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 :o)

    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.

    Java 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

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    what does getOneObjectAtOffset() return?

  3. #3
    Boyee is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    getOneObjectAtOffset() is the code for checking if another actor is located in the same location as the actor the code is used by

  4. #4
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    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!

  5. #5
    hiranya is offline Member
    Join Date
    Jun 2007
    Location
    Colombo, Sri Lanka
    Posts
    32
    Rep Power
    0

    Default

    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

  6. #6
    Boyee is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •