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

2021/11/9

i get a error when trying to use ARRAYS

Aaron-aid Aaron-aid

2021/11/9

#
i get the error `. java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:657) at java.util.ArrayList.get(ArrayList.java:433) at you.act(you.java:43) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) when i try to run
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
 * Write a description of class you here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class you extends ProjectFEAR
{
    int speed = 3;
    List sus;
    
    /**
     * Act - do whatever the you wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    
    {
        
        if(Greenfoot. isKeyDown("A"))
        {
            setLocation(getX()-speed, getY());
        }
        if(Greenfoot. isKeyDown("D"))
        {
            setLocation(getX()+speed, getY());
        }
        if(Greenfoot. isKeyDown("W"))
        {
            setLocation(getX(), getY()-speed);
        }
        if(Greenfoot. isKeyDown("S"))
        {
            setLocation(getX(), getY()+speed);
        }
        if(isTouching(enemys.class))
        {
            sus = getObjectsInRange(20, enemys.class);
            
            sus.add(sus.get(0));
        }
        
    }    
}
danpost danpost

2021/11/10

#
What exactly are you trying to accomplish with that code. It looks like you are trying to add to the list an object that is already in the list (which does not make sense); that is:
sus.add(sus.get(0));
which says to get the first element in the list (which may not exist -- hence the error) and add it to the same list.
Aaron-aid Aaron-aid

2021/11/10

#
im trying to add a object that the player is touching to the list so that if the player touches the obj it adds it to a list and you can fight it, but I want to make reusable code so I don't have to code each battle screen
Aaron-aid Aaron-aid

2021/11/10

#
i fixed the error with this
list.add(getIntersectingObjects(enemys.class));
            sus = list.get(0).getClass();
but now how would i do sus.health - 10?
Aaron-aid Aaron-aid

2021/11/10

#
cuz every item in the enemy class has a health int
danpost danpost

2021/11/10

#
Aaron-aid wrote...
im trying to add a object that the player is touching to the list so that if the player touches the obj it adds it to a list and you can fight it, but I want to make reusable code so I don't have to code each battle screen
List<enemys> sus = new ArrayList<enemys>();

// in act
enemys e = getOneIntersectingObject(enemys.class);
if (e != null && !sus.contains(e)) sus.add(e);
Aaron-aid Aaron-aid

2021/11/10

#
i get the error that actor cant be converted to enemys :c
danpost danpost

2021/11/10

#
Aaron-aid wrote...
i get the error that actor cant be converted to enemys :c
Okay, use:
enemys e = (enemys) getOneIntersectingObject(enemys.class);
You need to login to post a reply.