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.
I thank you yet again for helping me out!
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); } } } } |