This site requires JavaScript, please enable it in your browser!
Greenfoot back
ThatAsianPixel
ThatAsianPixel wrote ...

2020/3/7

Please help

ThatAsianPixel ThatAsianPixel

2020/3/7

#
So I'm trying to get one of the values out of the stats ArrayList from a different class than the one I'm currently in. I want to get the int in the stats of the specifiedCPU that is declared in BattlePage. Problem is when I assign the getCPU() to the variable enemy in the current class and then try to do getStats(), it gives me an error. I think this makes sense, so please help. Current class:
public void Attack()
    {
        Moves m1 = new Moves("Skull Bash",130,100,"Normal","Physical");
        Moves m2 = new Moves("Hydro Pump",110,80,"Water","Special");
        Moves m3 = new Moves("Ice Beam",90,100,"Ice","Special");
        Moves m4 = new Moves("Weather Ball",50,100,"Normal","Special");
        Moveset ms = new Moveset(m1,m2,m3,m4);
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter: Fight or Run");
        if(scan.nextLine().toLowerCase()=="fight")
        {
             System.out.println("Choose a move: ");
             for(int i = 0; i < 4;i++)
             {
                 ArrayList<Moves> x = ms.getMoveset();
                 System.out.println("- " + x.get(i).getName());
             }
             if(scan.nextLine().toLowerCase() == "skull bash")
             {
                 int power = m1.getPower();
                 double random = (Math.random()*0.85)+0.15;
                 Actor enemy = BattlePage.getCPU();
                 ArrayList<Integer> enemyDefense = getStats();                 
             }
        }
        
    }
    
    public ArrayList<Integer> getStats()
    {
        return stats;
    }
}
BattlePage class:
public class BattlePage extends World
{
    private static Actor selectedPlayer;
    private static Actor selectedCPU;
    /**
     * Constructor for objects of class BattlePage.
     * 
     */
    public BattlePage()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setBackground("battle_background.jpg");
        
        addObject(selectedCPU,500,100);
        addObject(selectedPlayer,150,275);
    }
    
    public static Actor getPlayer()
    {
        return selectedPlayer; 
    }
    public static Actor getCPU()
    {
        return selectedCPU;
    }
    
    public static void setPlayer(Actor player)
    {
        selectedPlayer = player;
    }
    public static void setCPU(Actor CPU)
    {
        selectedCPU = CPU;
    }
}
danpost danpost

2020/3/8

#
Lines 21 thru 24 in your current class look to be totally independent of each other (just gathering data, it appears). From what I can tell, none should produce an error. What error are you getting? Copy/paste error message and show entire current class codes.
ThatAsianPixel ThatAsianPixel

2020/3/8

#
This is what I was trying to do, and I just realized that the current class sample is missing what I'm trying to do. Here is the whole thing:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Blastoise here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Blastoise extends Actor
{
    private String name;
    private String type;
    private int health;
    private ArrayList<Integer> stats;
    private ArrayList<String> strength;
    private ArrayList<String> weakness;
    private int time;
    /**
     * Act - do whatever the Blastoise wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        time++;
        
    }
    
    public Blastoise()
    {
        name = "Blastoise";
        type = "Water";
        health = 362;
        stats.add(291);stats.add(328); stats.add(295);
        stats.add(339);stats.add(280);
        strength.add("Fire");
        weakness.add("Grass");
    }
    
    public void Attack()
    {
        Moves m1 = new Moves("Skull Bash",130,100,"Normal","Physical");
        Moves m2 = new Moves("Hydro Pump",110,80,"Water","Special");
        Moves m3 = new Moves("Ice Beam",90,100,"Ice","Special");
        Moves m4 = new Moves("Weather Ball",50,100,"Normal","Special");
        Moveset ms = new Moveset(m1,m2,m3,m4);
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter: Fight or Run");
        if(scan.nextLine().toLowerCase()=="fight")
        {
             System.out.println("Choose a move: ");
             for(int i = 0; i < 4;i++)
             {
                 ArrayList<Moves> x = ms.getMoveset();
                 System.out.println("- " + x.get(i).getName());
             }
             if(scan.nextLine().toLowerCase() == "skull bash")
             {
                 
                 int power = m1.getPower();
                 double random = (Math.random()*0.85)+0.15;
                 Actor enemy = BattlePage.getCPU();
                 ArrayList<Integer> enemydefense = enemy.getStats();                 
             }
        }
        
    }
    
    public ArrayList<Integer> getStats()
    {
        return stats;
    }
}
The error is that it can't find a method called getStats() for the Actor enemy.
danpost danpost

2020/3/8

#
What are all possible types allowed for selectedCPU?
ThatAsianPixel ThatAsianPixel

2020/3/8

#
types? if you mean the things that it can be assigned to, it's Blastoise, Charizard, and Venusaur. They all have the same code as Blastoise (the current class)
danpost danpost

2020/3/8

#
ThatAsianPixel wrote...
types? if you mean the things that it can be assigned to, it's Blastoise, Charizard, and Venusaur. They all have the same code as Blastoise (the current class)
Then you will need to determine which is assigned to selectedCPU before you can call getStats on it:
ArrayList<Integer> enemydefense = null;
if (enemy instanceof Blastoise) enemydefense = ((Blastoise)enemy).getStats();
else ...
You need to login to post a reply.