So I went on expanding my game, and then I ran into another problem. I tried to make it so you lose health when an enemy touches you, but I get an error on the reference again when trying to compile my code.
This is my current code in Enemies:
This is what I have in Player:
And it might be useful to also show Bullet:
The error is on line 19 in the Enemies, it says it "cannot find symbol- method isAttacked()", which I have specified in the Player.
I hope you guys will be able to help me again.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public static Actor player; public void act() { } public static void setPlayer (Actor a) { player = a; } public void attackPlayer() { if (isTouching (Player. class )) { World world = getWorld(); world.removeObject( this ); player.isAttacked(); } } |
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 | int cooldown = 0 ; int health = 3 ; public void act() { //Move when certain keys are pressed, but only when not at the edge if (Greenfoot.isKeyDown( "a" ) && getX() != 0 ) { setLocation(getX()- 2 , getY()); } if (Greenfoot.isKeyDown( "d" ) && getX() != 600 ) { setLocation(getX()+ 2 , getY()); } if (Greenfoot.isKeyDown( "w" ) && getY() != 0 ) { setLocation(getX(), getY()- 2 ); } if (Greenfoot.isKeyDown( "s" ) && getY() != 400 ) { setLocation(getX(), getY()+ 2 ); } //Create a bullet when the spacebar is pressed if (Greenfoot.isKeyDown( "space" )) { if (cooldown <= 0 ) { World world = getWorld(); world.addObject( new Bullet(), getX(), getY()); cooldown = 25 ; } } cooldown--; } public void isAttacked() { health--; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | int age = 0 ; public void act() { MouseInfo mouse = Greenfoot.getMouseInfo(); if (age == 0 ) { turnTowards(mouse.getX(),mouse.getY()); } age += 1 ; move( 3 ); shotEnemy(); } public void shotEnemy() { if (isTouching (Enemies. class )) { Enemies enemy = (Enemies)getOneIntersectingObject (Enemies. class ); World world = getWorld(); world.removeObject( this ); world.removeObject(enemy); } } |