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

2020/1/5

Platformer game

Alex99 Alex99

2020/1/5

#
i need to put at the right edge of the screen a balloon and interact with it, and if i collect 4 bananas for example, the balloon asks me: In order to proceed to the next level you need the give me the bananas, do you agree? " and then i type yes and it takes me to the next level of the game.That's the idea but i don't know how to do it exactly :p
danpost danpost

2020/1/5

#
Alex99 wrote...
i need to put at the right edge of the screen a balloon and interact with it, and if i collect 4 bananas for example, the balloon asks me: In order to proceed to the next level you need the give me the bananas, do you agree? " and then i type yes and it takes me to the next level of the game.That's the idea but i don't know how to do it exactly :p
What you failed to mention is what transpires when the response is not "yes". In that case, you would not want the question be posted again (at least not immediately) as the interaction (touching the balloon) still exists. Therefore you need a second condition (other than touching balloon) to regulate the prompt. The difference between the initial prompting and any possible successive promptings is the the balloon was not being touched on the previous act before the initial one. You can add a field to track the last state of the balloon being touched:
private boolean touchingBalloon; // = false (by default)
By comparing its value to current balloon touching state, you can "catch" any change in state, update the field and act on the new state:
if (touchingBalloon == getObjectsAtOffset(0, 0, Balloon.class).isEmpty())
{
    touchingBalloon = !touchingBalloon;
    if (touchingBalloon)
    {
        String response = Greenfoot.ask("Respond with 'yes' or 'no'");
        if ("yes".equals(response.toLowerCase()))
        {
            // proceed to next level
        }
    }
}
Alex99 Alex99

2020/1/5

#
Ok, so for collecting the coins, every time i touch them, i collect them and then go to the balloon, touch the balloon and the dialogue pops up and i gotta type yes to proceed to the next level.Do i have to make it somehow that i keep track of the coins collected? since there are only 4 in the current world.
danpost danpost

2020/1/5

#
Alex99 wrote...
Ok, so for collecting the coins, every time i touch them, i collect them and then go to the balloon, touch the balloon and the dialogue pops up and i gotta type yes to proceed to the next level.Do i have to make it somehow that i keep track of the coins collected? since there are only 4 in the current world.
If you must collect all of them, it is not necessary to count those collected. You can determine if all have been collected by making sure none are left in the world. For example:
if (getWorld().getObjects(Coin.class).isEmpty())
Alex99 Alex99

2020/1/5

#
thanks :D, one last question i think, how can i make it that, if i touch the balloon, a window with a dialogue or something will pop up and will ask me if i want to proceed to the next level, and i type yes? because i only need the yes answer :p it's a school project and she wont type no i suppose xD
danpost danpost

2020/1/5

#
Alex99 wrote...
thanks :D, one last question i think, how can i make it that, if i touch the balloon, a window with a dialogue or something will pop up and will ask me if i want to proceed to the next level, and i type yes? because i only need the yes answer :p it's a school project and she wont type no i suppose xD
You can add an object with the text into the world and then do:
getWorld().repaint();
before the ask command. Then remove it after the response is given. If "yes" is the only response, then why ask the question to begin with. Also, I had it so it only checks for "yes". Any other response, whether it be "no" or "ye", or whatever) should be treated as invalid and not pass to next level. That reminds me, as the response may be canceled out of, making the response variable null, my line 7 above should be:
if (response != null && "yes".equals(response.toLowerCase()))
Alex99 Alex99

2020/1/5

