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

2013/10/23

Need an object fixed to the screen, not the map.

1
2
3
4
Tavi Tavi

2013/10/23

#
Hello Everyone :) , I'm currently having some trouble with my GUI ( lifebar, money counter and level representation ). The REAL issue here is that my maps are way bigger then my screen,, wich forces me to scroll at this point ( HUGE ISSUE ). i've been trying to work around it so far, but now when i scroll, my scorecounters just slide off screen :( .. Can anyone help me with this ?? I'm not too technical, so any well working examples or extended comments are more then welcome :) .. Thank you ! EDIT : This is what i really need :(
Gevater_Tod4711 Gevater_Tod4711

2013/10/23

#
Are you using a scrolling engine? If not you can use my Infinite Scrolling World. I think this engine should do exactly what you want.
SPower SPower

2013/10/23

#
This would also be useful: http://www.greenfoot.org/scenarios/5806 You can just choose which engine you find the easiest or most powerful.
danpost danpost

2013/10/23

#
Basically, you have two types of Actors; those that should scroll and those that should not. Your scrolling engine needs to be able to distinguish between the two. The easiest way is to subclass all the classes that create scrolling objects under one super actor class 'Scroller'. Then adjust your engine to move objects listed as 'Scroller' objects instead of all objects. As an alternative, you can use a List and add all scrolling objects into the list instead of superclassing the scrollers; and use that list in your scrolling engine.
Tavi Tavi

2013/10/23

#
Thank you :) ! Although my game is starting to get pretty complex, i will try to implement it in , hopefully it's still possible. I will let you guys know soon :) . Oh and my screen is not infinite though, i do have portals with multiple worlds.. But i assume that i can adress that somewhere in the code.. I will post updates on this issue here :) .
Tavi Tavi

2013/10/23

