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?
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 Wolrdimport 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 HealthbarSystem.out.println(""+numberOfObjects());public void act()
{
System.out.println(""+numberOfObjects());
}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;
}
}
}
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;
}
}
}
public void act()
{
System.out.println("");
for (Object obj : getObjects(null))
{
System.out.println(obj.getClass().getTypeName());
}
}public void removeObject(Actor object)
{
System.out.println("A "+object.getClass().getTypeName()+ "type object was removed from the world.");
super.removeObject(object);
}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);
}
}
}
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);
}