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

2020/3/23

Dealing damage to Enemys

Zyrtex1 Zyrtex1

2020/3/23

#
Hello yet again. My problem today is: How can my Actor, when pressing space, deal damage to an enemy, equal to the int dmg = 1 I gave him? And: How can I achieve, that when the enemy is hit by the attack (and not when touching Link.class) that it receives the damage? Because right now the enemy receives the damage always when touching link, and not the attack.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.*;
/**
 * Link is the main character in this game. 
 * Link is controlled by the player using the Arrow Keys and the Space Bar.
 * Link can receive a movement-boost by pressing the Shift-Button.
 * 
 * @author (Tommy Simson & Andreas Fedorov) 
 * @version (1.0)
 */
 
 
public class Link extends Actor 
{  
    /**
     * Act - do whatever Link wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    private GreenfootImage Down0 = new GreenfootImage("linkDown0.png");
    private GreenfootImage Down1 = new GreenfootImage("linkDown1.png");
    private GreenfootImage Left0 = new GreenfootImage("linkLeft0.png");
    private GreenfootImage Left1 = new GreenfootImage("linkLeft1.png");
    private GreenfootImage Right0 = new GreenfootImage("linkRight0.png");
    private GreenfootImage Right1 = new GreenfootImage("linkRight1.png");
    private GreenfootImage Up0 = new GreenfootImage("linkUp0.png");
    private GreenfootImage Up1 = new GreenfootImage("linkUp1.png");
    private GreenfootImage swordDown = new GreenfootImage("linkSwordDown.png");
    private GreenfootImage swordLeft = new GreenfootImage("linkSwordLeft.png");
    private GreenfootImage swordUp = new GreenfootImage("linkSwordUp.png");
    private GreenfootImage swordRight = new GreenfootImage("linkSwordRight.png");
     
    //Links health
    public static int health = 5;
    //Variable for Animation
    private static int spin = 20;
    
    //Links attack
    public static int dmg = 1;
    
    
    public static boolean isAttacking = false;
    public static boolean isGameOver = false;
    public static boolean isKnockback = false;
    public static GameOverScreen gos = new GameOverScreen();
     
    public void act() 
    {
        keyMove();
        attack();
        
        
        
 
       //Checks if Link has ran out of hearts --> Leading to GameOver
        if(isGameOver == true){
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            Greenfoot.delay(spin);
            setImage("linkLeft0.png");
            Greenfoot.delay(spin);
            setImage("linkUp0.png");
            Greenfoot.delay(spin);
            setImage("linkRight0.png");
            Greenfoot.delay(spin);
            setImage("linkDown0.png");
            setImage("deadLink.png");
            //move(Direction.DOWN, 500);
            this.getImage().setTransparency(0);
            getWorld().addObject(gos, 400, 300);
             
            Greenfoot.stop();
        }
        
    }   
     
    public void keyMove()
    {
      if(Greenfoot.isKeyDown("Right")){
           setLocation(getX()+5, getY());
            if (getImage() == Right0) {
            setImage (Right1);
        }   else {
            setImage(Right0);
        }   
        }
          
        if(Greenfoot.isKeyDown("Left")){
           setLocation(getX()-5, getY());
            if (getImage() == Left0) {
            setImage (Left1);
        }   else {
            setImage(Left0);
             
        }   
        }
         
         
        if(Greenfoot.isKeyDown("Up")){
           setLocation(getX(), getY()-5);
            if (getImage() == Up0) {
            setImage (Up1);
        }   else {
            setImage(Up0);
        }   
        }
          
        if(Greenfoot.isKeyDown("Down")){
           setLocation(getX(), getY()+5);
            if (getImage() == Down0) {
            setImage (Down1);
        }   else {
            setImage(Down0);
            Greenfoot.delay(1);
        }
        }
    }
     
    public void attack() {
            if(Greenfoot.isKeyDown ("space")){
                if (getImage() == Right0 || getImage() == Right1) {
               setImage(swordRight);
            }
            else {
               if (getImage() == Left0 || getImage() == Left1) {
                   setImage (swordLeft);
                }
               if (getImage() == Up0 || getImage() == Up1) {
                   setImage (swordUp);
                }
               if (getImage() == Down0 || getImage() == Down1) {
                   setImage (swordDown);
                }
            }
            Octorock enemy = (Octorock) getOneIntersectingObject(Octorock.class);
            Greenfoot.delay(10);
            if (getImage() == swordUp) {
                setImage (Up0);
            }
            if (getImage() == swordDown) {
                setImage (Down0);
            }
            if (getImage() == swordLeft) {
                setImage (Left0);
            }
            if (getImage() == swordRight) {
                setImage (Right0);
            }
        }
    }
}   
I thank you yet again for helping me out!
Zyrtex1 Zyrtex1

2020/3/23

#
oh maybe I should post the code of the enemy here too then:
public void takeDamage() {
        if(isTouching(Link.class)) {
            if(health > 0) {
                health = health - 1;
            }
            else {
               if(health == 0) {
                   getWorld().removeObject(this);
                } 
            }
        }
    }
Basically dealing the damage once when space is pressed would be what I am searching for.
Super_Hippo Super_Hippo

2020/3/23

#
//in Enemy
public void takeDamage(int amount)
{
    health -= amount;
    if (health < 1) getWorld().removeObject(this);
}
//in Link
if (<should attack>)
    Enemy enemy = (Enemy) getOneIntersectingObject(Enemy.class);
    if (enemy != null)
    {
        enemy.takeDamage(dmg);
    }
}
‘Greenfoot.isKeyDown ("space")’ won’t be enough for the <should attack> part though since you should remove the ‘Greenfoot.delay’ part (or want to remove it at some point). Maybe you want to execute it only if the key changed from not-pressed to pressed state or only once every x act cycles.
Zyrtex1 Zyrtex1

2020/3/23

#
How would I do that?
RcCookie RcCookie

2020/3/23

#
You need to have a boolean type variable that saves weather the key was pressed before, like this:
private boolean pressed; //initialize it to false
//in method
if(Greenfoot.isKeyDown(“space”)){
    if(!pressed){
        //attack code
        pressed = true;
    }
}
else pressed = false;
Zyrtex1 Zyrtex1

2020/3/25

#
okay thank you for yoir help I'll try to get it into my game
Zyrtex1 Zyrtex1

2020/3/25

#
okay thank you for your help! It works just as I wanted!
You need to login to post a reply.