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

2019/1/26

How can you keep variables between worlds

1
2
iminblue iminblue

2019/1/26

#
Ok so I'm making sort-of a Dungeon Crawler and the thing is how do i remember the current HP of the player and not reinitialize it every time I enter a new world???
danpost danpost

2019/1/26

#
You could have an addPlayer method in your worlds that is given the player to add as a parameter argument:
public void addPlayer(Player player)
{
    addObject(player, 50, 300); // wherever
}
Then, when you create a world to go to from the player class:
World2 w2 = new World2();
w2.addPlayer(this);
Greenfoot.setWorld(w2);
iminblue iminblue

2019/1/27

#
Thanks that's very helpful ! I also have another question :
public boolean attack()
    {
        if (Greenfoot.isKeyDown("x"))
        {
          
           if (!getWorld().getObjects(EnemyTest.class).isEmpty())
            {snake = getWorld().getObjects(EnemyTest.class).get(0); 
            if (snake!=null && isInRange()) { if ((direction == 1 && getX()<=snake.getX()) || (direction == -1 && getX()>=snake.getX()))
            getWorld().removeObject(snake);
            } }
            if (isAttacking==true) return false;
        return true;
        }
        return false;
    }
This is the code for attacking a snake enemy. I am planning to add more types of enemies in the game . Is there any way I could group them somehow so that the enemy actor that I'm searching for (in my code "snake") could be any type of enemy existing in the world?
danpost danpost

2019/1/27

#
iminblue wrote...
<< Code Omitted >> This is the code for attacking a snake enemy. I am planning to add more types of enemies in the game . Is there any way I could group them somehow so that the enemy actor that I'm searching for (in my code "snake") could be any type of enemy existing in the world?
You could have all your enemy classes extend a general Enemy class. Then: getObjects(Enemy.class) would pick up any object created from any of its subclasses.
iminblue iminblue

2019/1/29

#
danpost wrote...
iminblue wrote...
<< Code Omitted >> This is the code for attacking a snake enemy. I am planning to add more types of enemies in the game . Is there any way I could group them somehow so that the enemy actor that I'm searching for (in my code "snake") could be any type of enemy existing in the world?
You could have all your enemy classes extend a general Enemy class. Then: getObjects(Enemy.class) would pick up any object created from any of its subclasses.
Ok , and how do I access the HP variable from any of the enemies ?
getWorld().getObjects(Enemy.class).get(0).HP--;
doesn't really seem to work.
danpost danpost

2019/1/29

#
iminblue wrote...
how do I access the HP variable from any of the enemies ? << Code Omitted >> doesn't really seem to work.
The HP variable is in the Enemy class; but the returned gotten object is in an Object (or Actor) type field. You need to cast it as an Enemy type object to be able to have it find the HP variable:
( (Enemy) getWorld().getObjects(Enemy.class).get(0) ).HP--;
iminblue iminblue

2019/1/29

#
danpost wrote...
iminblue wrote...
how do I access the HP variable from any of the enemies ? << Code Omitted >> doesn't really seem to work.
The HP variable is in the Enemy class; but the returned gotten object is in an Object (or Actor) type field. You need to cast it as an Enemy type object to be able to have it find the HP variable:
( (Enemy) getWorld().getObjects(Enemy.class).get(0) ).HP--;
The HP variable is not in the Enemy class . It's in every subclass of it because I want different enemies to have different HPs.
danpost danpost

2019/1/29

#
iminblue wrote...
The HP variable is not in the Enemy class . It's in every subclass of it because I want different enemies to have different HPs.
Put it in the Enemy class, remove them from the subclasses and set their initial values in the constructors of the subclasses.
iminblue iminblue

2019/1/29

#
danpost wrote...
iminblue wrote...
The HP variable is not in the Enemy class . It's in every subclass of it because I want different enemies to have different HPs.
Put it in the Enemy class, remove them from the subclasses and set their initial values in the constructors of the subclasses.
Ok so this is the code for the Player :
public boolean attack()
    {
        if (Greenfoot.isKeyDown("x"))
        {
          
           if (!getWorld().getObjects(Enemy.class).isEmpty())
            {enemy = getWorld().getObjects(Enemy.class).get(0); 
            if (enemy!=null && isInRange()) { if ((direction == 1 && getX()<=enemy.getX()) || (direction == -1 && getX()>=enemy.getX()))
          

  ( (Enemy) getWorld().getObjects(Enemy.class).get(0)).HP--;
//dealing damage

            } }
            if (isAttacking==true) return false;
        return true;
        }
        return false;
    }
