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

2019/3/30

Problems with LineOfSight Method

LobsterL LobsterL

2019/3/30

#
I have followed the Jim Stewart video on forming a lineof sight between object however the line that i have drawn is not in the world.
Actor Army1;
    Actor Army2;
    int army1X;
    int army1Y;
    int army2X;
    int army2Y;
    GreenfootImage myImage;
    public LineOfSight (Actor a1, Actor a2)
    {
        Army1 = a1;
        Army2 = a2;
        setImage(new GreenfootImage(1,1));
    }
    /**
     * Act - do whatever the LineOfSight wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Army1.getWorld()!=null && Army2.getWorld()!=null)
        {
            army1X = Army1.getX();
            army1Y = Army1.getY();
            army2X = Army2.getX();
            army2Y = Army2.getY();
            int myX = (army2X + army1X)/2; 
            int myY = (army2Y + army1Y)/2;
            setLocation(myX, myY);
            turnTowards(army1X, army1Y);
            int d =(int)Math.sqrt(Math.pow((army2X-army1X),2)+Math.pow((army2Y-army1Y),2));
            setImage(new GreenfootImage(d,1));
            myImage = getImage();
            //getWorld().getBackground().fillRect(army1X, army1Y, army2X, army2Y);
            //getWorld().getBackground().drawRect(army1X,army1Y,army2X,army2Y);
            myImage.setColor(Color.RED);
            myImage.drawLine(1,1,d,1);
            //getWorld().getBackground().setColor(Color.BLUE);
            //getWorld().getBackground().drawLine(0,0,d,0);
        }
    }    
    
    
}
i have added the LineOfSight object into the world aswell
nccb nccb

2019/3/30

#
Coordinates begin at zero in images, so at first glance, my suggestion is that line 36:
myImage.drawLine(1,1,d,1);
should become this to keep the line in the image bounds:
myImage.drawLine(0,0,d,0);
Similar to how it is in the commented-out code. Note that since this image already has exactly the right dimensions for your line, you could instead just use the much simpler:
myImage.fill();
LobsterL LobsterL

2019/3/31

#
I have changed that over and it still seems to not want to work. I'm sorta lost right now.
danpost danpost

2019/3/31

#
LobsterL wrote...
I have changed that over and it still seems to not want to work. I'm sorta lost right now.
Please provide your World subclass codes for review -- or, whatever class you add the LineOfSight object into the world in.
LobsterL LobsterL

2019/3/31

#
here it is chief
public BattleGround()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1200, 900, 1);
        Army1 army1 = new Army1();
        Army2 army2 = new Army2();
        LineOfSight los = new LineOfSight(army2, army1);
        addObject(new Barriers(), 400, 0);
        addObject(new Barriers(), 400, 10);
        addObject(new Barriers(), 400, 20);
        addObject(new Barriers(), 400, 30);
        addObject(new Barriers(), 400, 40);
        addObject(new Barriers(), 400, 50);
        addObject(new Barriers(), 400, 60);
        addObject(new Barriers(), 400, 70);
        addObject(new Barriers(), 400, 80);
        addObject(new Barriers(), 400, 90);
        addObject(new Barriers(), 400, 100);
        addObject(new Barriers(), 400, 110);
        addObject(new Barriers(), 400, 120);
        addObject(new Barriers(), 400, 130);
        addObject(new Barriers(), 400, 140);
        addObject(new Barriers(), 400, 150);
        addObject(new Barriers(), 400, 160);
        addObject(new Barriers(), 400, 170);
        addObject(new Barriers(), 400, 180);
        addObject(new Barriers(), 400, 190);
        addObject(new Barriers(), 400, 200);
        addObject(new Barriers(), 400, 210);
        addObject(new Barriers(), 400, 220);
        addObject(new Barriers(), 400, 230);
        addObject(new Barriers(), 400, 240);
        addObject(new Barriers(), 400, 250);
        addObject(new Barriers(), 400, 260);
        addObject(new Barriers(), 400, 270);
        addObject(new Barriers(), 400, 280);
        addObject(new Barriers(), 400, 290);
        addObject(new Barriers(), 400, 300);
        addObject(new Barriers(), 400, 310);
        addObject(new Barriers(), 400, 320);
        addObject(new Barriers(), 400, 330);
        addObject(new Barriers(), 400, 340);
        addObject(new Barriers(), 400, 350);
        addObject(new Barriers(), 400, 360);
        addObject(new Barriers(), 410, 360);
        addObject(new Barriers(), 420, 360);
        addObject(new Barriers(), 430, 360);
        addObject(new Barriers(), 440, 360);
        addObject(new Barriers(), 450, 360);
        addObject(new Barriers(), 460, 360);
        addObject(new Barriers(), 470, 360);
        addObject(new Barriers(), 480, 360);
        addObject(new Barriers(), 490, 360);
        addObject(new Barriers(), 500, 360);
        addObject(los, 1, 1);
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Army2 army2 = new Army2();
        addObject(army2,959,274);
        Army1 army1 = new Army1();
        addObject(army1,127,746);
    }
}

LobsterL LobsterL

2019/3/31

#
i have also tried addObject(los, 0, 0); and that didn't work either
Super_Hippo Super_Hippo

2019/3/31

#
You create two objects of Army1 and Army2. Each. And you pass the ones to the LineOfSight object which aren't added to the world. Remove the prepare method and add army1 and army2 to the world that you create in line 5 and 6.
You need to login to post a reply.