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

2012/2/10

Img change on object class

Tomc84 Tomc84

2012/2/10

#
I need to make the Man object class change black on the Wall and red on the Fire, but I'm not sure how to. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Man here. * * @author (your name) * @version (a version number or a date) */ public class Man extends Actor { /** * Act - do whatever the Man wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. checkKeyPress(); // call this method to work } public void checkKeyPress() { if (Greenfoot.isKeyDown("left")) { setLocation(getX()-1,getY()); // get loaction of x and y, then move -1 on x move Left } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+1,getY()); // move Right } if (Greenfoot.isKeyDown("down")) { setLocation(getX(),getY()+1); // move down } if (Greenfoot.isKeyDown("up")) { setLocation(getX(),getY()-1); // move up } } public boolean onFire() { Actor Fire = getOneObjectAtOffset( 0 , 0, Fire.class); return Fire != null; } public boolean onWall() { Actor Wall = getOneObjectAtOffset( 0 , 0, Wall.class); return Wall != null; } }
GameCode GameCode

2012/2/10

#
There are 2 ways of doing it: You could just draw the color you want onto the image or you could change the image to something you have already drawn. CHANGE COLOR: import java.awt.Color; //import the colors you can draw with public void changeColor() { if( onWall() ) { getImage().setColor( Color.BLACK ); // sets the drawing color to black getImage().fill; // fills the hole image with black } if( onFire() ) { getImage().setColor( Color.RED ); // sets the drawing color to red getImage().fill; // // fills the hole image with red } else { getImage().setColor( Color.BLUE); // sets the drawing color to blue getImage().fill; // fills the hole image with blue } } OR CHANGE IMAGE: At first draw the 3 images ManBlack.png, ManRed.png and Man.png. Drag them into the image folder in your project and then use the following code: public void changeImage() { if( onWall() ) setImage( "ManBlack.png" ); if( onFire() ) setImage( "ManRed.png" ); else setImage( "Man.png" ); } Hope that helped ; )
Tomc84 Tomc84

2012/2/10

#
Awesome! Thank you so much. That worked. so why don't we put the if statement within the boolean onFire and onWall methods?
You need to login to post a reply.