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

2023/12/6

help having projectiles act how i want

Lonelyf60 Lonelyf60

2023/12/6

#
i want to have them move a certain range before exploading and i want them to shoot in the direction of the mouse while also having a 30 second delay ty in advance for the help heres my code: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class dragonclaw here. * * @author (your name) * @version (a version number or a date) */ public class dragonclaw extends attack { /** * Act - do whatever the dragonclaw wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX() + speed, getY()); checkBoundaries(); destroyEnemies(); } //we add a method "checkBoundaries()" that destroys bullets that are off screen. public void checkBoundaries() { if(getX() > getWorld().getWidth() - 1) getWorld().removeObject(this); else if(getX() < 1) getWorld().removeObject(this); if(getY() > getWorld().getHeight() - 1) getWorld().removeObject(this); else if(getY() < 1) getWorld().removeObject(this); } //"destroyEnemies()" destroys enemies. public void destroyEnemies() { //"Enemy" can be any class that you want the bullet to destroy. Actor gible = getOneIntersectingObject(gible.class); if(gible != null) { getWorld().removeObject(gible); getWorld().removeObject(this); } } private int speed = 10; }
danpost danpost

2023/12/7

#
Lonelyf60 wrote...
i want to have them move a certain range before exploading and i want them to shoot in the direction of the mouse while also having a 30 second delay ty in advance for the help heres my code: << Code Omitted >>
Where does the 30-second delay come in? When does it start? What, if anything, happens during the delay? And what happens when the 30 seconds is up? There are two things about the code provided that should be noted. One is your loose use of the getWorld method throughout the class. Once: the line
getWorld().removeObject(this);
is executed, that method will thereafter return a 'null' value. That means you cannot execute any method or access any fields using it or an error (NullPointerException) will ensue. So, in the checkBoundaries' method, the second if should have else before it as well.. As an alternative, you can combine the logic checking to:
boolean a = getX() == 0;
boolean b = getY() == 0;
boolean c = getX() == getWorld().getWidth()-1;
boolean d = getY() == getWorld().getHeight()-1;
if (a || b || c || d) getWorld().removeObject(this);
or,
if (getX()%(getWorld().getWidth()-1) == 0 || getY()%(getWorld().getHeight()-1) == 0) getWorld().removeObject(this);
Another place where this comes into play is the order of calls in the act method. If the second act method executes the removal of the object, then a error will occur during the call to the destroyEnemies method. Switching the two method won't work because this method also may remove this object (which will cause an error in the first method). You will have to check the returned value of getWorld for a null value before moving on to the second method call:
checkBoundaries();
if (getWorld() == null) return;
destroyEnemies();
You need to login to post a reply.