The game starts by killing the first type of enemy. I have a condition that when a number of enemies are dead it will switch to a different scenario(Shop) where they can purchase weapons, I have this "back button"(actor) that when it is clicked. It will switch back to the myWorld. but instead of killing the same type of enemies I want to spawn Golem(actor) instead.
This is the code I have in myWorld ,, This is like my main level.
##### This is the code I have in my Shop class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public void act() { count++; swtichToShop(); if (currentLevel == 1 ) { spawnZombies(); } else if ( currentLevel == 2 ) { spawnGolem(); } } |
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 | public void spawnZombies() { if ( count % spawnSpeed == 0 ) { randomSpawn = Greenfoot.getRandomNumber( 8 ); switch (randomSpawn){ case 0 : addObject( new Ghost(hero, counter), 0 , 0 ); break ; case 1 : addObject( new Ghost(hero, counter), getWidth()/ 2 , 0 ); break ; case 2 : addObject( new Ghost(hero, counter), getWidth(), 0 ); break ; case 3 : addObject( new Ghost(hero, counter), 0 , getHeight()/ 2 ); break ; case 4 : addObject( new Ghost(hero, counter), getWidth(), getHeight()/ 2 ); break ; case 5 : addObject( new Ghost(hero, counter), 0 , getHeight()); break ; case 6 : addObject( new Ghost(hero, counter), getWidth()/ 2 , getHeight()); break ; case 7 : addObject( new Ghost(hero, counter), getWidth(), getHeight()); break ; } } } public void spawnGolem() { if ( count % spawnSpeed == 0 ) { randomSpawn = Greenfoot.getRandomNumber( 5 ); switch (randomSpawn){ case 0 : addObject( new Golem(hero, counter), 0 , 0 ); break ; // top left case 1 : addObject( new Golem(hero, counter), getWidth(), 0 ); break ; //top right case 2 : addObject( new Golem(hero, counter), 0 , getHeight()); break ; //bottom left case 3 : addObject( new Golem(hero, counter), getWidth(), getHeight()); break ; //bottom right } } } |
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 | public class Shop extends World { Counter counter; MyWorld myWorld = new MyWorld(); Back back = new Back(); /** * Constructor for objects of class Shop. * */ public Shop() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 800 , 600 , 1 ); addObject(back, 130 , 550 ); getBackground().drawImage( new GreenfootImage( "$5000" , 20 , Color.BLACK, null ), 680 , 80 ); getBackground().drawImage( new GreenfootImage( "$3500" , 20 , Color.BLACK, null ), 680 , 200 ); getBackground().drawImage( new GreenfootImage( "$500" , 20 , Color.BLACK, null ), 680 , 320 ); getBackground().drawImage( new GreenfootImage( "+10hp, $500" , 20 , Color.BLACK, null ), 660 , 490 ); getBackground().drawImage( new GreenfootImage( "Welcome hero! \n Come and spend your cash!" , 20 , Color.BLUE, null ), 40 , 300 ); prepare(); } public void act() { next(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Buy buy = new Buy(); addObject(buy, 693 , 535 ); Buy buy2 = new Buy(); addObject(buy2, 702 , 377 ); Buy buy3 = new Buy(); addObject(buy3, 703 , 249 ); Buy buy4 = new Buy(); addObject(buy4, 702 , 127 ); } public void next() { back.nextLevel(); myWorld.currentLevel = 2 ; } } |