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

2017/12/12

get a variable from object at offset from another actor

Arkin Arkin

2017/12/12

#
hello! So what i have is a boolean in a class (cell.class) and i want to test it from another actor by another actor
boolean test = getWorld().getObjects(getWorld().getObjects(getWorld().getObjects(Jambe_p2_1.class).get(0).getOneObjectAtOffset(0,0,cell.class).getClass()).get(0).isGround1);
i execute this code from my Body class but the error said is "cannot find symbol - variable isGround1". my boolean isGround1 is a public boolean. do you understand what i want?
danpost danpost

2017/12/12

#
The line is just wrong in so many ways -- a casting issue, an access issue and a parameter type conversion issue. CASTING: 'isGround1' is in the 'cell' class; but, 'getObjects(...).get(0)' returns an Object type object. To cast as 'cell' type object, something like this is neededd: '(cell)getObjects(...).get(0)' ACCESS: 'getOneObjectAtOffset' is a protected method; it cannot be called on any object from a different class than from which that object belongs. TYPES: the ')' at the end of the line closes the parameter list of the first 'getObjects' call; the parameter required is a Class object or' null' -- not a boolean. Obviously, you are trying to determine if some 'cell' object is considered to be a ground object. However, without more info, it cannot be seen how a Jambe_p2_1 object relates to a Body object. Also, the cell size of your world might be something that you should share.
Arkin Arkin

2017/12/12

#
I can't give you my Body class because there's more than 500 lines but here is my cell.class:
import greenfoot.*; 
import java.util.List;
import java.util.ArrayList;

public class cell extends Actor{
    public void addedToWorld(World world) {
        int width = 20;
        int height = 20;
        setImage( new GreenfootImage( width, height ));
        getImage().setColor(Color.WHITE);
        getImage().fillRect( 0, 0, width, height);
    }
    int addY;
    int y1;
    int addX;
    int x1;
    
    int SpawnX;
    public boolean isGround1;

    public cell(int Y, int X, int Random, boolean isGround){
        addY = Y;
        y1 = Math.abs(Y);
        addX = X;
        x1 = X;
        SpawnX = Random;
        if(isGround == true){
            isGround1 = true;
        }else{
            isGround1 = false;
        }
    }
    public void act(){
        if(y1 != 0){
            test1();
        }
        if(y1 == 0){
            test2();
        }
    }
    public void test1(){
        if(y1 != 1){
            y1 = 1;
            getWorld().addObject(new cell(addY-(Math.abs(addY)/addY),addX,SpawnX,false), getX(), (getY()+((Math.abs(addY)/addY)*20)));
        }
        if(Math.abs(addY) == SpawnX && x1 != 0){
            x1 = 0;
            getWorld().addObject(new cell(0,addX-(int)(addX/Math.abs(addX)),SpawnX,true), getX()+(int)((addX/Math.abs(addX))*20), getY());
        }
    }
    public void test2(){
        if(x1 != 0){
            x1 = 0;
            getWorld().addObject(new cell(0,addX-(int)(addX/Math.abs(addX)),SpawnX,true), getX()+(int)((addX/Math.abs(addX))*20), getY());
        }
    }
    public int RandomHighX(){
        return Greenfoot.getRandomNumber(3)+2;
    }
}
what should I do?
danpost danpost

2017/12/12

#
The 'cell' class given does not give me any more info than what was already known (except that the cell size of the world is 1 and your cell objects are 20x20).
Arkin Arkin

2017/12/15

#
I found how to do this!
List<cell> cellUP1 = getWorld().getObjects(Jambe_p2_1.class).get(0).getObjectsInRange((int)(getWorld().getObjects(Jambe_p2_1.class).get(0).getImage().getHeight()*0.4), cell.class);
boolean testFOR1 = cellUP1.get(0).isGround1;
You need to login to post a reply.