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

2022/2/3

Add Actors through actor.

NUKES NUKES

2022/2/3

#
I am trying to make it so, when a Bullet touches a rock, it splits into 2 smaller rocks at the location, where the rock was shot, but I cant seem to figure out how to do it, i tried creating the following method. class ball (rock):
public void split()
    {
        Actor bullet = getOneIntersectingObject(bullet.class);
        if (bullet != null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(this);
            ball2 ball2 = new ball2();
            getWorld().addObject(ball2, locationX, locationY);
        }
    }
While at the top in the same class i declare the variables locationX and locationY
public int locationX=this.getX();
    public int locationY=this.getY();
this gives me an error saying something along the lines of "actor does not exist in world" or something.
Super_Hippo Super_Hippo

2022/2/3

#
Two things here: 1. You call ‘getWorld’ after you removed the actor from the world. So it’s no in the world anymore, so you can’t get a reference to the world with this method anymore. 2. You try to save the location of the actor in a variable. When the object is created, the actor isn’t in a world, so it can’t get its position. And even if you could, it wouldn’t update if it moves around.
//in Bullet
Rock rock = (Rock) getOneIntersectingObject(Rock.class);
if (rock != null)
{
    rock.split();
    getWorld().removeObject(this);
}
//in Rock
public void split()
{
    for (int i=0; i<2; i++)
    {
        Rock rock = new Rock();
        getWorld().addObject(rock, getX(), getY());
        rock.setRotation(Greenfoot.getRandomNumber(360));
    }
    getWorld().removeObject(this);
}
This will duplicate the rocks upon hit. Next thing to do is to pass a parameter to the constructor of the rock which decreases by one every time it gets hit.
NUKES NUKES

2022/2/5

#
I am very new to coding so bare with me if I am being stupid. But am I supposed to put the first code into a method? if so, I did that, but when I call that method, my other method which removes the bullet when touching the edge, gets broken. I currently have it like this, but I am not sure if this is correct.
public class bullet extends Actor
{
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setLocation(getX(), getY() - 5); 
        remove();
        ballsplit();
    }
    public void remove()
    {
        if (isAtEdge())
        {
           getWorld().removeObject(this); 
        }
    }
    public void ballsplit(){
        ball ball = (ball) getOneIntersectingObject(ball.class);
         if (ball != null)
        {
            ball.split();
            getWorld().removeObject(this);
        } 
    }
}
danpost danpost

2022/2/5

#
Only one class should be used to detect collision between two type objects. In your case, the better one to place the collision code in is the ball (rock) class as that is the one that does something (split). Also, if one of the two objects is removed from the world, placing the collision code in the one that stays in the world makes more sense. The only thing that should be in bullet class is movement and removal if at edge:
import greenfoot.*;

public class bullet extends Actor
{
    public void act()
    {
        setLocation(getX(), getY()-5);
        if (isAtEdge()) getWorld().removeObject(this);
    }
}
In ball (rock) class, use:
if (isTouching(bullet.class))
{
    removeTouching(bullet.class);
    split();
}
NUKES NUKES

2022/2/7

#
But that does not do what I want it to do. I have a second actor which is "ball2". 2 of those should be added when bullet touches the actor "Ball". You mentioned using the split method in ball class but the above mentioned split code just creates millions of balls right when I click run.
Roshan123 Roshan123

2022/2/7

#
Test
NUKES NUKES

2022/2/7

#
I have managed to make it work now but a different Problem now arrived. I have a "game over" screen, when the rock touches the actor, and I did that the following way.
public void hitball()
    {
        Actor ball = getOneIntersectingObject(ball.class);
        if (ball != null)
        {
            World myWorld = getWorld();
            GameOver gameover = new GameOver();
            myWorld.addObject(gameover, myWorld.getWidth()/2, myWorld.getHeight()/2);
            myWorld.removeObject(this);
        }
    }
but now the "getOneIntersectingObject" cannot function and throws me an error code that the Actor has been removed from the world. What is the best solution to bypass this? This is only for Ball1 Class. I also have the same method for each Ball Class.
Roshan123 Roshan123

2022/2/7

#
Roshan123 wrote...
Test
Sorry, I was trying to see what's going wrong with my old web browser and then unknowingly I posted irrelevant answers here. I tried to delete the message but then I saw that you(@NUKES) posted a new message, so please ignore it
danpost danpost

2022/2/7

#
NUKES wrote...
But that does not do what I want it to do. I have a second actor which is "ball2". 2 of those should be added when bullet touches the actor "Ball". You mentioned using the split method in ball class but the above mentioned split code just creates millions of balls right when I click run.
Sorry. I should have stated that the split method should be as Hippo wrote it above. Adjust it to create ball2 objects as required. If still getting millions of ball2 objects, post entire class codes for review.
NUKES NUKES

2022/2/7

#
danpost wrote...
NUKES wrote...
But that does not do what I want it to do. I have a second actor which is "ball2". 2 of those should be added when bullet touches the actor "Ball". You mentioned using the split method in ball class but the above mentioned split code just creates millions of balls right when I click run.
Sorry. I should have stated that the split method should be as Hippo wrote it above. Adjust it to create ball2 objects as required. If still getting millions of ball2 objects, post entire class codes for review.
I was able to make it work, so this is not an issue anymore :D thanks anyways.
You need to login to post a reply.