#
Okay so i tried all night, (using SPower's scenario) to try and convert my own project. Everything seems fine at first when i start my game ( Awsome ! ): However, if i press one key :( it all goed to sh*t... Enemies fall trough the floor, walk in the sky, i also teleport to the floor and i can't get out of it ! What's going on here? And i will be happy to post code if needed, it's alot of code though.. Thank you guys in advance, and i hope we can resolve this issue. Getting there though !
Gevater_Tod4711 Gevater_Tod4711

2013/10/24

#
I think your game is a platformer right? So if your enemys seem to ignore the platforms and go through them you should post the code of the enemies and the platforms (an the class where the enemies check whether there is a platform if this is not the enemy class). But I don't realy get what you mean by enemies walking in the sky. Could you explain that a bit more prezise? To help you with your second problem you need to post the code of your teleporter and maybe the world class if the teloporting system is connected to the world.
Tavi Tavi

2013/10/24

#
Hi Guys :) Ok let's post some info ! And i might see how this is, It's not that my enemies fall trough the floor, but my background is the only thing that's moving .. not my map itself. I will try to explain this as precisely as i can for you guys :) First, What's Happening ??: On Game startup (looks fine ) ( i've colored the floor for you guys ) : I Use a background image and invisible platforms to walk on When i move : All all that's moving is the background, not the platforms... Now to show some of my classes : Current world im using : Some of my classes : ( Please note, the mover class is the mover class provided by greenfoor support classes) And now for my code ! Class ScrollWorld
import greenfoot.*;
import java.util.ArrayList;

/**
 * A world which has the possibility to scroll
 * over a really big area. That big area is called
 * the big space, It scrolls over it using a camera.
 * The camera's location starts as x=half width, y=half height
 * of the world. It can't get outside of the big space.<p>
 * 
 * The background is always scrolling, you don't have
 * to do anything for it. Just make a background like you
 * always do in your scenarios, and this scenario will
 * scroll over it. The only note is that the method
 * {@link setBackground} is replaced by {@link 
 * setNewBackground}.<p>
 * 
 * The world supports adding ScrollActors in 2 ways:
 * <Li>
 * Adding it in the regular way, which means that it can
 * go off screen and come back on screen.
 * </Li><Li>
 * Adding a camera follower, which means that it will
 * move just as much as the camera does. So if you add it
 * to the center of the screen, it will always be there 
 * (except for when you change it yourself).
 * </Li>
 * <b>IMPORTANT NOTE:</b> if your Actor isn't a subclass of ScrollActor, it
 * will looklike it is a camera follower.
 * 
 * @author Sven van Nigtevecht
 * @version 2.1.2
 */
public abstract class ScrollWorld extends World
{
    private final int width, height, cellSize;
    private final ArrayList<ScrollActor> objects;
    private final ArrayList<ScrollActor> camFollowers;
    private final int fullWidth, fullHeight;
    
    private int camX, camY, camDir;
    
    private final GreenfootImage bigBackground, back;
    private int scrollPosX, scrollPosY;
    
    /**
     * Create a new ScrollWorld.
     * @param width The width of the scroll world in cells.
     * @param height The height of the scroll world in cells.
     * @param cellSize The size of the cells (in pixels).
     * @param fullWidth The total width of the world.
     * That means, objects can't move further than this limit.
     * @param fullHeight The total height of the world.
     * That means, objects can't move further than this limit.
     * @throws IllegalArgumentException If one of the arguments
     * is smaller or equal to 0.
     */
    public ScrollWorld(int width, int height, int cellSize, int fullWidth, int fullHeight)
    {
        super(width, height, cellSize, false);
        this.back = getBackground();
        this.width = back.getWidth();
        this.height = back.getHeight();
        this.cellSize = cellSize;
        this.fullWidth = fullWidth;
        this.fullHeight = fullHeight;
        if (fullWidth <= 0)
            throw new IllegalArgumentException("The width of the big space ("+fullWidth
            +") can't be smaller then the width of the world ("+width+")");
        if (fullHeight <= 0)
            throw new IllegalArgumentException("The height of the big space ("+fullHeight
            +") can't be smaller then the height of the world ("+height+")");
        
        objects = new ArrayList<ScrollActor>();
        camFollowers = new ArrayList<ScrollActor>();
        
        camX = getWidth() /2;
        camY = getHeight() /2;
        camDir = 0;
        
        scrollPosX = 0;
        scrollPosY = 0;
        
        bigBackground = new GreenfootImage(width+width, height+height);
        setNewBackground(back);
    }
    
    /** EXTRA METHODS: */
    
    /**
     * Sets the background of the world. This will also initialize
     * everything to make the background scroll, something the
     * normal {@link setBackground} method doesn't.
     */
    public void setNewBackground(GreenfootImage background)
    {
        bigBackground.clear();
        if (background.getWidth() == bigBackground.getWidth() &&
            background.getHeight() == bigBackground.getHeight()) {
            bigBackground.drawImage(background, 0,0);
            back.clear();
            back.drawImage(bigBackground, scrollPosX,scrollPosY);
            return;
        }
        
        bigBackground.drawImage(background, 0,0);
        bigBackground.drawImage(background, background.getWidth(),0);
        bigBackground.drawImage(background, 0,background.getHeight());
        bigBackground.drawImage(background, background.getWidth(),background.getHeight());
        
        back.clear();
        back.drawImage(bigBackground, scrollPosX,scrollPosY);
    }
    
    /** ADDING + REMOVING OBJECTS: */
    
    /**
     * Adds an object which will follow the camera.
     * The location is seen from the camera, not from the
     * big space.
     * @param cameraFollower The object that will be added to the world
     * as a camera follower.
     * @param x The x coördinate seen from the camera where the object
     * will be added.
     * @param y The y coördinate seen from the camera where the object
     * will be added.
     * @see #addObject(ScrollActor, int, int)
     */
    public void addCameraFollower(ScrollActor cameraFollower, int x, int y)
    {
        super.addObject(cameraFollower, getWidth() /2 +x, getHeight() /2 +y);
        camFollowers.add(cameraFollower);
        cameraFollower.setIsCameraFollower(true);
    }
    
    /**
     * Adds an object to the the world. If the given object
     * is a ScrollActor or a subclass of it, the x and y
     * coördinates are in the big space.
     * 
     * @param object The object that will be added to the world.
     * @param x The x coördinate in the world where the object
     * will be added.
     * @param y The y coördinate in the world where the object
     * will be added.
     * @see #addCameraFollower(ScrollActor, int, int)
     */
    public void addObject(Actor object, int x, int y)
    {
        if (object instanceof ScrollActor) {
            if (x >= fullWidth)
                x = fullWidth -1;
            else if (x < 0)
                x = 0;
            if (y >= fullHeight)
                y = fullHeight -1;
            else if (y < 0)
                y = 0;
            ScrollActor sa = (ScrollActor) object;
            super.addObject(sa, x -(camX -getWidth() /2), y -(camY -getHeight() /2));
            objects.add(sa);
            sa.setIsCameraFollower(false);
        } else
            super.addObject(object,x,y);
    }
    
    /**
     * Removes an object from the world.
     * @param object The object that will be removed
     * from the world. This can either be a camera follower,
     * or just a regular object.
     */
    public void removeObject(Actor object)
    {
        super.removeObject(object);
        if (object instanceof ScrollActor) {
            ScrollActor a = (ScrollActor) object;
            objects.remove(a);
            camFollowers.remove(a);
            a.setIsCameraFollower(false);
        }
    }
    
    /** RETURN VALUES: */
    
    /**
     * Returns the camera's x coördinate in big space.
     * @see #getCameraY
     */
    public int getCameraX()
    {
        return camX;
    }
    
    /**
     * Returns the camera's y coördinate in big space.
     * @see #getCameraX
     */
    public int getCameraY()
    {
        return camY;
    }
    
    /**
     * Returns the width of the big space.
     * @see #getFullHeight
     */
    public int getFullWidth()
    {
        return fullWidth;
    }
    
    /**
     * Returns the height of the big space.
     * @see #getFullWidth
     */
    public int getFullHeight()
    {
        return fullHeight;
    }
    
    /** CAMERA MOVEMENT + ROTATION: */
    
    /**
     * Moves the camera to a particular location.
     * Note that this is a location in the big space.
     * @param x The new x coördinate of the camera.
     * @param y The new y coördinate of the camera.
     */
    public void setCameraLocation(int x, int y)
    {
        if (camX == x && camY == y) return;
        if (x > fullWidth -getWidth() /2)
            x = fullWidth -getWidth() /2;
        else if (x < getWidth() /2)
            x = getWidth() /2;
        if (y > fullHeight -getHeight() /2)
            y = fullHeight -getHeight() /2;
        else if (y < getHeight() /2)
            y = getHeight() /2;
        int dx = x -camX;
        int dy = y -camY;
        camX = x;
        camY = y;
        for (ScrollActor a : objects)
            a.setLocation(a.getX() -dx, a.getY() -dy);
        for (ScrollActor a : camFollowers)
            a.setLocation(a.getX(), a.getY());
        moveBackgroundRight(dx *cellSize);
        moveBackgroundUp(dy *cellSize);
    }
    
    /**
     * Sets the direction the camera is facing.
     * It doesn't change anything you see, but it makes
     * it possible to use the {@link moveCamera} method.
     * @param degrees The new rotation in degrees.
     * @see #turnCamera(int)
     * @see #moveCamera(int)
     */
    public void setCameraDirection(int degrees)
    {
        if (degrees >= 360) {
            if (degrees < 720)
                degrees -= 360;
            else
                degrees %= 360;
        } else if (degrees < 0) {
            if (degrees >= -360)
                degrees += 360;
            else
                degrees = 360 +(degrees %360);
        }
        if (camDir == degrees) return;
        camDir = degrees;
    }
    
    /**
     * Turns the camera.
     * It doesn't change anything you see, but it makes
     * it possible to use the {@link moveCamera} method.
     * @param amount The number of degrees the camera will
     * turn clockwise. If this is negative, it will turn
     * counter-clockwise.
     * @see #setCameraDirection(int)
     * @see #moveCamera(int)
     */
    public void turnCamera(int amount)
    {
        setCameraDirection(camDir +amount);
    }
    
    /**
     * Moves the camera forward to the direction
     * it's facing (to go backwards, enter a negative number).
     * @param amount The number of cells the camera will move.
     * When this is negative, the camera will move forward.
     */
    public void moveCamera(int amount)
    {
        if (amount == 0) return;
        double radians = Math.toRadians(camDir);
        double dx = Math.cos(radians) *amount;
        double dy = Math.sin(radians) *amount;
        setCameraLocation((int)(camX +dx +0.5), (int)(camY +dy +0.5));
    }
    
    /** MOVING BACKGROUND: */
    
    /**
     * All the honor for this goes to Busch2207 from
     * greenfoot.org
     */
    private void moveBackgroundUp(int amount)
    {
        if (amount == 0) return;
        int height = getHeight();
        scrollPosY -= amount;
        while (scrollPosY < 0)
            scrollPosY += height;
        scrollPosY %= height;
        getBackground().drawImage(bigBackground, scrollPosX -getWidth(),scrollPosY -height);
    }
    
    /**
     * All the honor for this goes to Busch2207 from
     * greenfoot.org
     */
    private void moveBackgroundRight(int amount)
    {
        if (amount == 0) return;
        int width = getWidth();
        scrollPosX -= amount;
        while (scrollPosX < 0)
            scrollPosX += width;
        scrollPosX %= width;
        getBackground().drawImage(bigBackground, scrollPosX -width,scrollPosY -getHeight());
    }
}
Rogue Char
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Char here.
 * 
 * @author (Patrick van Kralingen) 
 * @version (0.14  19OKT2013)
 */
public class Rogue extends ScrollActor
{
    //Verticale snelheid en acceleratie
    private int vSpeed = 0;
    private int acceleration = 1;
    private boolean jumping;
    private int jumpStrength = -15;

    //Loop variabelen
    private int speed = 24;
    //Frame float
    public float frame = 0;
    long lastAdded = System.currentTimeMillis(); 
    // init sequence
    private int sequence = 0;
    //richting
    public int richting = 2;

    //CHAR Eigenschappen
    private int mesos = 0;
    public int level = 1;
    public int hp = 120;
    private LevelCounter currentlevel;
    private MesoCounter currentmeso;  
    private LifeBar currenthp;

    private int xValue;
    private int yValue;

    //Richting aanval
    public double throwingDirection = 10.0; 

    //Shoot interaction
    private int reloadDelayCount;   
    private int throwReloadTime;   

    // Levelup
    private LevelUp levelUp;
    private Thombstone thombstone;

    // CLAW & STER
    public int claw = 0 ;
    public int throwingStar = 0;
    
    /****
       *
        *SCROLL WORLD VARIABELEN
         *
          *
           *
            ********/
    /** The number of cells we move forward and backword */
    private static final int MOVE_AMOUNT = 5;        
            
            
    /**
     * SPRITES INLADEN
     **/
    //links lopen
    GreenfootImage[] walkLeft = new GreenfootImage[]
        {
            new GreenfootImage("Rogue_Walk_left_1_0.png"),
            new GreenfootImage("Rogue_Walk_left_1_1.png"),
            new GreenfootImage("Rogue_Walk_left_1_2.png"),
            new GreenfootImage("Rogue_Walk_left_1_3.png")
        };  
    //rechts lopen
    GreenfootImage[] walkRight = new GreenfootImage[]
        {
            new GreenfootImage("Rogue_Walk_right_1_0.png"),
            new GreenfootImage("Rogue_Walk_right_1_1.png"),
            new GreenfootImage("Rogue_Walk_right_1_2.png"),
            new GreenfootImage("Rogue_Walk_right_1_3.png")
        };
    //Idle links
    GreenfootImage [] alertLeft = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Alert_left_0.png"),
            new GreenfootImage("Rogue_Alert_left_1.png"),
            new GreenfootImage("Rogue_Alert_left_2.png"),
            new GreenfootImage("Rogue_Alert_left_3.png"),
            new GreenfootImage("Rogue_Alert_left_4.png")
        };
    //Idle rechts
    GreenfootImage [] alertRight = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Alert_right_0.png"),
            new GreenfootImage("Rogue_Alert_right_1.png"),
            new GreenfootImage("Rogue_Alert_right_2.png"),
            new GreenfootImage("Rogue_Alert_right_3.png"),
            new GreenfootImage("Rogue_Alert_right_4.png")
        };
    //Aanvallen links
    GreenfootImage [] attackLeft = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Attack_left_0.png"),
            new GreenfootImage("Rogue_Attack_left_1.png"),
            new GreenfootImage("Rogue_Attack_left_2.png"),
            new GreenfootImage("Rogue_Attack_left_3.png"),
            new GreenfootImage("Rogue_Attack_left_4.png"),
            new GreenfootImage("Rogue_Attack_left_5.png"),
            new GreenfootImage("Rogue_Attack_left_6.png"),
            new GreenfootImage("Rogue_Attack_left_7.png"),
            new GreenfootImage("Rogue_Attack_left_8.png"),
        };

    GreenfootImage [] attackRight = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Attack_right_0.png"),
            new GreenfootImage("Rogue_Attack_right_1.png"),
            new GreenfootImage("Rogue_Attack_right_2.png"),
            new GreenfootImage("Rogue_Attack_right_3.png"),
            new GreenfootImage("Rogue_Attack_right_4.png"),
            new GreenfootImage("Rogue_Attack_right_5.png"),
            new GreenfootImage("Rogue_Attack_right_6.png"),
            new GreenfootImage("Rogue_Attack_right_7.png"),
            new GreenfootImage("Rogue_Attack_right_8.png"),
        };
        
        GreenfootImage [] climb = new GreenfootImage []
        {
            new GreenfootImage("rope_0.png"),
            new GreenfootImage("rope_1.png")
        };

    //SPRINGEN
    GreenfootImage [] jumpLeft = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Jump_left_0.png")
        };
    GreenfootImage [] jumpRight = new GreenfootImage []
        {
            new GreenfootImage("Rogue_Jump_right_0.png")
        };

    // Rogue constructor
    public Rogue(LevelCounter levelCounter, MesoCounter mesocounter, LifeBar lifebar)
    {
        //throwReloadTime = 22;
        throwReloadTime = 3;
        reloadDelayCount = 0;
        currentlevel = levelCounter;
        currentmeso = mesocounter;
        currenthp = lifebar;
    }

    public void act() 
    {
        checkFall();
        checkKey();
        //checkRightWalls();
        //checkLeftWalls();
        animate(sequence);
        timer();
        reloadDelayCount++;
        checkForPortal();  
        //makeCenter();
        // Toevoeging 22OCT2013
        checkLiane();
    }
    
    /*******
       *
        *Scrollable world method
         *
          *
           */
    
    //public void makeCenter()
   // {
   // MouseInfo m = Greenfoot.getMouseInfo();
        //if (m != null) {
            //turnTowards(m.getX(), m.getY());
            // set the camera's direction to ours:
            //getWorld().setCameraDirection(getRotation());
        //}
        //if (Greenfoot.isKeyDown("down")) {
            // move the camera backwards:
            //getWorld().moveCamera(-MOVE_AMOUNT);
        //}
        //if (Greenfoot.isKeyDown("up")) {
            // move the camera forwards:
       //     getWorld().moveCamera(MOVE_AMOUNT);
        //}
    //}
    

    /**
     * CONTROLS
     **/
    public void checkKey()
    {
        if(Greenfoot.isKeyDown("space") && jumping == false)
        {
            sequence = 1;
            jump();
        }

        if (Greenfoot.isKeyDown("right"))
        {
            sequence = 2;
            richting = 1;
            //moveRight();
            getWorld().moveCamera(MOVE_AMOUNT);
        }
        if (Greenfoot.isKeyDown("left"))
        {
            sequence = 3;
            richting = 2;
            //moveLeft();
            getWorld().moveCamera(-MOVE_AMOUNT);
        }
        if ("a".equals(Greenfoot.getKey()))
        {
            sequence = 4;
            luckySeven(richting);
        }
        //if (getWorld().getObjects(LevelUp.class).isEmpty() && Greenfoot.isKeyDown("u")){
         //   levelUp();
       // }

        // Tijdelijk wordt de thombstone opgevangen middels een key
        // Deze moet straks worden geactiveerd als hij dood gaat.
        //if (getWorld().getObjects(Thombstone.class).isEmpty() && Greenfoot.isKeyDown("y")){
         //   thombstone = new Thombstone();
          //  getWorld().addObject(thombstone, getX(), -200);
        //}
    }

    public void checkLiane(){
        Actor lianeActor = getOneIntersectingObject(Liane.class);

        yValue = getY();

        if(lianeActor != null && Greenfoot.isKeyDown("up")){
            System.out.println(yValue);
            System.out.println("Ik probeer te klimmen.");
            climbInLiane();
            sequence = 5;
        }
    }

    public void climbInLiane(){
        // Vallen uitschakelen
        vSpeed = 0;

        // Rogue X value gelijk stellen aan die van Liane
        // System.out.println(getWorld().getObjects(Liane.class));

        yValue = getY();
        // Vreemd.... bij waarde yValue = 5 klimt ie niet meer....
        yValue -= 2;
        // Nog Gelijk stellen aan X van Liane

        Liane liane = (Liane) getOneIntersectingObject(Liane.class);  

        setLocation(liane.getX(), yValue);  
        // Stil hangen in touw verwerken
    }

    // Toevoeving Portal
    public void checkForPortal(){
        Portal portal = (Portal) getOneObjectAtOffset(10, 10, Portal.class);
        // Te lang op de portal geeft andere effecten
        // opvangen met getKey (indien mogelijk)
        // NOG TE DOEN: OP GOEDE PLEK TERECHT LATEN KOMEN,,,
        // MISSCHIEN MET GETTER AND SETTERS.
        // Proberen te verwerken in portal --> ding voor later
        if(portal != null && Greenfoot.isKeyDown("up")){
            // Description of cases
            // Case 1: Portal to TrainingGround
            // Case 2: Portal to Questmap
            // Case 3: Portal to map3
            // Case 4: Portal to mapTransition1
            // Case 5: Portal to mapTransition2
            // Case 6: Portal to IronBoarLand
            // Case 7: Portal to BossMap
            Greenfoot.playSound("Portal.wav");
            switch (portal.getOurWorld()){
                case 1: 
                Greenfoot.setWorld(new TrainingGround());
                break;

                case 2: 
                Greenfoot.setWorld(new Questmap());
                break;

                case 3: 
                Greenfoot.setWorld(new map3());      
                break;

                case 4:
                Greenfoot.setWorld(new mapTransition1()); 
                break;

                case 5:
                Greenfoot.setWorld(new mapTransition2()); 
                break;
                
                case 6:
                Greenfoot.setWorld(new IronBoarLand()); 
                break;
                
                case 7:
                Greenfoot.setWorld(new BossMap()); 
                break;
               
                default:
                break;
            }
        }
    }

    /*************
     *
     *
     *
     *
     *PHYSYCS & MOVEMENT
     *
     *
     *
     **********/

    //Naar links kunnen bewegen
    public void moveLeft()
    {
        setLocation(getX()-speed, getY());
    }

    //Dit is het hoofd-onderdeel dat ervoor zorgt dat char valt wanneer hij de grond raakt.
    public void fall() 
    {
        setLocation (getX(), getY() +vSpeed);
        if (vSpeed <=9)
        {
            //elke pixel dat hij valt, word er +1 extra pixel bijgevoegd (exponentieel)
            vSpeed = vSpeed + acceleration;
        }
        jumping = true;
    }
    //Eerst controleren of ons character op de grond staat.
    public boolean onGround() 
    {
        //Uitrekenen hoe hoog de sprite is.
        int spriteHeight = getImage().getHeight();
        // Dit pakt de sprite hoogte, en deelt deze door 2, ook vind conversie plaats en worden er 5 extra pixels gegeven.
        int lookForGround = (int)(spriteHeight/2) + 5;
        //Hier word er gezocht naar de grond, en er word direct naar beneneden gekeken.
        Actor ground = getOneObjectAtOffset(0, lookForGround, platform.class);
        //als er niets onder hem is, false, als er wel iets onder zn voeten is, true
        if (ground == null)
        {
            jumping = true;
            return false;
        }
        else 
        {
            moveToGround(ground);
            return true;
        }

    }

    public boolean checkRightWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);

        Actor rightWall = getOneObjectAtOffset(xDistance, 0, platform.class);

        if (rightWall == null) 
        {
            return false;
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }

    public void stopByRightWall (Actor rightWall)
    {
        int wallWidth = rightWall.getImage().getWidth();
        int newX = rightWall.getX() -(wallWidth + getImage().getWidth())/2;
        setLocation(newX - 2, getY());
    }

    //niet door LINKER muur heenlopen
    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/2);

        Actor leftWall = getOneObjectAtOffset(xDistance, 0, platform.class);

        if (leftWall == null) 
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }

    public void stopByLeftWall (Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() +(wallWidth + getImage().getWidth())/2;
        setLocation(newX + 2, getY());
    }

    //Dit plaatst het character precies met zn voeten op de grond   
    public void bopHead (Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight()) / 2;
        //Dit plaatst hem werkelijk   
        setLocation(getX(), newY);
    }

    //Dit plaatst het character precies met zn voeten op de grond   
    public void moveToGround (Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight()) / 2;
        //Dit plaatst hem werkelijk   
        setLocation(getX(), newY);
        //hier is hij weer klaar om te springen
        jumping = false;
    }
    //als hij op de grond staat, vSpeed word naar 0 gezet, zo niet word de fall methode aangeroepen
    public void checkFall() 
    {
        if(onGround())
        {
            vSpeed =0;
        }
        else
        {
            fall();
        }
    }

    public void jump()
    {
        Greenfoot.playSound("Jump.wav");
        vSpeed = vSpeed = jumpStrength;
        jumping = true;
        fall();
    }

    /*************
     *
     *
     *
     *
     *Animaties
     *
     *
     *
     **********/
    public void animate(int sequence) {
        switch(sequence)
        {
            case 0:
            idle(richting);
            break;
            case 1:
            animateJump(richting);
            break;
            case 2: 
            animateRight();
            break;
            case 3:
            animateLeft();
            break;
            case 4:
            animateAttack(richting);
            break;
            case 5: 
            animateClimb();
            break;
            case 6: 
            break;
            default:
            break;
        }
    }

    public void timer()
    {  
        long curTime  = System.currentTimeMillis();  
        if (curTime >= lastAdded + 150) //5000ms = 5s  
        {  
            frame++;
            lastAdded  = curTime;  
        }  
    }  

    public void idle (int richting)
    {
        if(richting == 2)
        {
            animateAlertLeft();
        }
        if (richting == 1)
        {
            animateAlertRight();
        }
    }

    public void animateAlertRight()
    {
        if(frame == 1)
        {
            setImage(alertRight[0]);
        }
        else if (frame == 2)
        {
            setImage(alertRight[1]);
        }
        if(frame == 3)
        {
            setImage(alertRight[2]);
        }
        else if (frame == 4)
        {
            setImage(alertRight[3]);
            frame = 0;
            return;
        }

    }

    public void animateAlertLeft()
    {
        if(frame == 1)
        {
            setImage(alertLeft[0]);
        }
        else if (frame == 2)
        {
            setImage(alertLeft[1]);
        }
        if(frame == 3)
        {
            setImage(alertLeft[2] );
        }
        else if (frame == 4)
        {
            setImage(alertLeft[3]);
            frame = 0;
            return;
        }

    }

    //Naar rechts kunnen bewegen
    public void moveRight()
    {
        setLocation(getX()+speed, getY());
    }

    //animatie rechts lopen
    public void animateRight()
    {
        if(frame == 1)
        {
            setImage(walkRight[0]);
        }
        else if (frame == 2)
        {
            setImage(walkRight[1]);
        }
        if(frame == 3)
        {
            setImage(walkRight[2] );
        }
        else if (frame == 4)
        {
            setImage(walkRight[3]);
            frame = 0;
            sequence = 0;
            return;
        }
    }

    //animatie links lopen
    public void animateLeft()
    {
        if(frame == 1)
        {
            setImage(walkLeft[0]);
        }
        else if (frame == 2)
        {
            setImage(walkLeft[1]);
        }
        if(frame == 3)
        {
            setImage(walkLeft[2]);
        }
        else if (frame == 4)
        {
            setImage(walkLeft[3]);
            frame = 0;
            sequence = 0;
            return;
        }
    }

    public void animateJump(int richting)
    {
        if(richting == 2)
        {           
            setImage(jumpLeft[0]);
            frame = 0;
            return;

        }
        if (richting == 1)
        {
            setImage(jumpRight[0]);
            frame = 0;
            return;
        }

    }

    //FIRE ZE MISSILES! 
    public void animateAttack(int richting)
    {
        if(richting == 2)
        {
            if(frame == 1)
            {
                setImage( attackLeft [0]);
            }
            if(frame == 2)
            {
                setImage( attackLeft [1]);
            }
            if(frame == 3)
            {
                setImage( attackLeft [2]);
            }
            if(frame == 4)
            {
                setImage( attackLeft [3]);
            }
            if(frame == 5)
            {
                setImage( attackLeft [4]);
            }
            if(frame == 6)
            {
                setImage( attackLeft [5]);
            }
            if(frame == 7)
            {
                setImage( attackLeft [6]);
            }
            if(frame == 8)
            {
                setImage( attackLeft [7]);
                frame = 0;
                sequence = 0;
                return;
            }
        }
        if(richting == 1)
        {
            if(frame == 1)
            {
                setImage( attackRight [0]);
            }
            if(frame == 2)
            {
                setImage( attackRight [1]);
            }
            if(frame == 3)
            {
                setImage( attackRight [2]);
            }
            if(frame == 4)
            {
                setImage( attackRight [3]);
            }
            if(frame == 5)
            {
                setImage( attackRight [4]);
            }
            if(frame == 6)
            {
                setImage( attackRight [5]);
            }
            if(frame == 7)
            {
                setImage( attackRight [6]);
            }
            if(frame == 8)
            {
                setImage( attackRight [7]);
                frame = 0;
                sequence = 0;
                return;
            }
        }
    }
            public void animateClimb()
            {
                if(frame == 1)
                    {
                        setImage(climb[0]);
                    }
                    else if (frame == 2)
                    {
                       setImage(climb[1]);
                       frame = 0;
                       sequence = 1;
                       return;
                    }

            }

    /*************
     *
     *
     *
     *
     *USER ACTIONS
     *
     *
     *
     **********/
    public void levelUp()
    {
        hp =(int) (hp * 1.32);
        currentlevel.add(1);
        Greenfoot.playSound("LevelUp.wav");
        levelUp = new LevelUp();
        getWorld().addObject(levelUp, getX(), getY()-181+70);
        
    }

    public void luckySeven(int richting)
    {
        if(richting == 1)
        {
            throwRight();
        }
        if(richting == 2)
        {
            throwLeft();
        }
    }

    public void throwRight()
    {
        if(reloadDelayCount >= throwReloadTime)
        {
            Ilbi throwingstar = new Ilbi();
            getWorld().addObject(throwingstar, getX(), getY());
            throwingDirection = 10.0;
            Greenfoot.playSound("Throw.wav");
            reloadDelayCount = 0;
            throwingstar.move(40.0);
        }

    }

    public void throwLeft()
    {
        if (reloadDelayCount >= throwReloadTime)
        {
            Ilbi throwingstar = new Ilbi();
            getWorld().addObject(throwingstar, getX(), getY());
            throwingDirection = -10.0;
            throwingstar.setRotation(180); 
            Greenfoot.playSound("Throw.wav");
            reloadDelayCount = 0;
            throwingstar.move(30.0);
        }

    }

    public void setGunReloadTime(int reloadTime)
    {
        throwReloadTime = reloadTime;
    }

} 
My TrainingGround World
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Sky here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class TrainingGround extends ScrollWorld
{
    
    private Rogue theRogue;
    private int xValueRogue = 500;
    private int yValueRogue = 70;
    private int spawnTimer = 300;
    GreenfootSound trainingGround = new GreenfootSound("TrainingGround.wav");
    /**
     * Constructor for objects of class Sky.
     * 
     */
    public TrainingGround()
    {   
        
       super(800, 600, 1, 1600, 1000);
       
        

        prepare();

    }

    //public TrainingGround(int xValueRogue, int yValueRogue){
        //super(1600, 1000, 1); 
        //this.xValueRogue = xValueRogue; 
        //this.yValueRogue = yValueRogue;        
    //}

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        RibbonPig ribbonpig = new RibbonPig();
        CouragiousLamb couragiouslamb = new CouragiousLamb();
        addObject(ribbonpig, 734, 299);
        Portal portal = new Portal(2);
        addObject(portal, 206, 338);

        setBackground("lvl1.png");

        //blokje rechtsonder
        addObject (new platform(), 800  , 585);
        addObject (new platform(), 850  , 585); 

        addObject (new platform(), 900  , 585); 
        addObject (new platform(), 950  , 585); 
        addObject (new platform(), 1000  , 585); 
        addObject (new platform(), 1050  , 585); 
        addObject (new platform(), 1100  , 585); 
        addObject (new platform(), 1150  , 585); 
        addObject (new platform(), 1200  , 585); 
        addObject (new platform(), 1250  , 585); 
        addObject (new platform(), 1300  , 585); 
        addObject (new platform(), 1328  , 585); 

        //NaastPortal
        addObject (new platform(), 700, 525);
        addObject (new platform(), 650, 525);
        addObject (new platform(), 600, 525);
        addObject (new platform(), 550, 525);
        addObject (new platform(), 500, 525);
        addObject (new platform(), 450, 525);
        addObject (new platform(), 400, 525);
        addObject (new platform(), 350, 525);

        //blokje links
        addObject (new platform(), 175, 465);
        addObject (new platform(), 225, 465);
        addObject (new platform(), 275, 465);

        //blok rechts
        addObject (new platform(), 1425, 525);
        addObject (new platform(), 1475, 525);
        addObject (new platform(), 1510, 525);

        //TOP - 1
        addObject (new platform(), 375, 345);
        addObject (new platform(), 425, 345);
        addObject (new platform(), 475, 345);
        addObject (new platform(), 525, 345);
        addObject (new platform(), 575, 345);
        addObject (new platform(), 625, 345);
        addObject (new platform(), 675, 345);
        addObject (new platform(), 725, 345);
        addObject (new platform(), 775, 345);
        addObject (new platform(), 825, 345);
        addObject (new platform(), 875, 345);
        addObject (new platform(), 925, 345);
        addObject (new platform(), 975, 345);
        addObject (new platform(), 1025, 345);
        addObject (new platform(), 1075, 345);
        addObject (new platform(), 1125, 345);
        addObject (new platform(), 1175, 345);
        addObject (new platform(), 1225, 345);
        addObject (new platform(), 1240, 345);

        //HooiBlokken
        addObject (new platform(), 1080, 285);
        addObject (new platform(), 1130, 285);
        addObject (new platform(), 1180, 285);
        addObject (new platform(), 1230, 285);

        addObject (new platform(), 1130, 228);
        addObject (new platform(), 1180, 228);

        //Blok rechts
        addObject (new platform(), 1340, 285);
        addObject (new platform(), 1390, 285);
        addObject (new platform(), 1440, 285);
        addObject (new platform(), 1490, 285);
        addObject (new platform(), 1508, 285);

        //TOP
        addObject (new platform(), 450, 170);
        addObject (new platform(), 500, 170);
        addObject (new platform(), 550, 170);
        addObject (new platform(), 600, 170);
        addObject (new platform(), 650, 170);
        addObject (new platform(), 700, 170);
        addObject (new platform(), 750, 170);
        addObject (new platform(), 800, 170);
        addObject (new platform(), 850, 170);
        addObject (new platform(), 900, 170);
        addObject (new platform(), 950, 170);
        addObject (new platform(), 1000, 170);
        addObject (new platform(), 1050, 170);
        addObject (new platform(), 1100, 170);
        addObject (new platform(), 1150, 170);
        addObject (new platform(), 1200, 170);
        addObject (new platform(), 1240, 170);

        //LinksOnder
        addObject (new platform(), 0, 642);
        addObject (new platform(), 50, 642);
        addObject (new platform(), 100, 642);
        addObject (new platform(), 150, 642);
        addObject (new platform(), 200, 642);
        addObject (new platform(), 250, 642);
        addObject (new platform(), 300, 642);
        addObject (new platform(), 350, 642);

        //Links Trap
        addObject (new platform(), 400, 702);
        addObject (new platform(), 440, 702);

        //Bodem
        addObject (new platform(), 490, 760);
        addObject (new platform(), 540, 760);
        addObject (new platform(), 590, 760);
        addObject (new platform(), 640, 760);
        addObject (new platform(), 690, 760);
        addObject (new platform(), 740, 760);
        addObject (new platform(), 790, 760);
        addObject (new platform(), 840, 760);
        addObject (new platform(), 890, 760);
        addObject (new platform(), 940, 760);
        addObject (new platform(), 990, 760);
        addObject (new platform(), 1040, 760);
        addObject (new platform(), 1090, 760);
        addObject (new platform(), 1140, 760);
        addObject (new platform(), 1190, 760);
        addObject (new platform(), 1240, 760);
        addObject (new platform(), 1290, 760);
        addObject (new platform(), 1340, 760);
        addObject (new platform(), 1380, 760);

        //RechterBlok
        addObject (new platform(), 1430, 700);
        addObject (new platform(), 1480, 700);
        addObject (new platform(), 1530, 700);
        addObject (new platform(), 1580, 700);
        //GUI
        LevelCounter levelcounter = new LevelCounter();
        addObject(levelcounter, 136, 157);
        
        
        
        MesoCounter mesocounter = new MesoCounter();
        addObject(mesocounter, 136, 206);
        
        LifeBar lifebar = new LifeBar();
        addObject(lifebar, 136, 250);
        
        addCameraFollower(new Rogue(levelcounter, mesocounter, lifebar), 0, 0);
        //SPAWN CHARACTER
        //addObject (new Rogue(levelcounter, mesocounter, lifebar), 205, 420);

        //MUSIC
        //trainingGround.playLoop();
        RibbonPig ribbonpig2 = new RibbonPig();
        addObject(ribbonpig2, 1502, 670);
        RibbonPig ribbonpig3 = new RibbonPig();
        addObject(ribbonpig3, 847, 723);
        RibbonPig ribbonpig4 = new RibbonPig();
        addObject(ribbonpig4, 1211, 542);
        RibbonPig ribbonpig5 = new RibbonPig();
        addObject(ribbonpig5, 1121, 306);
        ribbonpig.setLocation(574, 305);
        ribbonpig.setLocation(553, 312);
        ribbonpig5.setLocation(1112, 309);
        RibbonPig ribbonpig6 = new RibbonPig();
        addObject(ribbonpig6, 624, 485);
        ribbonpig6.setLocation(615, 491);
        RibbonPig ribbonpig7 = new RibbonPig();
        addObject(ribbonpig7, 1301, 718);
        ribbonpig7.setLocation(1307, 721);
        ribbonpig3.setLocation(672, 712);
        RibbonPig ribbonpig8 = new RibbonPig();
        addObject(ribbonpig8, 911, 542);

        Liane liane = new Liane(10, 150);
        addObject(liane, 238, 515);

        Liane liane2 = new Liane(10, 150);
        addObject(liane2, 336, 205);
        liane2.setLocation(500, 229);
        Liane liane3 = new Liane(10, 150);
        addObject(liane3, 308, 185);
        liane3.setLocation(376, 399);
        Liane liane4 = new Liane(10, 150);
        addObject(liane4, 796, 543);
        liane4.setLocation(848, 637);
        Liane liane5 = new Liane(10, 150);
        addObject(liane5, 1080, 466);
        liane5.setLocation(1330, 638);
        Liane liane6 = new Liane(10, 150);
        addObject(liane6, 1438, 388);
        liane6.setLocation(1524, 580);
        Liane liane7 = new Liane(10, 250);
        addObject(liane7, 1290, 257);
        liane7.setLocation(1284, 357);
        Liane liane8 = new Liane(10, 300);
        addObject(liane8, 1510, 197);
        liane8.setLocation(1360, 413);
        removeObject(liane7);
    }


    public void act()
    {
        respawn(spawnTimer);
        spawnTimer--;
        spawnTimeCheck();
    }

    public void spawnTimeCheck()
    {
        if (spawnTimer <= 0)
        {
            spawnTimer = spawnTimer + 1500;
        }
    }

    public void setXValueRogue(int xValueRogue){
        this.xValueRogue = xValueRogue;
    }

    public int getXValueRogue(){
        return xValueRogue;
    }

    public void setYValueRogue(int yValueRogue){
        this.yValueRogue = yValueRogue;
    }

    public int getYValueRogue(){
        return yValueRogue;
    }

    public void respawn(int spawnTimer)
    {
        if(spawnTimer == 1)
        {
            RibbonPig ribbonpig2 = new RibbonPig();
            addObject(ribbonpig2, 1502, 670);
            RibbonPig ribbonpig3 = new RibbonPig();
            addObject(ribbonpig3, 847, 723);
            RibbonPig ribbonpig4 = new RibbonPig();
            addObject(ribbonpig4, 1211, 542);
            RibbonPig ribbonpig5 = new RibbonPig();
            addObject(ribbonpig5, 1121, 306);
            ribbonpig5.setLocation(1112, 309);
            RibbonPig ribbonpig6 = new RibbonPig();
            addObject(ribbonpig6, 624, 485);
            ribbonpig6.setLocation(615, 491);
            RibbonPig ribbonpig7 = new RibbonPig();
            addObject(ribbonpig7, 1301, 718);
            ribbonpig7.setLocation(1307, 721);
            ribbonpig3.setLocation(672, 712);
            RibbonPig ribbonpig8 = new RibbonPig();
            addObject(ribbonpig8, 911, 542);
        }
    }

}
And last but not least.. My Platform Class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class platform here.
 * 
 * @author (Patrick van kralingen) 
 * @version (14 OKT 2013)
 */
