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

2012/8/6

dno whats wrong with this code.

gusbus123 gusbus123

2012/8/6

#
private int ID;
if(ID>2) {
            String key = Greenfoot.getKey();
            if(key!=null && ID>10) {
                switch(ID) {
                   case 11: pw.p1Left=key; break;
                   case 12: pw.p1Right=key; break;
                   case 13: pw.p1Up=key; break;
                   case 14: pw.p1Down=key; break;
                   case 15: pw.p2Left=key; break;
                   case 16: pw.p2Right=key; break;
                   case 17: pw.p2Up=key; break;
                   case 18: pw.p2Down=key; break;
                }
                getWorld().removeObject(this);
            }
        }
ok. I have this code (above) that checks what keys are being pressed and depending on the ID it will save the pressed key to the world. If i dont have the "&& ID>10" in line 4 and the "2" in line 2 is "10" it would work perfectly fine except for the fact that it will not reset to null when its supposed to. So in the scenario when the object (that has the ID greater than 10) is created it will automatically save the most recently pressed key and remove itself, when i want it to accually have the string "key" being rechecked so it doesnt do that quick save. Check the first help page (the pacman help page) and press a few keys then click on one of the picures of the keys in the following scenario in the link below to see what is really happening. I will also fix the bad descriptions soon (im in the lowest class for english... crap descriptions are expected). http://www.greenfoot.org/scenarios/5608
gusbus123 gusbus123

2012/8/6

#
oh almost forgot to state what happens when its wrong... Ok the code there is supposed to allow the objects with the ID>2 to keep on checking the currently pressed key and save it into the string called "key". If the ID is also greater than 10 they will set the keys according to the switch and also remove the object. But for some reason whenever i have line 2 as "if(Id>2) {" and line 4 with the ID>10 or any part that stops the "removeObject" command for objects with the ID<11 then all the objects including the ones with the ID>10 wont remove itself as well. Iv even tried it exactly like the code above but without this bit "&& ID>10" but that basically made all objects of this kind that has the ID>2 will get removed whenever i pressed a key. But when all other objects get remeved (only the object with the ID>10 is left) and u press a key then it will save the string properly and remove itself as i wanted (but with all other needed control buttons removed...).
davmac davmac

2012/8/6

#
I'm really confused. Is the code you posted above not doing what you want? What is the 'ID' supposed to represent - why is it being > 2 significant? or > 10? When I go to the help screen in your scenario it seems to work like it should? I mean, I can click on a key icon and then choose another key for it.
nccb nccb

2012/8/6

#
If you have this code in several actors, you may be encountering the common issue that Greenfoot.getKey() takes the key out of the queue, so the first actor to call it will get a different result to subsequent actors.
gusbus123 gusbus123

2012/8/7

#
well if u press a key then click on one of those key icons it will automatically set the key to what was pressed. The key icons are of the same class and has the ID 3-10. The pages created when u click on a icon are also the same class but has the ID 11-18. If i had the code like the one above it will still check the key but the objects with the ID 11-18 will not do whats in line 4-16. Right now the code in the scenario that is linked is like this:
private int ID;
if(ID>10) {
            String key = Greenfoot.getKey();
            if(key!=null) {
                switch(ID) {
                   case 11: pw.p1Left=key; break;
                   case 12: pw.p1Right=key; break;
                   case 13: pw.p1Up=key; break;
                   case 14: pw.p1Down=key; break;
                   case 15: pw.p2Left=key; break;
                   case 16: pw.p2Right=key; break;
                   case 17: pw.p2Up=key; break;
                   case 18: pw.p2Down=key; break;
                }
                getWorld().removeObject(this);
            }
        }
with the code like this it has that glitch i said at the begining of this reply. But if i used this code:
private int ID;
if(ID>2) {
            String key = Greenfoot.getKey();
            if(key!=null && ID>10) {
                switch(ID) {
                   case 11: pw.p1Left=key; break;
                   case 12: pw.p1Right=key; break;
                   case 13: pw.p1Up=key; break;
                   case 14: pw.p1Down=key; break;
                   case 15: pw.p2Left=key; break;
                   case 16: pw.p2Right=key; break;
                   case 17: pw.p2Up=key; break;
                   case 18: pw.p2Down=key; break;
                }
                getWorld().removeObject(this);
            }
        }
it will still create those menu objects when u click on the key icons but won't be able to set your new keys. However if i didnt have the second ID check, so like this:
private int ID;
if(ID>2) {
            String key = Greenfoot.getKey();
            if(key!=null) {
                switch(ID) {
                   case 11: pw.p1Left=key; break;
                   case 12: pw.p1Right=key; break;
                   case 13: pw.p1Up=key; break;
                   case 14: pw.p1Down=key; break;
                   case 15: pw.p2Left=key; break;
                   case 16: pw.p2Right=key; break;
                   case 17: pw.p2Up=key; break;
                   case 18: pw.p2Down=key; break;
                }
                getWorld().removeObject(this);
            }
        }
It set the String key correctly but whenver i press a key it will remove all those key icons first then if you have clicked on an icon which creates the menu's with the ID 11-18 it will finally check the switch and set the key. But all the icons will be gone. This shows what nccb said, the first object created will be the first to check and have different results to the rest.
gusbus123 gusbus123

2012/8/7

#
@davmac go into the help menu and press a key (on ur keyboard) then press the icon and you will see the glitch i am facing. And that uses the first code of the last post i did.
danpost danpost

2012/8/7

#
What you need to do, is clear the buffer when an icon is clicked on. In the Icon class in your code block for:
if (Greenfoot.mouseClicked(this))
add the following line:
while (Greenfoot.getKey() != null);
gusbus123 gusbus123

2012/8/7

#
wow that worked. kill me now... jks, thx dan that fixed the problem.
You need to login to post a reply.