I want to check if there's still an existing actor before running another method
1 | if (someActor.getWorld() != null) |
1 | if ( ! getWorld().getObjects(SomeActor.class).isEmpty()) |
1 | if (someActor.getWorld() != null) |
1 | if ( ! getWorld().getObjects(SomeActor.class).isEmpty()) |
1 2 3 4 5 | public void exist(){ if ( ! getWorld().getObjects(runner.class).isEmpty()){ getWall(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | public class runner extends Actor{ int health = 5; public void exist(){ if ( ! getWorld().getObjects(runner.class).isEmpty()){ getWall(); } } public void act() { exist(); health(); kill(); if(getWall() == false) { move(5); } else { int turn = Greenfoot.getRandomNumber(3); if(turn == 0) { turn(90); } else if (turn == 1){ turn(-90); } else if (turn == 2){ turn(180); } else if (turn == 3){ turn(-180); } } private boolean getWall(){ { GreenfootImage myImage = getImage(); int distance = myImage.getWidth()/2; int xOffset = (int)Math.ceil(distanceToFront*Math.cos(Math.toRadians(getRotation()))); int yOffset = (int)Math.ceil(distanceToFront*Math.sin(Math.toRadians(getRotation()))); Actor wall = getOneObjectAtOffset(xOffset, yOffset, wall.class); return(wall != null); } } public void health(){ Actor shot = getOneIntersectingObject(bullet.class); if (bullet != null) { health = health - 1; } } public void kill(){ if (health == 0){ getWorld().removeObject(this); } }} |
1 | if (getWorld() == null) return; |