0

first question, sorry if it's a bad one i'm new to OOP. I have two instances that i'm using in the method, one passed as an argument called in another class where the instance is made, and one that gets created in the method. I then try calling a method from a seperet class that makes use of those instances, but get the error saying it cant be referenced from a static location?

any help much appreciated

public void displayRoom(character c){
    if(ItemsInRoom.size() > 0){
        for(int i = 0; i < ItemsInRoom.size(); i++){
            System.out.println(ItemsInRoom.get(i).getName());
        }
}
    if(enemies.size() > 0){
        for(int i = 0; i < enemies.size(); i++){
            System.out.println(enemies.get(i).getName());
            Random rand = new Random();
            int rand_3 = rand.nextInt(10);
            if(rand_3 > 5){
                character temp = enemies.get(0);
                character.attack(c, temp);//ERROR IS HERE

the method I'm trying to call that's in another class:

public void attack(character c, character e){
        System.out.println(e.getName() + "has attacked you!");
        c.setHealth(-3);
        System.out.println("Health: " + c.getHealth());
  • 3
    You wrote `character.attack(c, temp)` but I think you meant `temp.attack(c, temp)`. Your instance is doing that attacking - not your entire class. Please capitalize class names and start variables with lower (camel) case to avoid confusion. – sleepToken Mar 05 '20 at 15:52
  • ahh yeh that's exactly it cheers haha. – QuestionHaver99 Mar 05 '20 at 15:55
  • 2
    appreciate it lads, will start capitalizing class names and lowercasing variables. – QuestionHaver99 Mar 05 '20 at 15:57
  • Not related to your problem but: There is no need to pass two characters into the attack method. You just need to pass one because as an instance method it already belongs to a character, for example just path the character that you want to attack. So in the end you would have something like `public void attack(character targetToAttack)` using it like `warrior.attack(priest)` where `warrior` and `priest` are `character` objects. – OH GOD SPIDERS Mar 05 '20 at 15:58
  • not sure how to reply to specific people on stackoverflow but cheers oh god spiders for the tips – QuestionHaver99 Mar 05 '20 at 16:13

0 Answers0