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

2022/11/28

How Do I make Camera movement only go Horizontal and vertical at a certain height?

1
2
3
Fire Fire

2022/11/28

#
This is the code Not Sure what to change
public class ExampleWorld extends ScrollingWorld
{
    public ExampleWorld() {
        super(600, 500, 1);//creates an infinite scrolling world with a screen size of 600 x 400;
        //if you want to limitate the scrolling world you have to use this constructor:
        //super(600, 400, 1, scrollingWidth, scrollingHeight);
        setScrollingBackground(new GreenfootImage("Unbenanntes_Projekt.PNG"));
        createExampleWorld();//this method just adds some objects to the world.
    }

    /**
     * Creates an example world where the ScrollingActor can move.
     */
    public void createExampleWorld() {
        for (int x = getWidth() * -3; x < getWidth() * 3; x += getWidth()/2) {
            for (int y = getHeight() * -3; y < getHeight() * 3; y += getHeight()/2) {
                if (x != getWidth()/2 || y != getHeight()/2) {
                    addObject(new Alien_Basic(), x, y);
                }
            }
        }
        for (int x = getWidth() * -3 + getWidth()/2; x < getWidth() * 3; x += getWidth()) {
            for (int y = getHeight() * -3 + getHeight()/4; y < getHeight() * 3; y += getHeight()) {
                addObject(new Alien_Basic(), x, y);
            }
        }
        addObject(new FBI_Agent(), getWidth()/2, getHeight()/2);
    }

}
public class ScrollingWorld extends World
{

    //A maximum size of the Scrolling World. If the value for width or height is 0 the world is infinite in this direction.
    //The variables are final so they have to be set before compiling the game and can't be set while executing the game.
    public static int WORLD_WIDTH;
    public static int WORLD_HEIGHT;

    protected int totalXMovement = 0;
    protected int totalYMovement = 0;

    //This image is used as the background image of the scrolling world.
    //If you want to use another image just chang the path.
    protected GreenfootImage textur;

    public ScrollingWorld(int screenWidth, int screenHeight) {
        super(screenWidth, screenHeight, 1, false);
        WORLD_WIDTH = 3000;
        WORLD_HEIGHT = 800;
    }

    public ScrollingWorld(int screenWidth, int screenHeight, int cellSize) {
        super(screenWidth, screenHeight, cellSize, false);
        WORLD_WIDTH = 3000;
        WORLD_HEIGHT = 800;
    }

    public ScrollingWorld(int screenWidth, int screenHeight, int scrollingWidth, int scrollingHeight) {
        super(screenWidth, screenHeight, 1, false);
        WORLD_WIDTH = scrollingWidth;
        WORLD_HEIGHT = scrollingHeight;
    }

    public ScrollingWorld(int screenWidth, int screenHeight, int cellSize, int scrollingWidth, int scrollingHeight) {
        super(screenWidth, screenHeight, cellSize, false);
        WORLD_WIDTH = scrollingWidth;
        WORLD_HEIGHT = scrollingHeight;
    }

    /**
     * Reset the position of the ScrollingActor and set the position of all other objects in the world.
     */
    public final void resetPlayersPosition(ScrollingActor scrollingActor) {
        int xMovement = (int) ((double) getWidth()/2 - scrollingActor.getExactX());
        int yMovement = (int) ((double) getHeight()/2 - scrollingActor.getExactY());
        totalXMovement += xMovement;
        totalYMovement += yMovement;
        List<Actor> actors = getObjects(Actor.class);
        for (Actor actor : actors) {
            if (actor instanceof ScrollingActor) {
                ((ScrollingActor) actor).setLocation(actor.getX() + xMovement, actor.getY() + yMovement, false);
            }
            else if (actor instanceof Menu || actor instanceof FixedObject) {
                ;
            }
            else {
                actor.setLocation(actor.getX() + xMovement, actor.getY() + yMovement);
            }
        }
        createTextur();
    }

    /**
     * Creates a moving textur on the background image of the world.
     */
    protected final void createTextur() {
        int x;
        int y;
        if (totalXMovement > 0) {
            for (x = totalXMovement; x > 0; x -= textur.getWidth()) {
                ;
            }
        }
        else {
            for (x = totalXMovement; x < 0; x += textur.getWidth()) {
                ;
            }
            x -= textur.getWidth();
        }
        if (totalYMovement > 0) {
            for (y = totalYMovement; y > 0; y -= textur.getHeight()) {
                ;
            }
        }
        else {
            for (y = totalYMovement; y < 0; y += textur.getHeight()) {
                ;
            }
            y -= textur.getHeight();
        }
        getBackground().clear();
        for (int i = x; i < getWidth(); i += textur.getWidth()) {
            for (int j = y; j < getHeight(); j += textur.getHeight()) {
                getBackground().drawImage(textur, i, j);
            }
        }
    }

