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

2016/4/26

Danpost Plz

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

#
If you want actors to be able to go beyond the edge of the world, then you need an unbounded world. There is a constructor of the World class that can be used to make an unbounded world. See the documentation here.
jmsmall jmsmall

2016/4/28

#
So I made my world unbounded and my enemies move past the edges of the world. But when my actor reaches the edge in order to make the world scroll, they teleport back to where they originally spawned. FYI they're spawned in by a drawn map that I made in Photoshop. I learned this from Mr. Stewarts scrolling platform lessons. Here's the code that spawns the enemies into the world
public void update2()
    {
        Enemy thisEnemy;
        int thisEnemyX;
        int thisEnemyY;
        int screenX;
        int screenY;
        for(int i=0; i<theEnemies.size(); i++)
        {
            thisEnemy = theEnemies.get(i);
            thisEnemyX = thisEnemy.mapX;
            thisEnemyY = thisEnemy.mapY;
            if(thisEnemyX+ENEMYWIDTH>=leftBound && thisEnemyX-ENEMYWIDTH<=rightBound && thisEnemyY >=topBound && thisEnemyY <= bottomBound)
            {
                screenX = thisEnemyX - leftBound;
                screenY = thisEnemyY - topBound;
                if(thisEnemy.getWorld()==null)
                {
                    addObject(thisEnemy, screenX, screenY);
                }
                else
                {
                    thisEnemy.setLocation(screenX, screenY);
                }
            }
            else
            {
               if(thisEnemy.getWorld()!=null)
               {
                   removeObject(thisEnemy);
                }
            }
        }
    }
Hopefully you can help.
danpost danpost

2016/4/28

#
jmsmall wrote...
Hopefully you can help.
This is not the code you are having problems with. The code you are having problems with is probably in the class of your scrolling actor or elsewhere in your world class. The Background class has the shift method and some fields that are referred to in the class of your scrolling actor.
You need to login to post a reply.