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

2018/4/22

How to remove bullet when it his the edge of the screen?

IlikeThePrequels IlikeThePrequels

2018/4/22

#
^
IlikeThePrequels IlikeThePrequels

2018/4/22

#
This is my code for the bullet and it causes it to disappear but my game suddenly stops. Help? public void deleteStinger() { if(atWorldEdge()) { getWorld().removeObject(this); } } public void act() { setRotation(direction); move(speed); move(5); checkCollision(); deleteStinger(); }
danpost danpost

2018/4/22

#
And the error message is ...?
IlikeThePrequels IlikeThePrequels

2018/4/22

#
danpost wrote...
And the error message is ...?
There is no error message. The game literally pauses for some reason.
danpost danpost

2018/4/22

#
IlikeThePrequels wrote...
There is no error message. The game literally pauses for some reason.
What class does the Stinger class extend?
IlikeThePrequels IlikeThePrequels

2018/4/22

#
The stinger class extends the animal class in order for atWorldEdge to work. Here is all the code for the stinger and Bee2 (enemy) classes. import greenfoot.*; import java.util.List; /** * Write a description of class Ladybug1 here. * * @author (your name) * @version (a version number or a date) */ public class Bee2 extends Actor { private int beehivesEaten; private GreenfootImage image3; private GreenfootImage image4; /** * Act - do whatever the Ladybug1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Bee2() { beehivesEaten = 0; image3 = new GreenfootImage("bee.png"); image4 = new GreenfootImage("bee2.png"); setImage(image3); } public void act() { followBeehive(); lookForBeehive(); switchImage(); } public void followBeehive() { turnTowards(150,500); Actor beehive = (Actor) getOneIntersectingObject(Beehive.class); move(3); } public void attackBeehive() { if(!getObjectsInRange(500, Beehive.class).isEmpty()) { Actor beehive=(Actor) getObjectsInRange(200, Beehive.class).get(0); if(beehive !=null) { followBeehive(); } } } private void lookForBeehive() { if (isTouching(Beehive.class)) { Greenfoot.playSound("munch.mp3"); removeTouching(Beehive.class); Greenfoot.setWorld(new GameOverScreen()); Greenfoot.stop(); } } public void switchImage() { if (getImage() == image3) { setImage(image4); } else { setImage(image3); } } } // References //www.youtube.com/watch?v=DoElE2f_QdM //www.youtube.com/watch?v=aiNg8ChsyUg //https://www.youtube.com/watch?v=xD98iODedWk //www.greenfoot.org/topics/60455/0 //https://www.greenfoot.org/topics/60455/0 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class bullet here. * * @author (your name) * @version (a version number or a date) */ public class Stinger extends Animal { private int direction, speed; public Stinger(int dir) { direction = dir; speed = 5; } public void act() { setRotation(direction); move(speed); move(5); checkCollision(); if(atWorldEdge()) { getWorld().removeObject(this); } } public void checkCollision() { Actor bee2 = (Actor) getOneIntersectingObject(Bee2.class); if(bee2 != null) { getWorld().removeObject(bee2); Fields fields = (Fields)getWorld(); getWorld().removeObject(this); Greenfoot.playSound("splat.mp3"); Counter counter = fields.obtainCounter(); counter.numberCount(+1); // error I had Counter capitalized giving a nonstatic error } } }
danpost danpost

2018/4/23

#
Looks like the if in your Stinger class act method should be:
if (getWorld() != null && atWorldEdge())
because the actor could be removed from the world in the checkCollision method.
IlikeThePrequels IlikeThePrequels

2018/4/23

#
danpost wrote...
Looks like the if in your Stinger class act method should be:
if (getWorld() != null && atWorldEdge())
because the actor could be removed from the world in the checkCollision method.
Thanks Im in tears right now.
danpost danpost

2018/4/23

#
IlikeThePrequels wrote...
Thanks Im in tears right now.
What exactly does that mean -- it works???
IlikeThePrequels IlikeThePrequels

2018/4/23

#
danpost wrote...
IlikeThePrequels wrote...
Thanks Im in tears right now.
What exactly does that mean -- it works???
Yes. Lol. I have spent alot of time invested in the game and its finally completed. Its a sigh of relief. However, I still dont understand how myworld can not be equal to nothing.
danpost danpost

2018/4/23

#
IlikeThePrequels wrote...
I still dont understand how myworld can not be equal to nothing.
The method getWorld returns the value of a field that retains the world the actor is currently in. Once you use removeObject(this), that field is set to null. It is not that the world no longer exists; it means that, for all intensive purposes, it does not exist for that actor.
You need to login to post a reply.