Anyone know how to change the pic of one object through the mouse click of another object?


1 2 3 4 5 6 | if (Greenfoot.mouseClicked( this )) { // get object reference if no readily available // assume name as 'morpheus' morpheus.updateImage( "imageFilename.gif" ); } |
1 2 3 4 | Morpheus morpheus = new Morpheus(); addObject(morpheus, x_coord, y_coord); // assume object to be clicked on as 'Icon.class' addObject( new Icon(morpheus), x2_coord, y2_coord); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private Morpheus morpheus; public Icon(Morpheus morph) { morpheus = morph; // rest of constructor } public void act() { // some of act method (maybe) if (Greenfoot.mouseClicked( this )) morpheus.updateImage( "imageFilename.gif" ); // rest of act method (maybe) } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class LightWorld here. * * @author (your name) * @version (a version number or a date) */ public class LightWorld extends World { private Light l1,l2; /** * Constructor for objects of class LightWorld. * */ public LightWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); l1 = new Light(); addObject(l1, 71 , 65 ); l2 = new Light(); addObject(l2, 304 , 60 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Button here. * * @author (your name) * @version (a version number or a date) */ public class Button extends Actor { private GreenfootImage sB0; private GreenfootImage sB1; private GreenfootImage sBDown; private boolean wasBtnPushed; public Button() { sB0 = new GreenfootImage ( "Button0.png" ); sB1 = new GreenfootImage ( "Button1.png" ); sBDown = new GreenfootImage ( "ButtonDown.png" ); setImage (sB0); wasBtnPushed = false ; } /** * Act - do whatever the Button wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mousePressed( this ) && !wasBtnPushed) { setImage (sBDown); wasBtnPushed = true ; } if (!Greenfoot.mousePressed( this ) && wasBtnPushed) { setImage (sB1); wasBtnPushed = 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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Light here. * * @author (your name) * @version (a version number or a date) */ public class Light extends Actor { private GreenfootImage red; private GreenfootImage blue; private GreenfootImage off; private int count; public Light() { red = new GreenfootImage( "red.png" ); blue = new GreenfootImage( "blue.png" ); off = new GreenfootImage( "Off.png" ); setImage(off); count = 0 ; } /** * Act - do whatever the Light wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (Greenfoot.mousePressed( this )) { setImage(red); count = count + 1 ; } if (Greenfoot.mousePressed( this ) && count == 2 ) { setImage(blue); count = count + 1 ; } if (Greenfoot.mousePressed( this ) && count > 3 ) { setImage(off); count = 0 ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public void act() { if (count == 0 && Greenfoot.mousePressed( this )) { setImage(red); count = count + 1 ; } if (count == 1 && Greenfoot.mousePressed( this )) { setImage(blue); count = count + 1 ; } if (count == 2 && Greenfoot.mousePressed( this )) { setImage(off); count = 0 ; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import greenfoot.*; public class Light extends Actor { private GreenfootImage[] image= { new GreenfootImage( "Off.png" ), new GreenfootImage( "red.png" ), new GreenfootImage( "blue.png" ) }; private state = 0 ; // same as your 'count' public Light() { setImage(image[state]); } public void act() { if (Greenfoot.mouseClicked( this )) { state = (state + 1 ) % 3 ; setImage(image[state]); } } } |