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

2017/7/21

Line Of Sight

1
2
LobsterL LobsterL

2017/7/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class LineOfSight here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LineOfSight extends Actor
{
    Actor Warrior1;
    Actor Soceror;
    int Warrior1X;
    int Warrior1Y;
    int SocerorX;
    int SocerorY;
    GreenfootImage myImage;
    /**
     * Act - do whatever the LineOfSight wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public LineOfSignt (Actor W, Actor S)
    {
        Warrior1 = W;
        Soceror = S;
        setImage(new GreenfootImage(1,1));
    }
    public void act() 
    {
        if(Warrior1.getWorld()!=null && Soceror.getWorld()!=null)
        {
            Warrior1X = Warrior1.getX();
            Warrior1Y = Warrior1.getY();
            SocerorX = Soceror.getX();
            SocerorY = Soceror.getY();
            int myX = (SocerorX + Warrior1X)/2;
            int myY = (SocerorY + Warrior1Y)/2;
            setLocation(myX, myY);
            turnTowards(Warrior1X, Warrior1Y);
            int d = (int)Math.sqrt(Math.pow((SocerorX-Warrior1X),2)+Math.pow((SocerorY-Warrior1Y),2));
            setImage(new GreenfootImage(d,1));
            myImage = getImage();
            myImage.setColor(Color.BLACK);
            myImage.drawLine(0,0,d,0);
        }
    }    
    public boolean clearLineOfSight()
    {
        return(getOneIntersectingObject(Fence.class)==null);
    }
}
I'm getting an invalid declaration method were on Jim Stewart's demo it is fine
danpost danpost

2017/7/21

#
Make sure LineOfSight is spelled correctly on line 22.
LobsterL LobsterL

2017/7/21

#
i have fixed it but am getting error
        if(los!=null)
        {
            if(los.clearLineOfSight())
            {
                if(Warrior1OnScreen != null && Warrior1OnScreen.getWorld()!=null)
                {
                    turnTowards(Warrior1OnScreen.getX(), Warrior1OnScreen.getY());
                    setRotation(90*Math.round(getRotation()/90));
                }
            }
        }
with los and warrior1OnScreen and in the world class
    LineOfSight los = new LineOfSight(Soceror, Warrior1);
withe the Warrior1 and Soceror
danpost danpost

2017/7/21

#
Please show the entire Socerer class code.
LobsterL LobsterL

2017/7/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soceror here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Soceror extends Characters
{
    
    /**
     * Act - do whatever the Soceror wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        turnTowards(Warrior1.Warrior1X, Warrior1.Warrior1Y);
        move(2);
        if (isTouching(Warrior1.class))
        {
            removeTouching(Warrior1.class);
        }
        if (isTouching(FenceUp.class))
        {
            move(-2);
        }
        if (isTouching(Fence.class))
        {
            move(-2);
        }
        if(los!=null)
        {
            if(los.clearLineOfSight())
            {
                if(Warrior1OnScreen != null && Warrior1OnScreen.getWorld()!=null)
                {
                    turnTowards(Warrior1OnScreen.getX(), Warrior1OnScreen.getY());
                    setRotation(90*Math.round(getRotation()/90));
                }
            }
        }
    }  
     public void chaseWarrior()
    {
        turnTowards(Warrior1.Warrior1X, Warrior1.Warrior1Y);
        move(2);
        
    }
    public Soceror()
    {
        GreenfootImage myImage = getImage();
        myImage.scale(50,50);
    }
    
}

danpost danpost

2017/7/21

#
Those variables are not declared and assigned values anywhere within the class. Is this done in the Characters class?
danpost danpost

2017/7/21

#
BTW, Jim Stewart has a downloadable scenario on this site that utilizes his LineOfSight class. Maybe you can take a look at it to see exactly what was done and what you might be missing. It is located here.
LobsterL LobsterL

2017/7/25

#
i have taking a look at the code and everything has no error but doesn't seem to be working
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soceror here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Soceror extends Characters
{
    LineOfSight los;
    Warrior1 Warrior1OnScreen;
    int Warrior1X;
    int Warrior1Y;
    public void addedToWorld(World myWorld)
    {
        if(myWorld instanceof MyWorld)
        {
            MyWorld myworld = (MyWorld)myWorld;
            los = myworld.getLOS();
            Warrior1OnScreen = myworld.getWarrior1();
        }
    }
    /**
     * Act - do whatever the Soceror wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Warrior1X = getX();
        Warrior1Y = getY();
        if (isTouching(Fence.class) && isTouching(FenceUp.class))
        {
            setRotation(180);
            move(5);
        }
        if(isTouching(Fence.class) 
        || isTouching(FenceUp.class))
        {
          setRotation(90);
          move(5);
        }
        if (isTouching(Warrior1.class))
        {
            removeTouching(Warrior1.class);
        }
        Actor warrior1ImTouching = getOneIntersectingObject(Warrior1.class);
        if(los!=null)
        {
            if(los.clearLineOfSight() && los.clearLineOfSight2())
            {
                if(Warrior1OnScreen != null && Warrior1OnScreen.getWorld()!=null)
                {
                    move(5);
                    turnTowards(Warrior1OnScreen.Warrior1X, Warrior1OnScreen.Warrior1Y);
                    setRotation(90*Math.round(getRotation()/90));
                }
            }
        }
    } 
    
    public void fire()
    {
        Warrior1X = getX();
        Warrior1Y = getY();
        SocerorShot S = new SocerorShot();
        getWorld().addObject(S, getX(), getY());
    }
    
    public Soceror()
    {
        GreenfootImage myImage = getImage();
        myImage.scale(50,50);
    }
    

}
LobsterL LobsterL

2017/7/25

#
the void fire it to fire a shot when the warrior is in the soceror's (i know i spelt it wrong) lineofsight and with a delay time of at least 2 seconds
danpost danpost

2017/7/25

#
I do not see any timer or code to determine when to fire a shot or any place where the 'fire' method is called from at all.. Where is it?
LobsterL LobsterL

2017/7/26

#
the fire method happens in between line 51 and 52 in the clear line of sight method and it does work but the enemy isn't moving towards the character and i can't figure out why?
Super_Hippo Super_Hippo

2017/7/26

#
LobsterL wrote...
the fire method happens in between line 51 and 52 in the clear line of sight method and it does work
In your first post, the clearLineOfSight method was just one line and has nothing to do with the fire method. There is no line between 51 and 52 and I don't see from where it is called either.
but the enemy isn't moving towards the character and i can't figure out why?
Maybe you have to turn and then move (swapping 54 and 55).
LobsterL LobsterL

2017/7/26

#
i have changed everything and the enemy still isn't moving
LobsterL LobsterL

2017/7/26

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Soceror here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Soceror extends Characters
{
    LineOfSight los;
    Warrior1 Warrior1OnScreen;
    int Warrior1X;
    int Warrior1Y;
    // public void addedToWorld(World myWorld)
    // {
        // if(myWorld instanceof MyWorld)
        // {
            // MyWorld myworld = (MyWorld)myWorld;
            // los = myworld.getLOS();
            // Warrior1OnScreen = myworld.getWarrior1();
        // }
    // }
    /**
     * Act - do whatever the Soceror wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Warrior1X = getX();
        Warrior1Y = getY();
        if (isTouching(Fence.class) && isTouching(FenceUp.class))
        {
            setRotation(180);
            move(5);
        }
        if(isTouching(Fence.class) 
        || isTouching(FenceUp.class))
        {
          setRotation(90);
          move(5);
        }
        if (isTouching(Warrior1.class))
        {
            removeTouching(Warrior1.class);
        }
        if(los!=null)
        {
            if(los.clearLineOfSight() && los.clearLineOfSight2())
            {
                move(5);
                if(Warrior1OnScreen != null && Warrior1OnScreen.getWorld()!=null)
                {
                    turnTowards(Warrior1OnScreen.getX(), Warrior1OnScreen.getY());
                    move(5);
                    setRotation(90*Math.round(getRotation()/90));
                }
            }
        }
    } 
    
    public void fire()
    {
        Warrior1X = getX();
        Warrior1Y = getY();
        SocerorShot S = new SocerorShot();
        getWorld().addObject(S, getX(), getY());
    }
    
    public Soceror()
    {
        GreenfootImage myImage = getImage();
        myImage.scale(50,50);
    }
    

}
LobsterL LobsterL

2017/7/26

#
the move in line 51 doesnt work either
There are more replies on the next page.
1
2