I have a game with 2 players, and I duplicated 3 actors to match the ones the first one has, and they do appear in the World, but dissapear after I press Run or Act. Why is that and how can I fix it?
![Twitter](/assets/twitter-4e19209ef84344ee0c433f4c7bad8d49.png)
![Twitter.hover](/assets/twitter.hover-1fb19a5bafc50deace8f88eaec867845.png)
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 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. */ public MyWorld() { super ( 1168 , 750 , 1 ); AttackD mainAttackD = new AttackD(); HealthBar healthbar = new HealthBar(); HealthBar2 healthbar2 = new HealthBar2(); Player2 mainPlayer2 = new Player2(); Player mainPlayer = new Player(); addObject(healthbar, 240 , 100 ); addObject(healthbar2, 940 , 100 ); addObject(mainPlayer, 200 , 500 ); addObject(mainPlayer2, 1000 , 500 ); addObject( new Attack2(), 100 , 100 ); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } } My Wolrd |
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 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class HealthBar2 extends Actor { /** * */ public HealthBar2() { GreenfootImage image3 = getImage(); image3.scale( 500 , 500 ); setImage(image3); } /** * Act - do whatever the HealthBar2 wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { int health = getWorld().getObjects(Player. class ).get( 0 ).health; setLocation( 330 - health, 100 ); } } The Healthbar |
1 | System.out.println( "" +numberOfObjects()); |
1 2 3 4 | public void act() { System.out.println( "" +numberOfObjects()); } |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Player extends Actor { public int health = 100 ; public int damage = 5 ; private int power = 0 ; private int attackDelay = 0 ; /** * */ public Player() { GreenfootImage image = getImage(); image.scale( 500 , 500 ); setImage(image); } /** * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ final public void act() { if (attackDelay > 0 ) { attackDelay = attackDelay - 1 ; return ; } int dx = 0 ; if (Greenfoot.isKeyDown( "d" )) { dx = dx + 1 ; } if (Greenfoot.isKeyDown( "a" )) { dx = dx - 1 ; } setLocation(getX() + 5 * dx, getY()); if ( "f" .equals(Greenfoot.getKey())) { if ( ! Greenfoot.isKeyDown( "s" )) { atac(); } if (Greenfoot.isKeyDown( "s" )) { atacDown(); } } } /** * */ private void atac() { attackDelay = 20 ; getWorld().addObject( new Attack(), getX() + 50 , getY()); } /** * */ private void atacDown() { attackDelay = 20 ; getWorld().addObject( new AttackD(), getX() + 50 , getY() + 50 ); } /** * */ public void getDamaged() { if (isTouching(Attack. class )) { Player2 p2 = (Player2)getWorld().getObjects(Player2. class ).get( 0 ); health = health - p2.damage2; } } } |
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | import lang.stride.*; I dont really know how I could see which one dissapeared because they just dont appear on screen import java.util.*; import greenfoot.*; /** * */ public class Player2 extends Actor { private int health2 = 100 ; public int damage2 = 5 ; private int power2 = 0 ; private int attackDelay = 0 ; /** * */ public Player2() { GreenfootImage image2 = getImage(); image2.scale( 500 , 500 ); setImage(image2); } /** * Act - do whatever the Player2 wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ final public void act() { if (attackDelay > 0 ) { attackDelay = attackDelay - 1 ; return ; } int dx = 0 ; if (Greenfoot.isKeyDown( "right" )) { dx = dx + 1 ; } if (Greenfoot.isKeyDown( "left" )) { dx = dx - 1 ; } setLocation(getX() + 5 * dx, getY()); if ( "x" .equals(Greenfoot.getKey())) { if ( ! Greenfoot.isKeyDown( "down" )) { atac(); } if (Greenfoot.isKeyDown( "down" )) { atacDown(); } } } /** * */ private void atac() { attackDelay = 20 ; getWorld().addObject( new Attack2(), getX() + 50 , getY()); } /** * */ private void atacDown() { attackDelay = 20 ; getWorld().addObject( new AttackD2(), getX() - 50 , getY() + 50 ); } /** * */ public void getDamaged() { if (isTouching(Attack. class )) { Player p1 = (Player)getWorld().getObjects(Player. class ).get( 0 ); health2 = health2 - p1.damage; } } } |
1 2 3 4 5 6 7 8 | public void act() { System.out.println( "" ); for (Object obj : getObjects( null )) { System.out.println(obj.getClass().getTypeName()); } } |
1 2 3 4 5 | public void removeObject(Actor object) { System.out.println( "A " +object.getClass().getTypeName()+ "type object was removed from the world." ); super .removeObject(object); } |
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 | import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Attack2 extends Actor { private long lastAdded = System.currentTimeMillis(); /** * */ public Attack2() { GreenfootImage image2 = getImage(); getImage().setTransparency( 30 ); image2.scale( 500 , 500 ); setImage(image2); } /** * Act - do whatever the Attack2 wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { long curTime = System.currentTimeMillis(); if (curTime >= lastAdded + 300 ) { getWorld().removeObject( this ); } } } |
1 2 3 4 5 6 7 | private int timer = 30 ; // about 1/2 second of time (about 60 frames per second at normal scenario speed of 50) public void act() { timer = timer - 1 ; if (timer == 0 ) getWorld().removeObject( this ); } |