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

2019/9/30

How to achieve Line Of Sight

1
2
SickTrickz832 SickTrickz832

2019/10/5

#
ImageScrollWorld
import greenfoot.*;

public class ImageScrollWorld extends World
{
    public static final int WIDE = 800; // World Width (view)
    public static final int HIGH = 800; // World Height (view)
    
    Scroller scroller; // the object that performs the scrolling
    Actor scrollActor; // an actor to stay in view
    
    public ImageScrollWorld()
    {    
        super (WIDE, HIGH, 1, false); // creates an unbound world
        GreenfootImage bg = new GreenfootImage("bg.jpg"); // creates the image
        int bgWide = bg.getWidth();
        int bgHigh = bg.getHeight();
        scroller = new Scroller (this, bg, bgWide, bgWide); // creates scroller object
        scrollActor = new Player(); // creates the actor
        addObject (scrollActor, bgWide / 2, bgHigh / 2); // add actor to world (wherever)
        scroll(); // sets initial background image and puts main actor in view if needed
    }
    
    public void act()
    {
        if (scrollActor != null)
        {
            scroll();
        }
    }
    
    // attempts scrolling when actor is not in center of visible world
    private void scroll()
    {
        // determine scrolling offsets and scroll
        int dsX = scrollActor.getX() - WIDE / 2; // horizontal offset from center
        int dsY = scrollActor.getY() - HIGH / 2; // vertical offset from center
        scroller.scroll (dsX, dsY);
    }
}
Scroller Class
import greenfoot.*;

public class Scroller
{
    private World world; // view window world
    private GreenfootImage scrollImage; // scrolling image
    private boolean limited; // flag to indicate whether scrolling is limited or not
    private int scrolledX, scrolledY; // current scrolled distances
    private int wide, high; // if limited, dimensions of scrolling area else of image to wrap
   
    /**
     * This constructor is for an unlimited scrolling world;
     * If 'image' is null, the background will not change; else the given image is wrapped
     * 
     * @param viewWorld the world that scrolling will be performed on
     * @param image the background image that will be tiled, if needed, and wrap with scrolling
     */
    public Scroller(World viewWorld, GreenfootImage image)
    {
        world = viewWorld;
        scrollImage = image;
        if (image != null)
        {
            wide = image.getWidth();
            high = image.getHeight();
        }
        scroll(0, 0); // sets initial background image
    }
   
    /**
     * This constructor is for a limited scrolling world;
     * If 'image' is smaller than the given total scrolling area, it will be tiled
     * If 'image' is null, the background will not change
     * 
     * @param viewWorld the world that scrolling will be performed on
     * @param image the background image that will be tiled, if needed, to fill the scrolling area
     * @param wide the width of the visible area encompassed through scrolling;
     * the given value must be at least equal to the width of 'viewWorld' and
     * is given in world cells (not in pixels)
     * @param high the height of the visible area encompassed through scrolling;
     * the given value must be at least equal to the height of 'viewWorld' and
     * is given in world cells (not in pixels)
     */
    public Scroller(World viewWorld, GreenfootImage image, int wide, int high)
    {
        this.wide = wide;
        this.high = high;
        limited = true;
        world = viewWorld;
        if (image != null)
        {
            // create an image as large as scrolling area; tiled, if needeed
            scrollImage = new GreenfootImage(wide*world.getCellSize(), high*world.getCellSize());
            for (int x=0; x<wide*world.getCellSize(); x+= image.getWidth())
                for (int y=0; y<high*world.getCellSize(); y+=image.getHeight())
                    scrollImage.drawImage(image, x, y);
            // set initial background image
            scroll(0, 0);
        }
    }
   
    /**
     * performs scrolling on 'world' by the given distances along the horizontal and vertical;
     * if 'limited' is false, requested distances are actual scrolling distances;
     * if 'limited' is true, the distances may be adjusted due to the limits of scrolling
     *
     * @param dsx the requested distance to shift everything horizontally
     * @param dsy the requested distance to shift everything vertically
     */
    public void scroll(int dsx, int dsy)
    {
        // adjust scroll amounts and scroll background image
        if (limited)
        {
            // calculate limits of scrolling
            int maxX = wide-world.getWidth();
            int maxY = high-world.getHeight();
            // apply limits to distances to scroll
            if (scrolledX+dsx < 0) dsx = -scrolledX;
            if (scrolledX+dsx >= maxX) dsx = maxX-scrolledX;
            if (scrolledY+dsy < 0) dsy = -scrolledY;
            if (scrolledY+dsy >= maxY) dsy = maxY-scrolledY;
            // update scroll positions
            scrolledX += dsx;
            scrolledY += dsy;
            // scroll background image
            if (scrollImage != null)
            {
                world.getBackground().drawImage
                (   
                    scrollImage,
                    -scrolledX*world.getCellSize(),
                    -scrolledY*world.getCellSize()
                );
            }
        }
        else // unlimited image wrapping
        {
            // update scroll positions
            scrolledX += dsx;
            scrolledY += dsy;
            // scroll background image
            if (scrollImage != null)
            {
                // create working variables of scroll positions
                int imageX = scrolledX*world.getCellSize();
                int imageY = scrolledY*world.getCellSize();
                // get near-zero starting positions for drawing 'scrollImage'
                imageX = imageX%wide;
                imageY = imageY%high;
                // adjust negative values as needed
                if (imageX < 0) imageX += wide;
                if (imageY < 0) imageY += high;
                // create image of appropriate size and tile fill 'scrollImage' onto it
                GreenfootImage hold = new GreenfootImage(scrollImage);
                hold.drawImage(scrollImage, -imageX, -imageY);
                if (imageX > 0) hold.drawImage(scrollImage, wide-imageX, -imageY);
                if (imageY > 0) hold.drawImage(scrollImage, -imageX, high-imageY);
                if (imageX > 0 && imageY > 0)
                    hold.drawImage(scrollImage, wide-imageX, high-imageY);
                // set image to background of 'world'
                world.setBackground(hold);
            }
        }
        // adjust position of all actors (that can move with 'setLocation')
        for (Object obj : world.getObjects(null))
        {
            Actor actor = (Actor) obj;
            actor.setLocation(actor.getX()-dsx, actor.getY()-dsy);
        }
    }
   
