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.
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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:
1
2
3
4
5
6
7
8
9
10
11
12
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

#
1
2
3
4
5
6
//in Enemy
public void takeDamage(int amount)
{
    health -= amount;
    if (health < 1) getWorld().removeObject(this);
}
1
2
3
4
5
6
7
8
//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:
1
2
3
4
5
6
7
8
9
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.