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

2023/11/16

I need help with checking image

TrueForm TrueForm

2023/11/16

#
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.
TrueForm TrueForm

2023/11/16

#
It is about last method checkImage()
danpost danpost

2023/11/17

#
TrueForm wrote...
It is about last method checkImage()
Well, this much I can say: each separate robot created in the method (first line in method block) each act step fails for non-usage. You may set the image of a robot, but nothing more is done with them. Each is lost as soon as the method ends execution each act step.
TrueForm TrueForm

2023/11/18

#
Any help on how to achieve what I want. To check image on collecting object and according to it to give properties to the robot? Thanks for replay Danpost
danpost danpost

2023/11/18

#
TrueForm wrote...
Any help on how to achieve what I want. To check image on collecting object and according to it to give properties to the robot?
The initial image selection process would be best if done before the actor is added to the world:
import greenfoot.*;

public Bag extends Actor
{
    public int score: // for this bag only (to be used by world when removed)
    private int imageNumber; // retains index of initial image (for simplicity's sake)
    
    public Bag() { // gives bag initial image
        String[] shooter = { "space", "bomb", "bigball", "ExtraLarge", "trash" };
        imageNumber = Greenfoot.getRandomNumber(shooter.length+4);
        if (imageNumber > shooter.length-1) imageNumber = shooter.length-1;
        GreenfootImage image = new GreenfootImage(shooter[imageNumber]+".png");
        setImage(image);
    }
    
    public void act() {
        bagFall();
        bagCollection();
    }
    
    private void bagFall() {
        if (!isTouching(Block.class) && !isTouching(Hblock.class)) {
            setLocation(getX(), getY()+1);
        }
    }
    
    private void bagCollection() {
        Actor robot = getOneIntersectingObject(Robot.class);
        if (robot != null) 
        {
            Greenfoot.playSound("pickup.mp3");
            if (imageNumber == 0) {
                robot.setImage("shooter.png");
            }
            else if (imageNumber == 2) {
                ((Actor)getWorld().getObjects(Ball.class).get(0)).setImage(getImage());
            }
            score++;
        }
        if (robot != null || getX() == 599) {
            getWorld().removeObject(this);
        }
    }
}
Then, in world class(es):
public void removeObject(Actor actor) {
    super.removeObject(actor);
    if (actor instanceof Bag) {
        score += ((Bag)actor).score;
    }
}
Also, in world class(es):
int bags = 4;

public void act() {
    if (getObjects(Bag.class).isEmpty()) {
        bags--;
        if (bags > 0) {
            addObject(new Bag(), Greenfoot.getRandomNumber(getWidth()), 0);
        }
    }
}
TrueForm TrueForm

2023/11/18

#
Thank you very much. I am new to green foot and java. Still figuring things out. And its my bad submitting such complicated game proposal.
You need to login to post a reply.