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

2013/5/26

Everytime it changes the world, but why?!

1
2
3
danpost danpost

2013/5/27

#
danpost wrote...
When you add a Reload2 object to the world, you are also adding a One object to the world.
Correction: 'Reload2' should have been 'Reload1'.
JesusJesus JesusJesus

2013/5/27

#
Thank you very much it worked :) But can you maybe tell me why the RELOAD class dont remove when the ammo is bigger then 0? I have written the simple code if it is bigger then 0 remove the reload class, but nothing happens ...
danpost danpost

2013/5/27

#
Please show what code is not working.
JesusJesus JesusJesus

2013/5/27

#
public void remove()
    {
        if (ammocounter > 0)
        {
            remove(Reload2.class);
            if (getWorld() == null) return;
        }
    }
this is in the Player two class, the class were also the reload is set on the world if the ammocounter is smaller then 1
danpost danpost

2013/5/27

#
Please show your method called 'remove(Class cls)'.
JesusJesus JesusJesus

2013/5/27

#
public void remove(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) 
        {
            getWorld().removeObject(actor);
            if (getWorld() == null) return;
        }
    }
danpost danpost

2013/5/27

#
Chances are, that when you ammo count is bumped up, your 'this' object is not on the Reload2 object; and, therefore, the object will not be removed (what your code is saying is this: remove one object of the given type from the world if, and only if, the image of that object intersects the location in the world that 'this' object is located. What you probably need to do is create and save the Reload objects in instance fields when you create the One and Two object; then you can refer directly to those fields, to add and remove those objects.
// instance field (in class One)
Reload1 reload = new Reload1();
// in 'act' method (or a method it calls)
if (ammoCount == 0 && reload.getWorld() == null) getWorld().addObject(reload, /* x, y */ );
if (ammoCount > 0 && reload.getWorld() != null) getWorld().removeObject(reload);
That would be the entire control for the Reload object, independent of any other code in the class. In other words, it should not be referenced anywhere else.
JesusJesus JesusJesus

2013/5/28

#
Okay thanks. But it says that there is no method of the .getWorld()?
JesusJesus JesusJesus

2013/5/28

#
I simply removed the && reload.getWorld() == 0) and now it works :D THANK YOU VERY MUCH! Maybe i had annoied you but im sry bout that.
danpost danpost

2013/5/28

#
Please copy/paste the line(s) that you are referring to when posting a question.
danpost danpost

2013/5/28

#
JesusJesus wrote...
I simply removed the && reload.getWorld() == 0) and now it works :D THANK YOU VERY MUCH! Maybe i had annoied you but im sry bout that.
Where did you get the invalid code 'reload.getWorld() == 0'; I gave 'reload.getWorld() == null'. By removing it, it will not work the way you may want. If you do not reload quick enough your game will probably crash or get very laggy.
JesusJesus JesusJesus

2013/5/28

#
Im sry i made it with null but with it it dont works and without it it works, and i have tested it if it gets laggy when i wait with reloading and nothing happens
danpost danpost

2013/5/28

#
Did you place the code in you world class or in the One class?
JesusJesus JesusJesus

2013/5/28

#
In the One class
danpost danpost

2013/5/28

#
Please post the entire class as it is now. I will try to figure out why you were getting that error
There are more replies on the next page.
1
2
3