Hello , currently I am working on a game for my assessment. Nearly done just got stuck on the last bit.
I have an Object with an image , when this object start falling change its image to a random image ( from a list of images). What I want is upon collecting to check which image is changed to and if its certain image my collecting actor to change its image and take different properties. here some code I tried but it doesn't seems to work.
public class Bag extends Actor
{
GreenfootImage ship = new GreenfootImage("shooter.png");
GreenfootImage bomb = new GreenfootImage("bomb.png");
GreenfootImage ball = new GreenfootImage("bigball.png");
GreenfootImage xxl = new GreenfootImage("XXL.png");
private boolean hasStartedFalling = false;
MyWorld thisGame;
Level2 nextGame;
public StageClear stage = new StageClear();
int score = 0;
int bags = 4;
/**
* Act - do whatever the Bag wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
bagFall();
bagCollection();
}
public void bagFall()
{
if((!isTouching(Block.class) && !isTouching(Hblock.class)))
{
if (!hasStartedFalling)
{
// Change the image at the beginning of the fall
setRandomImage();
hasStartedFalling = true;
}
// Move the actor down (adjust speed as needed)
setLocation(getX(), getY() + 1);
}
}
public void bagCollection()
{
Actor robot = getOneIntersectingObject(Robot.class);
if (robot != null)
{
Greenfoot.playSound("pickup.mp3");
checkImage();
getWorld().removeObject(this);
thisGame.score++;
nextGame.score++;
thisGame.bags--;
score++;
}
else if(getY() == 599)
{
getWorld().removeObject(this);
bags--;
}
}
private void setRandomImage()
{
String shooter = {"trash.png","space.png","trash.png", "bomb.png","trash.png", "ExtraLarge.png","trash.png", "bigball.png", "trash.png"};
int randomIndex = Greenfoot.getRandomNumber(shooter.length);
setImage(shooter);
}
private void checkImage()
{
Robot robot = new Robot();
Ball ball = getWorld().getObjects(Ball.class).get(0);
GreenfootImage currentImage = getImage();
if (currentImage != null && currentImage.equals("space.png"))
{
robot.setImage(ship);
}
else if(currentImage != null && currentImage.equals("bigball.png"))
{
ball.setImage(currentImage);
}
}
}
that is falling object actor.