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

2012/12/11

Need help with carrying objects

Sergio Sergio

2012/12/11

#
Hey guys, I need some help. For a school project I am creating a game which involves carrying and dropping objects. Due to circumstances I have to delete the object once it's picked up and place it back when dropped. Now I have the following problem, I use a variable that keeps track of if I'm holding an object and if so, which object by assigning an int value to each object. Here is a small bit of the code:
int objectCarrying = 0; 

public void carryObject(){
    
Actor objectpapier = getOneObjectAtOffset(0, 0, Papierenzak.class);
            
            
            if(objectpapier != null){
                
                if(objectCarrying == 0){
                    
                getWorld().removeObject(objectpapier);    
                objectCarrying = 1;
                
                }
                objectCarrying = 1;
            }
            if(objectCarrying == 1){
                
                getWorld().addObject(new Papierenzak(), 0, 0);
                objectCarrying=0;
                
                }
    
    }

So if I pick up a "papierenzak" ( Dutch ) it removes the object that I want to, after that it should assign "objectCarrying" with the value 1. However, the value of "objectCarrying" remains 0, but the object gets deleted so that shows it does go trough the loop. Why isn't the value of "objectCarrying" changing?
Gevater_Tod4711 Gevater_Tod4711

2012/12/11

#
if you collect the object your variable objectCarrying is 1 but then the following if clause is executed because it is 1. And then the object is added and the value of objectCarrying is 0 again. To fix the problem you could use an else in front of your if in line 18 or let your actor drop the object just when a key is pressen. And I think there is another bug: You probably want your actor to drop the object at his current position. But to do that you have to change line 20. Instead of
getWorld().addObject(new Papierenzak(), 0, 0);
do
getWorld().addObject(new Papierenzak(), getX(), getY());
Sergio Sergio

2012/12/11

#
Ah I see, thanks mate really helped a lot!!
You need to login to post a reply.