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

2023/2/16

My Actors dissapear after pressing act or run

Shaormentiul Shaormentiul

2023/2/16

#
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?
danpost danpost

2023/2/16

#
Shaormentiul wrote...
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?
Show codes. Your game world codes, initially.
Shaormentiul Shaormentiul

2023/2/16

#
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
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
danpost danpost

2023/2/17

#
Shaormentiul wrote...
<< Code Omitted >> My Wolrd << Code Omitted >> The Healthbar
Add the following line (temporarily) at line 27 in your MyWorld class:
System.out.println(""+numberOfObjects());
Your terminal window should show up with a number. I believe it should show a number 5. Then, remove the line and add to the class the following code:
public void act()
{
    System.out.println(""+numberOfObjects());
}
You should get the same number as before, before hitting Run or Act. Now, click on the Act button, counting how many times you have to click it before the number changes. How many times did you click the Act button before the number changed?
Shaormentiul Shaormentiul

2023/2/20

#
I had to press it twice before the number changed from 5 to 4
danpost danpost

2023/2/20

#
Shaormentiul wrote...
I had to press it twice before the number changed from 5 to 4
Okay. Need to see codes of Player and Player2. Do you know which actor was removed on the second act step?
Shaormentiul Shaormentiul

2023/2/20

#
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;
        }
    }
}
danpost danpost

2023/2/20

#
Shaormentiul wrote...
<< Code Omitted >> << Code Omitted >>
I do not see any code removing any objects. Do any of your other class codes remove any objects from the world?
danpost danpost

2023/2/20

#
Shaormentiul wrote...
I dont really know how I could see which one dissapeared because they just dont appear on screen
You could change the act in MyWorld to this to check it out:
public void act()
{
    System.out.println("");
    for (Object obj : getObjects(null))
    {
        System.out.println(obj.getClass().getTypeName());
    }
}
Again, use Act button (not Run). Or, you could add the following to specifically see which one was removed:
public void removeObject(Actor object)
{
    System.out.println("A "+object.getClass().getTypeName()+ "type object was removed from the world.");
    super.removeObject(object);
}
Shaormentiul Shaormentiul

2023/2/21

#
The one that is removed is Attack2
danpost danpost

2023/2/21

#
danpost wrote...
I do not see any code removing any objects. Do any of your other class codes remove any objects from the world?
Where do you have an Attack2 object being removed? Show the code of the class it is in?
Shaormentiul Shaormentiul

2023/2/21

#
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);
        }
    }
}
Shaormentiul Shaormentiul

2023/2/21

#
It might be because I used the same variables in Attack ( the one that I duplicated) but they are private so I dont think thats the problem
Shaormentiul Shaormentiul

2023/2/21

#
And Attack2 is just an object I used to see if it would dissapear as well Its not supposed to actually stay on the Scene
danpost danpost

2023/2/21

#
Shaormentiul wrote...
It might be because I used the same variables in Attack ( the one that I duplicated) but they are private so I dont think thats the problem And Attack2 is just an object I used to see if it would dissapear as well Its not supposed to actually stay on the Scene
The problem is with the code in your Attack and Attack2 act methods. The 300 milliseconds is 3/10 of one second -- and, you have the object being removed from the time it was created. Well, the internal clock of the computer continues to run even when your scenario is not running. So, of course, it will be removed pretty much immediately when you click the Run button. Better would be to use an int timer field to count some act steps before removing the object than to use system time:
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);
}
You need to login to post a reply.