    /**
     * Remove all objects that currently are in the world.
     */
    public void removeAllObjects() {
        removeObjects(getObjects(null));
    }

    /**
     * Remove all objects that currently are in the world with the exception of the actor given as parameter.
     * 
     * @param actor
     *      The only object that will not be removed from the world.
     */
    public void removeAllObjectsBut(Actor actor) {
        List<Actor> allActors = getObjects(null);
        for (Actor a : allActors) {
            if (!a.equals(actor)) {
                removeObject(a);
            }
        }
    }

    /**
     * Change the background image of the scrolling world to the given image.
     * 
     * @param bgImage
     *      The new background image.
     */
    public void setScrollingBackground(GreenfootImage bgImage) {
        textur = bgImage;
    }

    /**
     * Returns the width of to scrolling world. (0 => infinite).
     * 
     * @return
     *      The width of the scrolling system. If 0 is returned the world width is infinite.
     */
    public int getScrollingWidth() {
        return WORLD_WIDTH;
    }

    /**
     * Returns the height of to scrolling world. (0 => infinite).
     * 
     * @return
     *      The height of the scrolling system. If 0 is returned the world height is infinite.
     */
    public int getScrollingHeight() {
        return WORLD_HEIGHT;
    }

    /**
     * Get the total movement in x direction.
     * 
     * @return
     *      The total movement in x direction the scrolling actor has covered.
     */
    public int getTotalXMovement() {
        return totalXMovement;
    }

    /**
     * Get the total movement in y direction.
     * 
     * @return
     *      The total movement in y direction the scrolling actor has covered.
     */
    public int getTotalYMovement() {
        return totalYMovement;
    }
}
danpost danpost

2022/11/29

#
Fire wrote...
This is the code Not Sure what to change << Code Omitted >>
Maybe you should take a look at my Scrolling Tutorial.
Fire Fire

2022/11/29

#
danpost wrote...
Maybe you should take a look at my Scrolling Tutorial.
Can you send me a version that I can open in GreenFoot cause I like it and it is what I am looking for Also maybe this one as well?Scroller Class Demo
danpost danpost

2022/11/30

#
Fire wrote...
Can you send me a version that I can open in GreenFoot cause I like it and it is what I am looking for Also maybe this one as well? << Link Omitted >>
There is a link to the exact codes used in the class in the description of both scenarios linked in the previous post. No need to modify the Scroller class at all.
Fire Fire

2022/11/30

#
danpost wrote...
There is a link to the exact codes used in the class in the description of both scenarios linked in the previous post. No need to modify the Scroller class at all.
Which one is the Image code from the scrolling class demo in the scrolling Tutorial?
danpost danpost

2022/12/1

#
Fire wrote...
Which one is the Image code from the scrolling class demo in the scrolling Tutorial?
Please be specific. "Image code"? -- please be more specific and add details.
Fire Fire

2022/12/1

#
danpost wrote...
Please be specific. "Image code"? -- please be more specific and add details.
How in the demo where there is the image button and you click that and it brings you too a demo of this image in a box showing the limit to where the camera will follow it. Which of the written code corresponds to it in the scrolling tutorial?
danpost danpost

2022/12/2

#
Fire wrote...
How in the demo where there is the image button and you click that and it brings you too a demo of this image in a box showing the limit to where the camera will follow it. Which of the written code corresponds to it in the scrolling tutorial?
Ah. That is code you put in your world class. As there are an abundance of variants for scrolling, I could not be complete in the demos. I guess the closest one might be the Actor following scrolling code page where the world class limits the scrolling. As stated in the tutorial, the world class tells the scroller when to scroll and by how much in each direction. It is the responsibility of the world class to limit, if necessary, when and where scrolling is possible. The world code used for that particular one follows:
import greenfoot.*;
/**
 * CLASS: ImageScrollWorld
 *
 * DESCRIPTION: creates a limited (to scrolling background image size) center screen actor
 * following scrolling world 
 */