public class platform extends Mover
{
    /**
     * Act - do whatever the platform wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    }   
    

}
danpost danpost

2013/10/24

#
You need to have all classes that create scrolling actors to subclass the ScrollActor class.
Tavi Tavi

2013/10/24

#
Ok i've done that :) that works a little better right now.... However, my background ( which worked fine before trying to implement the ScrollWorld ) is still jumping around and.. especially when i hit the corner of the map.. or the area i can or cannot go ( still stops mid air) Or sometimes i fall trough the floor at this point :( it doesnt look good What's going on here :( ? EDIT : If there is ANYTHING you need to help me out, just say so.. i really need to fix this !
Gevater_Tod4711 Gevater_Tod4711

2013/10/25

#
You probably want the worlds background to move the same distance as your world objects right? But how is the world moving currently? On the photos it seems like the background is the only thing that is moving and not the platforms. I unfortunately have never workes with this scrolling engine but after having a look on the code I think the problem could be that all the objects you add to the world are cameFollowers for the scrolling world. probably you are always using this addObject method: public void addCameraFollower(ScrollActor cameraFollower, int x, int y) instead of this method: public void addObject(Actor object, int x, int y) what makes all your actors camaraFollowers. I think that would explain why they are not moving. If every actor is a camaraFollower the scrolling world tries to follow every actor and does not scroll. To fix this you could try casting your objects to Actors before adding them to the world (addObject((Actor) new ScrollActor(), ...); But this is just a suggestion because I don't realy know how this engine works. So I'm not shure if this will fix the problem. You'll have to try.
Tavi Tavi

2013/10/25

#
My biggest issue at this point is that my background is getting kinky as as soon i move.. when i start the game, it's all fine bus as soon as i touch a key, the whole background "misplaces" itself and it looks like im walking underground ( you can check out my last 3 screenshots ).. All the mobs work fine ! I had that problem before and then i subclassed all "movers" underneath ScrollActor, :) That's working fine !
Random Question, but is the background as big as the whole map? I'm not exactly sure what's going on, but that may be a problem if its not as big as your world size (not the screen size).
SPower SPower

2013/10/25

#
By the way, is it just the background which is moving 'weird', or are the other scrollactors doing the same thing?
Tavi Tavi

2013/10/25

#
I've made a short video guys :) i thought that would be best !! When i start the game, you can see the platforms match my background , but when i move... the background "skips" and all my game is messed up .. I Can also fall out of my screen :( If you guy's like the video to explain problems, i'd be glad to use it more often :) Thanks in advance :)
There are more replies on the next page.
1
2
3
4