#
ok soooo, i edited the 2nd world which i added called World 2 and now the whole thing is messed up.Were they linked? Gonna send u what i did, see if u can help me a bit :p. Player: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { private int vSpeed =0; private int acceleration = 1; private int jumpHeight= -20; public static int coins = 0; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAround(); checkFalling();// Add your action code here. } private void fall() { setLocation(getX(), getY() + vSpeed); vSpeed = vSpeed + acceleration; } public void moveAround() { if (Greenfoot.isKeyDown("right")) { move(4); } if (Greenfoot.isKeyDown("left")) { move(-4); } if (Greenfoot.isKeyDown("space")&&(onGround()==true)) { vSpeed = jumpHeight; fall(); } } boolean onGround() { Actor under = getOneObjectAtOffset(0, getImage().getHeight()/2, Ground.class); return under != null; } public void checkFalling() { if (onGround()== false) { fall(); } if (onGround()== true) { vSpeed = 0; } } } First world : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Background here. * * @author (your name) * @version (a version number or a date) */ public class Background extends World { /** * Constructor for objects of class Background. * */ public Background() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1400, 800, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Ground ground = new Ground(); addObject(ground,113,488); Ground ground2 = new Ground(); addObject(ground2,368,372); Ground ground3 = new Ground(); addObject(ground3,589,283); Ground ground4 = new Ground(); addObject(ground4,725,476); Ground ground5 = new Ground(); addObject(ground5,829,130); Ground ground6 = new Ground(); addObject(ground6,933,291); Ground ground7 = new Ground(); addObject(ground7,1143,156); ground5.setLocation(813,135); ground5.setLocation(794,159); ground5.setLocation(806,161); ground2.setLocation(311,380); ground.setLocation(73,484); ground2.setLocation(318,377); ground3.setLocation(581,283); ground4.setLocation(664,476); ground6.setLocation(887,344); ground5.setLocation(828,175); ground7.setLocation(1122,218); Player player = new Player(); addObject(player,57,440); ground.setLocation(199,709); ground.setLocation(87,701); ground.setLocation(64,712); ground2.setLocation(247,611); ground2.setLocation(287,605); ground4.setLocation(434,500); ground3.setLocation(346,352); ground6.setLocation(579,265); ground6.setLocation(599,254); ground5.setLocation(855,376); ground7.setLocation(1003,467); ground7.setLocation(1019,470); ground7.setLocation(1010,472); Ground ground8 = new Ground(); addObject(ground8,1210,347); ground8.setLocation(1219,344); Ground ground9 = new Ground(); addObject(ground9,1297,189); ground9.setLocation(1323,199); Ground ground10 = new Ground(); addObject(ground10,1398,67); ground10.setLocation(1433,121); ground10.setLocation(1620,324); Ground ground11 = new Ground(); addObject(ground11,1893,282); Ground ground12 = new Ground(); addObject(ground12,2160,354); Ground ground13 = new Ground(); addObject(ground13,2398,239); Ground ground14 = new Ground(); addObject(ground14,2562,185); ground14.setLocation(2592,155); Ground ground15 = new Ground(); addObject(ground15,2800,299); Ground ground16 = new Ground(); addObject(ground16,2949,436); Ground ground17 = new Ground(); addObject(ground17,3224,390); Ground ground18 = new Ground(); addObject(ground18,3448,439); Ground ground19 = new Ground(); addObject(ground19,3659,345); Ground ground20 = new Ground(); addObject(ground20,3846,217); Ground ground21 = new Ground(); addObject(ground21,4010,96); Ground ground22 = new Ground(); addObject(ground22,4183,311); Ground ground23 = new Ground(); addObject(ground23,4324,442); Ground ground24 = new Ground(); addObject(ground24,4597,362); Ground ground25 = new Ground(); addObject(ground25,4839,248); Ground ground26 = new Ground(); addObject(ground26,5022,143); ground26.setLocation(5044,136); Ground ground27 = new Ground(); addObject(ground27,5244,248); Ground ground28 = new Ground(); addObject(ground28,5459,374); Ground ground29 = new Ground(); addObject(ground29,5702,293); Ground ground30 = new Ground(); addObject(ground30,5923,189); ground30.setLocation(5913,187); removeObject(ground21); removeObject(ground26); removeObject(ground30); removeObject(ground14); removeObject(ground9); removeObject(ground20); removeObject(ground13); removeObject(ground27); removeObject(ground11); removeObject(ground25); removeObject(ground29); removeObject(ground15); removeObject(ground22); removeObject(ground10); removeObject(ground24); removeObject(ground28); removeObject(ground17); removeObject(ground23); removeObject(ground18); removeObject(ground19); removeObject(ground16); removeObject(ground12); ground8.setLocation(1171,349); addObject(ground9,1333,212); ground9.setLocation(1341,227); Balloon Balloon = new Balloon(); addObject(Balloon,1369,172); Balloon.setLocation(1378,173); Balloon.setLocation(1375,181); Coin coin = new Coin(); addObject(coin,584,213); Coin coin2 = new Coin(); addObject(coin2,838,334); Coin coin3 = new Coin(); addObject(coin3,419,458); Coin coin4 = new Coin(); addObject(coin4,1119,313); } } Second world: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class World2 here. * * @author (your name) * @version (a version number or a date) */ public class World2 extends World { /** * Constructor for objects of class World2. * */ public World2() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1400, 800, 1); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Ground ground = new Ground(); addObject(ground,113,488); Ground ground2 = new Ground(); addObject(ground2,368,372); Ground ground3 = new Ground(); addObject(ground3,589,283); Ground ground4 = new Ground(); addObject(ground4,725,476); Ground ground5 = new Ground(); addObject(ground5,829,130); Ground ground6 = new Ground(); addObject(ground6,933,291); Ground ground7 = new Ground(); addObject(ground7,1143,156); ground5.setLocation(813,135); ground5.setLocation(794,159); ground5.setLocation(806,161); ground2.setLocation(311,380); ground.setLocation(73,484); ground2.setLocation(318,377); ground3.setLocation(581,283); ground4.setLocation(664,476); ground6.setLocation(887,344); ground5.setLocation(828,175); ground7.setLocation(1122,218); Player player = new Player(); addObject(player,57,440); ground.setLocation(199,709); ground.setLocation(87,701); ground.setLocation(64,712); ground2.setLocation(247,611); ground2.setLocation(287,605); ground4.setLocation(434,500); ground3.setLocation(346,352); ground6.setLocation(579,265); ground6.setLocation(599,254); ground5.setLocation(855,376); ground7.setLocation(1003,467); ground7.setLocation(1019,470); ground7.setLocation(1010,472); Ground ground8 = new Ground(); addObject(ground8,1210,347); ground8.setLocation(1219,344); Ground ground9 = new Ground(); addObject(ground9,1297,189); ground9.setLocation(1323,199); Ground ground10 = new Ground(); addObject(ground10,1398,67); ground10.setLocation(1433,121); ground10.setLocation(1620,324); Ground ground11 = new Ground(); addObject(ground11,1893,282); Ground ground12 = new Ground(); addObject(ground12,2160,354); Ground ground13 = new Ground(); addObject(ground13,2398,239); Ground ground14 = new Ground(); addObject(ground14,2562,185); ground14.setLocation(2592,155); Ground ground15 = new Ground(); addObject(ground15,2800,299); Ground ground16 = new Ground(); addObject(ground16,2949,436); Ground ground17 = new Ground(); addObject(ground17,3224,390); Ground ground18 = new Ground(); addObject(ground18,3448,439); Ground ground19 = new Ground(); addObject(ground19,3659,345); Ground ground20 = new Ground(); addObject(ground20,3846,217); Ground ground21 = new Ground(); addObject(ground21,4010,96); Ground ground22 = new Ground(); addObject(ground22,4183,311); Ground ground23 = new Ground(); addObject(ground23,4324,442); Ground ground24 = new Ground(); addObject(ground24,4597,362); Ground ground25 = new Ground(); addObject(ground25,4839,248); Ground ground26 = new Ground(); addObject(ground26,5022,143); ground26.setLocation(5044,136); Ground ground27 = new Ground(); addObject(ground27,5244,248); Ground ground28 = new Ground(); addObject(ground28,5459,374); Ground ground29 = new Ground(); addObject(ground29,5702,293); Ground ground30 = new Ground(); addObject(ground30,5923,189); ground30.setLocation(5913,187); removeObject(ground21); removeObject(ground26); removeObject(ground30); removeObject(ground14); removeObject(ground9); removeObject(ground20); removeObject(ground13); removeObject(ground27); removeObject(ground11); removeObject(ground25); removeObject(ground29); removeObject(ground15); removeObject(ground22); removeObject(ground10); removeObject(ground24); removeObject(ground28); removeObject(ground17); removeObject(ground23); removeObject(ground18); removeObject(ground19); removeObject(ground16); removeObject(ground12); ground8.setLocation(1171,349); addObject(ground9,1333,212); ground9.setLocation(1341,227); Balloon Balloon = new Balloon(); addObject(Balloon,1369,172); Balloon.setLocation(1378,173); Balloon.setLocation(1375,181); Coin coin = new Coin(); addObject(coin,584,213); Coin coin2 = new Coin(); addObject(coin2,838,334); Coin coin3 = new Coin(); addObject(coin3,419,458); Coin coin4 = new Coin(); addObject(coin4,1119,313); ground5.setLocation(826,470); balloon.setLocation(1373,174); ground6.setLocation(721,353); ground3.setLocation(330,358); } }
Alex99 Alex99

2020/1/5

#
Nevermind figured it out.Im sorry for disturbing u again :/
Alex99 Alex99

2020/1/5

#
Btw, if i had my character to tranzition into the next world after touching the balloon and saying yes, is there any way of editing the second world? Because i want to add collision with some monsters so when i touch them, it puts me to the beginning but if i edit anything from the second world, the whole thing turns grey and i can't access anything.(if i rightclick any world on top right it says "needs compining" when i try to access it)
danpost danpost

2020/1/6

#
Alex99 wrote...
if i edit anything from the second world, the whole thing turns grey and i can't access anything
Show your edited version of the second world for review.
You need to login to post a reply.