public class ImageScrollWorld extends DemoWorld
{
    public static final int WIDE = 600; // world width (viewport)
    public static final int HIGH = 400; // world height (viewport)
    
    Scroller scroller; // the object that performs the scrolling
    Actor scrollActor; // an actor to stay in view
    boolean roaming;
    
    public ImageScrollWorld()
    {
        super(WIDE, HIGH, 1, false); // creates an unbounded world
        setPaintOrder(Player.class);
        GreenfootImage bg = new GreenfootImage(100, 100); // creates an image to scroll
        bg.fill();
        bg.setColor(Color.LIGHT_GRAY);
        bg.fillRect(2, 2, 96, 96);
        bg.scale(1000, 1000);
        bg.setColor(Color.DARK_GRAY);
        for (int i=100; i<1000; i+=100)
        {
            bg.drawLine(i, 0, i, 1000);
            bg.drawLine(0, i, 1000, i);
        }
        int bgWide = bg.getWidth(); // scrolling image width
        int bgHigh = bg.getHeight(); // scrolling image height
        scroller = new Scroller(this, bg, bgWide, bgHigh); // creates the Scroller object
        scrollActor = new Player(); // creates the actor to maintain view on
        addObject(scrollActor, bgWide/2, bgHigh/2); // add actor at center of scrolling area (wherever)
        addObject(new Range(roaming), WIDE/2, HIGH/2);
        scroll(); // sets initial background image and puts main actor in view if needed
    }
    
    public void act()
    {
        checkEscape(); // to return to demo menu
        if (Greenfoot.mouseClicked(null))
        {
            roaming = !roaming;
            removeObjects(getObjects(Range.class));
            addObject(new Range(roaming), WIDE/2, HIGH/2);
        }
        if (scrollActor != null)
        {
            if (roaming) scroll2(); else 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 screen
        int dsy = scrollActor.getY()-HIGH/2; // vertical offset from center screen
        scroller.scroll(dsx, dsy);
    }

    // attempts scrolling when actor is outside roaming limits
    private void scroll2()
    {
        // roaming limits of actor
        int loX = 100;
        int hiX = WIDE-100;
        int loY = 50;
        int hiY = HIGH-50;
        // determine offsets and scroll
        int dsx = 0, dsy = 0;
        if (scrollActor.getX() < loX) dsx = scrollActor.getX()-loX;
        if (scrollActor.getX() > hiX) dsx = scrollActor.getX()-hiX;
        if (scrollActor.getY() < loY) dsy = scrollActor.getY()-loY;
        if (scrollActor.getY() > hiY) dsy = scrollActor.getY()-hiY;
        scroller.scroll(dsx, dsy);
    }
}
Fire Fire

2022/12/2

#
danpost wrote...
Ah. That is code you put in your world class. As there are an abundance of variants for scrolling, I could not be complete in the demos. I guess the closest one might be the Actor following scrolling code page where the world class limits the scrolling. As stated in the tutorial, the world class tells the scroller when to scroll and by how much in each direction. It is the responsibility of the world class to limit, if necessary, when and where scrolling is possible. The world code used for that particular one follows:
What would demo world be? In the code.
danpost danpost

2022/12/2

#
Fire wrote...
What would demo world be? In the code.
That is the root world of the demo worlds. DemoWorld contains the main controls of the demos -- most of the stuff that is common among all the demos. It would only have stuff pertaining to the demonstrations themselves and has nothing to do with the scrolling. I kept all the scrolling stuff in the specific demo worlds. In the class code given, if you change "DemoWorld" to "World" and remove line 43, it should work okay provided you have a Player and Range class.
Fire Fire

2022/12/2

#
danpost wrote...
That is the root world of the demo worlds. DemoWorld contains the main controls of the demos -- most of the stuff that is common among all the demos. It would only have stuff pertaining to the demonstrations themselves and has nothing to do with the scrolling. I kept all the scrolling stuff in the specific demo worlds. In the class code given, if you change "DemoWorld" to "World" and remove line 43, it should work okay provided you have a Player and Range class.
1. What would be the Range class be a subclass of? 2. Is there any specific Player class that I would have to make? 3. What would the scroller class code be? 4. Where would I put the Scroller class? 5. Is the Player a subclass of Scroller? 6. What is the code for the Scroller, Range, and Player
danpost danpost

2022/12/3

#
Fire wrote...
1. What would be the Range class be a subclass of? 2. Is there any specific Player class that I would have to make? 3. What would the scroller class code be? 4. Where would I put the Scroller class? 5. Is the Player a subclass of Scroller? 6. What is the code for the Scroller, Range, and Player
1. Range is a subclass of Actor. It is the rectangle that in the demo shows how far the Player object can move before scrolling starts. When a click is detected, the world will replace the Range object with a different one (it toggles between no movement before scrolling and some movement before scrolling. 2. The Player class does not need to be modified from any usual Player class in any scenario. All the Scroller class would do with a Player object is set it to be the actor that stays focused on. What the player is or what it does is not pertinent to the scrolling. 3. Link to the Scroller class code is in the description of both linked scenario above. 4. In the main menubar, select "Edit >> New class...", give it any name and create the class. Then, open its editor and remove ALL its code with the Scroller class' code. 5. As per (2) above, the code of the Player class has nothing to do with the scrolling or the Scroller class. 6. All previously answered above (in this post). However, what I used for my Player class in the demo was the following:
import greenfoot.*;

public class Player extends Actor
{
    int speed = 5;

    public void act()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("up")) dy--;
        dx *= speed;
        dy *= speed;
        if (getX()+dx < 32) dx = 32-getX();
        if (getX()+dx > getWorld().getWidth()-32) dx = getWorld().getWidth()-32-getX();
        if (getY()+dy < 46) dy = 46-getY();
        if (getY()+dy > getWorld().getHeight()-46) dy = getWorld().getHeight()-46-getY();
        setLocation(getX()+dx, getY()+dy);
    }
}
Fire Fire

