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

2020/3/26

Splitting big rocks

Libraoct7 Libraoct7

2020/3/26

#
Hi, I am trying to program a rocket shooting game. I have two kind of rocks on space that should be shot by the space rocket, big rocks and small rocks. I want the big rocks to split into three rocks when they are hit with a shot. The game works fine with no errors. However when I try to shoot the big rocks, sometimes they split and sometimes they don't. When I say sometimes I mean when I reset the game and run it again, the same rock that splitted before it is not splitting again. I don't know why this is happening. Please help. these are the codes I used in the big rock object. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class rock1 here. * * @author (your name) * @version (a version number or a date) */ public class BigRock extends Actor { public void act() { World w = getWorld(); move(-1); if(isTouching(shots.class)) { w.addObject(new SmallRock(), getX(), getY()); w.addObject(new SmallRock(), getX()+40, getY()-40); w.addObject(new SmallRock(), getX()+40, getY()+40); } else if(isAtEdge()) { setLocation(600,Greenfoot.getRandomNumber(600)); } } public BigRock() { getImage().scale(getImage().getWidth()/6, getImage().getHeight()/6); } }
RcCookie RcCookie

2020/3/27

#
I don’t know the code of the small rock class but the only problem I could find is that you aren’t removing the bigRock after spawning the small ones. They might overlap and sometimes show the one, sometimes the other object
danpost danpost

2020/3/28

#
You may want to remove the touching shot (and add small rocks), then remove the big rock (this). The shot was probably removing the small rocks immediately (because it was still in the world). You may want to adjust where small rock #3 is added into the world at (it currently is set to the same location as small rock #2).
You need to login to post a reply.