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

2016/12/7

Change image of an object when press "enter" key

Kazee Kazee

2016/12/7

#
Hi! I tried (like 5 hours) for change the image of an object.
     
String keyPressed = Greenfoot.getKey();

if ("enter".equals(keyPressed))
     {
        if (seccion == 0){
       ((Episodio1_1)getWorld()).text.setImage("ep_1_diag/kaz_2.PNG");
        seccion++;
        }
        if (seccion == 1){
        Greenfoot.playSound("sfx/monster.mp3");
        ((Episodio1_1)getWorld()).text.setImage("ep_1_diag/kaz_3.PNG");
        seccion++;
        }
        if (seccion == 2){
        ((Episodio1_1)getWorld()).text.setImage("ep_1_diag/kaz_4.PNG");
        ((Episodio1_1)getWorld()).borrarDialogo();
        }
     }
Like the code, I want to change the images of that object like a RPG dialog. When the user press "enter" the image change for the next. But the problem is when I press "enter" it only disappear, is like "jump" to the "seccion == 2" when I deleted the objects of dialog. Any suggestions? or help? Thanks!
Super_Hippo Super_Hippo

2016/12/7

#
Use 'else if' instead of 'if' in lines 9 and 14. Right now, if you press enter, seccion is 0, it is set to 1 in line 7, so the condition in line 9 is met and so on.
You need to login to post a reply.