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

2016/4/26

Need help with scrolling world

jmsmall jmsmall

2016/4/26

#
Okay so I have a fully working scrolling world, when my actor reaches the edge the world scrolls with him. The thing i'm having trouble figuring out is how the enemies can move freely throughout the world without them getting stuck on the wall, or teleporting when I reach the edge. Also I don't want the enemies to scroll the world, I just want them to be able to move past it. Here is my enemies code.
import greenfoot.*;

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{   
    int mapX;
    int mapY;

    public void addedToWorld(World theWorld)
    {
        World myWorld = (World)getWorld();
        myWorld = theWorld;

    }
    public void act()
    {
        move(5);
    }
    public Enemy(int getMapX, int getMapY)
    {
        mapX = getMapX;
        mapY = getMapY;
    }

}



Here is my Scrolling code inside of the actor that can currently scroll.
public void setLocationWithScroll(int newX, int newY)
    {
        Background myBackground = (Background)getWorld();
        if((newY<200&&myBackground.topBound>0) || (newY>worldHeight-200&&myBackground.bottomBound<myBackground.MAPHEIGHT))
        {
            int yShift = newY - getY();
            
        
        }
        else
        {
            setLocation(getX(), newY);
        }
        if((newX < 200 && myBackground.leftBound>0) || (newX>worldWidth-200 && myBackground.rightBound < myBackground.MAPWIDTH))
        {
            int xShift = newX - getX();
            myBackground.shiftScreen(xShift, 0);
        }
        else
        {
            setLocation(newX, getY());
        }
    }
If you need to see my world code which is where a lot of the scrolling code takes place, let me know and i'll provide it. Any and all help will be appreciated.
danpost danpost

2016/4/26

#
Please do not create more than one discussion thread on the same issue. Please do not reply to this thread. Discussion was continued on a different thread.
You need to login to post a reply.