2022/12/5

#
danpost wrote...
1. Range is a subclass of Actor. It is the rectangle that in the demo shows how far the Player object can move before scrolling starts. When a click is detected, the world will replace the Range object with a different one (it toggles between no movement before scrolling and some movement before scrolling. 2. The Player class does not need to be modified from any usual Player class in any scenario. All the Scroller class would do with a Player object is set it to be the actor that stays focused on. What the player is or what it does is not pertinent to the scrolling. 3. Link to the Scroller class code is in the description of both linked scenario above. 4. In the main menubar, select "Edit >> New class...", give it any name and create the class. Then, open its editor and remove ALL its code with the Scroller class' code. 5. As per (2) above, the code of the Player class has nothing to do with the scrolling or the Scroller class. 6. All previously answered above (in this post). However, what I used for my Player class in the demo was the following:
import greenfoot.*;

public class Player extends Actor
{
    int speed = 5;

    public void act()
    {
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("down")) dy++;
        if (Greenfoot.isKeyDown("up")) dy--;
        dx *= speed;
        dy *= speed;
        if (getX()+dx < 32) dx = 32-getX();
        if (getX()+dx > getWorld().getWidth()-32) dx = getWorld().getWidth()-32-getX();
        if (getY()+dy < 46) dy = 46-getY();
        if (getY()+dy > getWorld().getHeight()-46) dy = getWorld().getHeight()-46-getY();
        setLocation(getX()+dx, getY()+dy);
    }
}
1. What would the code for the Range class be if there is any? 2. I am also getting Errors relating to Scroller, Range, and scroll how would I fix it?
danpost danpost

2022/12/5

#
Fire wrote...
What would the code for the Range class be if there is any?
I do not why you would want it as it was just a visual aide for the demo. But, here it is:
import greenfoot.*;

public class Range extends Actor
{
    public Range(boolean open)
    {
        GreenfootImage img = new GreenfootImage(open ? 436 : 35, open ? 360 : 64);
        img.setColor(new Color(0, 20, 40, 32));
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
        setImage(img);
    }

    public void setLocation(int x, int y) {} // to prevent scrolling from moving this actor
}
Fire Fire

2022/12/6

#
danpost wrote...
I do not why you would want it as it was just a visual aide for the demo. But, here it is:
import greenfoot.*;

public class Range extends Actor
{
    public Range(boolean open)
    {
        GreenfootImage img = new GreenfootImage(open ? 436 : 35, open ? 360 : 64);
        img.setColor(new Color(0, 20, 40, 32));
        img.fill();
        img.setColor(Color.BLACK);
        img.drawRect(0, 0, img.getWidth()-1, img.getHeight()-1);
        setImage(img);
    }

    public void setLocation(int x, int y) {} // to prevent scrolling from moving this actor
}
How would I fix the error message with Scroller, Range, and scroll classes?
There are more replies on the next page.
1
2
3