I'm trying to create a player selection world for a game I'm creating. The character chosen by clicking should be the one added to the main world class (Camp). The problem is that I can't figure out how to program the mouse clicking, as most other posts seem to be based on... something else? In one of the example scenarios I did with my class this summer, click detection was as simple as using Greenfoot.mouseClicked(this); but apparently it can be far more complex?
Camp
PlayerSelection
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Camp extends World { GreenfootSound backgroundMusic = new GreenfootSound( "Lobo Loco.mp3" ); /** * Constructor for objects of class MyWorld. * */ public Camp () { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); Greenfoot.setWorld( new PlayerSelection()); prepare(); setPaintOrder(Tent. class , Block. class ); setPaintOrder(Flag. class , Tent. class , Block. class ); backgroundMusic.playLoop(); } public void stopped() { backgroundMusic.setVolume( 0 ); //change bg to what you declared the file as } public void started() { backgroundMusic.setVolume( 100 ); //change bg to what you declared the file as } public void act() { showPic(); } public void showPic() { GreenfootImage KalasImage; KalasImage= new GreenfootImage( "TransparentKalas.png" ); KalasImage.drawImage(KalasImage, 300 , 200 ); } public void playSoundtrack() { Greenfoot.playSound( "Lobo Loco.mp3" ); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Kalas kalas = new Kalas( 50 , 75 ); addObject(kalas, 394 , 234 ); Tent TheTent = new Tent(); addObject(TheTent, 126 , 5 ); TheTent.setLocation( 130 , 19 ); Block block = new Block( 25 , 200 ); addObject(block, 373 , 113 ); block.setLocation( 278 , 93 ); Block block2 = new Block( 100 , 25 ); addObject(block2, 275 , 310 ); block2.setLocation( 217 , 181 ); Block block3 = new Block( 100 , 25 ); addObject(block3, 280 , 269 ); block3.setLocation( 55 , 178 ); Flag flag = new Flag(); addObject(flag, 345 , 135 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class PlayerSelection here. * * @author (your name) * @version (a version number or a date) */ public class PlayerSelection extends World { boolean KalasChosen; boolean MailfistChosen; /** * Constructor for objects of class PlayerSelection. * */ public PlayerSelection() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); makeCharacters(); showLabels(); } public void showLabels() { showText( "CHOOSE YOUR PLAYER BOI" , getWidth()/ 2 , 50 ); showText( "Chiseled Pecs..." , 175 , 100 ); showText( "Or Chiseled Abs?" , 500 , 100 ); } public void makeCharacters() { Kalas kalas = new Kalas( 150 , 225 ); addObject(kalas, 175 , 250 ); Mailfist mailfist = new Mailfist( 150 , 225 ); addObject(mailfist, 500 , 250 ); } public void selectCharacter() { if (Greenfoot.mouseClicked(Kalas. class )) { KalasChosen = true ; Greenfoot.setWorld( new Camp()); } if (Greenfoot.mouseClicked(Mailfist. class )) { MailfistChosen = true ; Greenfoot.setWorld( new Camp()); } } } |