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

2016/12/23

how to get changeX and changeY

1
2
brothermic brothermic

2016/12/29

#
i tried opera chrome firefox... nothing
danpost danpost

2016/12/29

#
brothermic wrote...
i tried opera chrome firefox... nothing
Try referring to the discussion it has a link to in the description.
brothermic brothermic

2016/12/30

#
hi dan :) making progress figuring out the code from 'scroller'. However i have to fill in the world the dsx and dsy. what value's do i need to fill in here? where do they need to come from?
private void scroll()
    {
        // determine amount to scroll (dsx, dsy)
        scroller.scroll(dsx, dsy); // have scroller scroll world
    }
danpost danpost

2016/12/30

#
brothermic wrote...
i have to fill in the world the dsx and dsy. what value's do i need to fill in here? where do they need to come from? < Code Omitted >
They are basically the 'dx' and 'dy' in the 'scroll' method I gave previously.
brothermic brothermic

2016/12/30

#
oh ofcourse yeah. i tought some code from the scroller replaced it. anyway when i fill in the dx and dy my screen finially seems to move! however when i am riding to the left and top everythiong works just fine and on 150 pixels from the adge it starts moveing. however when i ride to the right and bottom my tank dissapears and acting strange ones i come in 150 pixels range..? i'll post the code from world here.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private Tank theTank;
    private static final Map map = new Map();
    private static final GreenfootImage mapImg = map.getImage();;
    private static final int MAP_IMG_WIDTH = mapImg.getWidth();
    private static final int MAP_IMG_HEIGHT = mapImg.getHeight();
    private static final Wall wallTemplate = new Wall();
    private static final GreenfootImage myWallImage = wallTemplate.getImage();
    private static final int WALL_HEIGHT = myWallImage.getHeight();
    private static final int WALL_WIDTH = myWallImage.getWidth();
    private static final int MAPWIDTH =  MAP_IMG_WIDTH * WALL_WIDTH;
    private static final int MAPHEIGHT = MAP_IMG_HEIGHT * WALL_HEIGHT;
    //private List<Wall> theWalls;
    //private int leftBound;
    //private int rightBound;
    //private int bottomBound;
    //private int topBound;
    private Scroller scroller;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(850, 850, 1,false);
        scroller = new Scroller(this, new GreenfootImage("sand.png"));
        theTank = new Tank(); 
        addObject(theTank,400, 400);
        //theWalls = new ArrayList<Wall>();
        //leftBound = 0;
        // rightBound = getWidth();
        //bottomBound = MAPHEIGHT;
        //topBound = MAPHEIGHT - getHeight();
        makeMap();

    }

    public void act()
    {
        scroll();
    }

    public void makeMap()
    {
        for(int y = 0;y < MAP_IMG_HEIGHT; y++){
            for(int x = 0;x < MAP_IMG_WIDTH; x++){
                int colorRGB = mapImg.getColorAt(x,y).getRGB();
                if (colorRGB == Color.BLACK.getRGB()){
                    int mapX= x * WALL_WIDTH + WALL_WIDTH/2;
                    int mapY= y * WALL_HEIGHT + WALL_HEIGHT/2;
                    addObject(new Wall(),mapX,mapY);
                }
            }
        }
    }

    private void scroll()
    {
        int dx = 0;
        int dy = 0;
        if (theTank.getX() < 150){
            dx = theTank.getX()-150;
        }
        if (theTank.getX() > getWidth()-150) {
            dx = theTank.getX()-getWidth()-150;
        }
        if (theTank.getY() < 150){
            dy = theTank.getY()-150;
        }
        if (theTank.getY() > getHeight()-150){
            dy = theTank.getY()-getHeight()-150;
        }
        if (dx == 0 && dy == 0){ 
            return;
        }
        scroller.scroll(dx, dy);
    }

    public Tank getTheTank()
    {
        return theTank;
    }
}
but it hink it must be something in the scrollerclass:
import greenfoot.*;
/**
 * CLASS: Scroller (extends Object)
 * AUTHOR: danpost (greenfoot.org username)
 * DATE: November 11, 2016
 * MODIFIED: December 22, 2016 (fixed 'scroll' method for limited no-image scrolling)
 * 
 * DESCRIPTION:  This is a support class for a scrolling world.  It contains two constructors;
 * one for unlimited scrolling and one for limited scrolling.  Both constructors have an 'image'
 * parameter.  Because image manipulation can hog up CPU time, it is important to remember that
 * it is better not to have a scrolling background image (having an Actor for the background is
 * probably worse than having the background scroll).  For unlimited scrolling using a background
 * image, the smaller that background image to be tiled, the better.  Making the viewport (the
 * size of the world that is visible) smaller can help in CPU expense, also.  Scrolling worlds
 * should be unbounded, allowing actors to move beyond the visible area.  Ensuring that actors
 * are removed from the world if no longer needed when out of view will help to prevent lag,
 * as well.  
 * 
 * It is the responsibility of the World object that creates a Scroller object to determine when
 * to scroll and by how much.
 */
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 image
            if (scrollImage != null)
            {
                world.getBackground().drawImage
                (   
                    scrollImage,
                    -scrolledX*world.getCellSize(),
                    -scrolledY*world.getCellSize()
                );
            }
        }
        else if (scrollImage != null) // unlimited image wrapping
        {
            // update scroll positions
            scrolledX += dsx;
            scrolledY += dsy;
            // create working variables of scroll positions
            int imageX = scrolledX*world.getCellSize();
            int imageY = scrolledY*world.getCellSize();
            // find a similar positive value for scroll positions
            while (imageX < 0) imageX += 100*wide;
            while (imageY < 0) imageY += 100*high;
            // get new starting positions for drawing 'scrollImage'
            imageX = imageX%wide;
            imageY = 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;
    }
}
i am using the unlimited scrolling constructot because it doesnt really matter cause i have walls they cant pass. its like when i am scrolling to the right or bottom that the backgroundimage gets on top of the tankimage and meaby thats why i dont see my tanks anymore suddenly? but that doesnt explain why when i scroll the first time right or bottom that my screen goes a little bit back to the opposite side before scrolling?
brothermic brothermic

2016/12/30

#
ok so i changed dx = theTank.getX()-getWidth()-150; to dx = theTank.getX()-(getWidth()-150); the same with line getheight and problem fixed. Thnaks for your help dan and have a happy newyear!!
danpost danpost

2016/12/30

#
brothermic wrote...
ok so i changed dx = theTank.getX()-getWidth()-150; to dx = theTank.getX()-(getWidth()-150); the same with line getheight and problem fixed.
Yeah -- I just figured that out myself. Was about to post and saw yours was posted 5 minutes ago.
have a happy newyear!!
Likwise.
You need to login to post a reply.
1
2