This is in the Enemy class :
public int HP;
    public void act() 
    {
        // Add your action code here.
    }  
And this is in one of the subclasses :
if (isSpawned==false) {isSpawned=true; HP=1; return;}
        if (HP==0) die();
        else ....//code for attacking and tracking the player
The enemy is not taking damage for some reason .
danpost danpost

2019/1/29

#
iminblue wrote...
And this is in one of the subclasses : << Code Omitted >> The enemy is not taking damage for some reason .
Please provide the entire subclass for review.
iminblue iminblue

2019/1/29

#
danpost wrote...
iminblue wrote...
And this is in one of the subclasses : << Code Omitted >> The enemy is not taking damage for some reason .
Please provide the entire subclass for review.
U asked for it :)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeEnemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeEnemy extends Enemy
{
    
        private final GreenfootImage[] MoveRightAnimation =
    {
        new GreenfootImage("Snake_Move_1.png"),
        new GreenfootImage("Snake_Move_2.png"),
        new GreenfootImage("Snake_Move_3.png"),
        new GreenfootImage("Snake_Move_4.png"),
        new GreenfootImage("Snake_Move_5.png") 
    };
    
    private final GreenfootImage[] MoveLeftAnimation =
    {
        new GreenfootImage("LSnake_Move_1.png"),
        new GreenfootImage("LSnake_Move_2.png"),
        new GreenfootImage("LSnake_Move_3.png"),
        new GreenfootImage("LSnake_Move_4.png"),
        new GreenfootImage("LSnake_Move_5.png") 
    };
    
    private final GreenfootImage[] AttackRightAnimation =
    {
        new GreenfootImage("Snake_Attack_1.png"),
        new GreenfootImage("Snake_Attack_2.png"),
        new GreenfootImage("Snake_Attack_3.png"),
        new GreenfootImage("Snake_Attack_4.png"),
        new GreenfootImage("Snake_Attack_5.png"),
        new GreenfootImage("Snake_Attack_6.png"),
        new GreenfootImage("Snake_Attack_7.png"),
        new GreenfootImage("Snake_Attack_8.png"),
        new GreenfootImage("Snake_Attack_9.png") 
    };
    
   private final GreenfootImage[] AttackLeftAnimation =
    {
        new GreenfootImage("LSnake_Attack_1.png"),
        new GreenfootImage("LSnake_Attack_2.png"),
        new GreenfootImage("LSnake_Attack_3.png"),
        new GreenfootImage("LSnake_Attack_4.png"),
        new GreenfootImage("LSnake_Attack_5.png"),
        new GreenfootImage("LSnake_Attack_6.png"),
        new GreenfootImage("LSnake_Attack_7.png"),
        new GreenfootImage("LSnake_Attack_8.png"),
        new GreenfootImage("LSnake_Attack_9.png") 
    };
    
    private final GreenfootImage[] DieRightAnimation =
    {
        new GreenfootImage("Snake_Die_1.png"),
        new GreenfootImage("Snake_Die_2.png"),
        new GreenfootImage("Snake_Die_3.png"),
        new GreenfootImage("Snake_Die_4.png"),
        new GreenfootImage("Snake_Die_5.png"),
        new GreenfootImage("Snake_Die_6.png")
    };
    
    private final GreenfootImage[] DieLeftAnimation =
    {
        new GreenfootImage("LSnake_Die_1.png"),
        new GreenfootImage("LSnake_Die_2.png"),
        new GreenfootImage("LSnake_Die_3.png"),
        new GreenfootImage("LSnake_Die_4.png"),
        new GreenfootImage("LSnake_Die_5.png"),
        new GreenfootImage("LSnake_Die_6.png")
    };
    
    public int direction = 1;
    public int dy=1;
    public int distx;
    public int disty;
    public int frame=-1;
    public int speed= 2;
    public boolean isAttacking=false;
    public boolean isSpawned=false;
    public int range=40;
    public int attackDelay=-1;
    
    Actor wall;
    Actor othersnek;
    
    public void act() 
    {
        // Add your action code here.
        if (isSpawned==false) {isSpawned=true; HP=1; return;}
        if (HP==0) die();
        else{
        if (getWorld().getObjects(Player.class).isEmpty() || getWorld().getObjects(Player.class).get(0).HP==0) return;
         if (attackDelay!=-1) delayAttack();      
         else if (isInRange() && isAttacking==false && attackDelay==-1) {frame=0; isAttacking=true; 
        }
        else if (isAttacking==true) {attack();
            if (frame==15 && getWorld().getObjects(HP_HUD.class).get(0).returnHP()>0){
        getWorld().getObjects(HP_HUD.class).get(0).HP--;
        getWorld().getObjects(Player.class).get(0).isHit=true;
        }
        else if (frame==44) {frame=-1; isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack(); }}
       
        else {
        checkmove();
        move(); 
       }}
        
    }    
    
    public void checkmove()
    {   Actor player =getWorld().getObjects(Player.class).get(0); 
        if (player.getX()>getX()) direction = 1;
        else if (player.getX()<getX()) direction =-1;
        if (player.getY()>getY()) dy=1;
        else dy=-1;

        if (direction == 1 ) {animateMoveRight();}
        else animateMoveLeft();
        if(player.getX()==getX()) direction=0;
    }
    
    public boolean isInRange()
    {         Actor player = getWorld().getObjects(Player.class).get(0); 
        if (player.getX()>getX()) distx=player.getX()-getX();
        else distx=getX()-player.getX();
        if (player.getY()>getY()) disty=player.getY()-getY();
        else disty=getY()-player.getY();
      if (distx<range && disty<range) return true;
      return false;
    }
    
    public void move()
    {   Actor player =getWorld().getObjects(Player.class).get(0); 
        wall = getOneObjectAtOffset(0,-10,Wall1.class);
        if (wall==null || getY()<player.getY())
        setLocation (getX() + direction*speed,getY()+dy*speed);
    }
    
    public void attack()
    {
        if (direction==1) animateAttackRight();
        else animateAttackLeft();
    }
    
    public void die()
    {  
        if (direction == 1 ) animateDieRight();
        else animateDieLeft();
    }
    
    public void delayAttack()
    {attackDelay++;
        if(attackDelay==30) attackDelay=-1;
    }
    
    public void animateMoveRight()
    {
        frame++;
        setImage(MoveRightAnimation[frame/10]);
        if (frame==49) frame=-1;
    }
    
    public void animateMoveLeft()
    {
        frame++;
        setImage(MoveLeftAnimation[frame/10]);
        if (frame==49) frame=-1;
    }
    
    public void animateAttackRight()
    {
    frame++;
        setImage(AttackRightAnimation[frame/5]);
        if (frame==44) {frame=-1;isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack();}
    }
    
    public void animateAttackLeft()
    {
    frame++;
        setImage(AttackLeftAnimation[frame/5]);
        if (frame==44) {frame=-1;isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack();}
    }
    
    public void animateDieRight()
    {
        if (frame<59) frame++;
        setImage(DieRightAnimation[frame/10]);
    }
    
    public void animateDieLeft()
    {
        if (frame<59) frame++;
        setImage(DieLeftAnimation[frame/10]);
    }
}
danpost danpost

2019/1/29

#
Remove lines 83 and 93 and add the following new block of code:
public SnakeEnemy()
{
    HP = 1;
}
What is the HP_HUD class for?
iminblue iminblue

2019/1/30

#
danpost wrote...
Remove lines 83 and 93 and add the following new block of code:
public SnakeEnemy()
{
    HP = 1;
}
What is the HP_HUD class for?
HP_HUD is the actor that bascially keeps track of the player's hp . This way it's easier to access and modify and the player's code has space for something else
iminblue iminblue

2019/1/30

#
Ok but how can I hit multiple enemies at once ? I mean i use the get(0) function and it will only deal damage to the first enemy spawned even if it is dead.
danpost danpost

2019/1/30

#
iminblue wrote...
Ok but how can I hit multiple enemies at once ? I mean i use the get(0) function and it will only deal damage to the first enemy spawned even if it is dead.
In the Player class attack method, you could use a structure like this:
java.util.List enemies = getWorld().getObjects(Enemy.class);
if (!enemies.isEmpty())
{
    for (Object obj : enemies)
    {
        Enemy enemy = (Enemy)obj;
        if (....) enemy.HP--;
    }
    return !isAttacking;
}
There are more replies on the next page.
1
2