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!
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); } } } }