I am trying to get it so that when the player object runs into a portal the background image the level switches. All objects are gone and a new screen appears. I need the player to be able to go through a first portal to get to the "equipment screen" and then go through a second portal to get back to the game. The second portal essentially starts the next level. At the moment, I can get the first portal to work the very first time I load up the scenario and then after that when you run the scenario the background image switches immediately without running into the portal at all.
This is the code for the first Portal.
This is currently the world code.
Any help is greatly appreciated. I am really not sure why this is is not working.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public class Portal extends SmoothMovement { public static boolean newLevel = false ; /** * Act - do whatever the Portal wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { teleport(); } public void teleport() { Sworde a = (Sworde) getOneIntersectingObject(Sworde. class ); if (a != null ) { newLevel = true ; } else { newLevel = false ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { public static boolean nextLevel = false ; public static boolean newLevel = false ; public int levelSelect = 0 ; GreenfootImage planet = new GreenfootImage( "Background for ProjectGame.png" ); /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 1000 , 600 , 1 ); prepare(); } public void act() { setNewLevel(); setNextLevel(); interlude(); nextLevelGet(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Portal.newLevel = false ; CavernTank caverntank = new CavernTank(); addObject(caverntank, 396 , 193 ); Sworde sword = new Sworde(); addObject(sword, 118 , 113 ); Portal portal = new Portal(); addObject(portal, 350 , 89 ); setBackground(planet); Bug1 bug1 = new Bug1(); addObject(bug1, 224 , 124 ); setPaintOrder(Sworde. class , TankBoss. class , Bug1. class ); } public void interlude() { if (newLevel = true ) { setBackground( "Backtobase.png" ); PortaltoPlanet portal = new PortaltoPlanet(); addObject(portal, 900 , 550 ); newLevel = false ; } } public void nextLevelGet() { if (nextLevel) { if (levelSelect == 0 ) { setBackground(planet); levelSelect++; } } } public void setNewLevel() { newLevel = Portal.newLevel; } public void setNextLevel() { nextLevel = PortaltoPlanet.nextLevel; } private void levelOne() { setBackground( "Background for ProjectGame.png" ); setPaintOrder(Sworde. class , TankBoss. class , Bug1. class ); } |