    /**
     * getter method for the current total scrolled distance horizontally
     *
     * @return the current total offset of horizontal scrolling
     */
    public int getScrolledX()
    {
        return scrolledX;
    }
   
    /**
     * getter method for the current total scrolled distance vertically
     *
     * @return the current total offset of vertical scrolling
     */
    public int getScrolledY()
    {
        return scrolledY;
    }
}
danpost danpost

2019/10/5

#
Is there a reason you use the width of the background image for both scrolling dimensions on line 17 in ImageScrollWorld?
SickTrickz832 SickTrickz832

2019/10/5

#
danpost wrote...
Is there a reason you use the width of the background image for both scrolling dimensions on line 17 in ImageScrollWorld?
OH I see what ive done. I accidentally used both the width when scrolling instead of width and height
SickTrickz832 SickTrickz832

2019/10/5

#
danpost wrote...
Is there a reason you use the width of the background image for both scrolling dimensions on line 17 in ImageScrollWorld?
Changed it but it didnt change anything in the scenario
import greenfoot.*;

public class ImageScrollWorld extends World
{
    public static final int WIDE = 800; // World Width (view)
    public static final int HIGH = 800; // World Height (view)
    
    Scroller scroller; // the object that performs the scrolling
    Actor scrollActor; // an actor to stay in view
    
    public ImageScrollWorld()
    {    
        super (WIDE, HIGH, 1, false); // creates an unbound world
        GreenfootImage bg = new GreenfootImage("bg.jpg"); // creates the image
        int bgWide = bg.getWidth();
        int bgHigh = bg.getHeight();
        scroller = new Scroller (this, bg, bgWide, bgHigh); // creates scroller object
        scrollActor = new Player(); // creates the actor
        addObject (scrollActor, bgWide / 2, bgHigh / 2); // add actor to world (wherever)
        scroll(); // sets initial background image and puts main actor in view if needed
    }
    
    public void act()
    {
        if (scrollActor != null)
        {
            scroll();
        }
    }
    
    // attempts scrolling when actor is not in center of visible world
    private void scroll()
    {
        // determine scrolling offsets and scroll
        int dsX = scrollActor.getX() - WIDE / 2; // horizontal offset from center
        int dsY = scrollActor.getY() - HIGH / 2; // vertical offset from center
        scroller.scroll (dsX, dsY);
    }
}
danpost danpost

2019/10/5

#
SickTrickz832 wrote...
Nope. I just wrote it out how it was on the tutorial
That is not what the tutorial shows. What are the width and height of your scrolling image (bg)?
SickTrickz832 SickTrickz832

2019/10/5

#
danpost wrote...
SickTrickz832 wrote...
Nope. I just wrote it out how it was on the tutorial
That is not what the tutorial shows. What is are the width and height of your scrolling image (bg)?
The actual width and height of the JPEG image is 64 x 64. Should I scale it up abit then? Because for testing it I just used a default one from backgrounds
danpost danpost

2019/10/5

#
SickTrickz832 wrote...
Changed it but it didnt change anything in the scenario
I actually did not expect it to.
SickTrickz832 SickTrickz832

2019/10/5

#
danpost wrote...
SickTrickz832 wrote...
Changed it but it didnt change anything in the scenario
I actually did not expect it to.
Ok, I changed the background image size to 800 x 800 and changed the world size to 800 x 800. The player now 'spawns' in the center of the world. However it doesnt stick to the center and the world doesnt scroll, the player just walks off screen
danpost danpost

2019/10/5

#
SickTrickz832 wrote...
Ok, I changed the background image size to 800 x 800 and changed the world size to 800 x 800. The player now 'spawns' in the center of the world. However it doesnt stick to the center and the world doesnt scroll, the player just walks off screen
No -- do not do that. Just replace lines 15 and 16 with:
int bgWide = 1200;
int bgHigh = 800;
SickTrickz832 SickTrickz832

2019/10/5

#
danpost wrote...
SickTrickz832 wrote...
Ok, I changed the background image size to 800 x 800 and changed the world size to 800 x 800. The player now 'spawns' in the center of the world. However it doesnt stick to the center and the world doesnt scroll, the player just walks off screen
No -- do not do that. Just replace lines 15 and 16 with:
int bgWide = 1200;
int bgHigh = 800;
Gave it a go, works perfectly. Only think i gotta do now is make it so the actor doesn’t walk offscreen, and all other actors, like obstacles and pickups, move as well. Thanks for your help, i’ll reply to this thread if i have any other problems.
You need to login to post a reply.
1
2