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

2013/9/14

Scrolling Engine only scrolls first class in the array????

Entity1037 Entity1037

2013/9/14

#
I've create a Scrolling engine which gets the coordinates of the player, and moves the player and the world so it scrolls. However, it doesn't seem to be moving anything but the player, and I have no Idea why. Could someone help me with this? Also, all of the terrain that the player interacts with (the collision physics are perfect, trust me) are subclasses of "Terrain", and do not have any coding.
import greenfoot.*;
import java.util.List;

public class XEngine extends World
{
    public XEngine()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);
        addObject(new Base(250,"ImpossibleBlock.png"),getWidth()/2,getHeight()-50);
        addObject(new X(),getWidth()/2,getHeight()/2);
        addObject(new GridCoordinates(),getWidth()/2,40);
    }
    
    public void act(){
        scrollingEngine();
        fadeOverlayEngine();
    }
    
    boolean autoScroll=true;
    int xX=0;//X's X coordinate
    int xY=0;//X's Y coordinate
    public void getCoordinates(int x, int y){xX=x; xY=y;}
    int xc=getWidth()/2;//X center of screen
    int yc=getHeight()/2;//Y center of screen
    int xcorrect=0;//How much world needs to be scrolled on the X-axis
    int ycorrect=0;//How much world need to be scrolled on the Y-axis
    int s=25;//How far from center X can stray before the screen scrolls
    public void scrollingEngine(){
        xcorrect=0;
        ycorrect=0;
        if (autoScroll==true){
            //Figure out how far away X is from the scrolling margin, if at all
            if (xX<xc-s){xcorrect=xc-s-xX;}else if (xX>xc+s){xcorrect=xc+s-xX;}
            if (xY<yc-s){ycorrect=yc-s-xY;}else if (xY>yc+s){ycorrect=yc+s-xY;}
        }
        //Scroll world, if at all
        ((GridCoordinates)getObjects(GridCoordinates.class).get(0)).giveGridCoordinates(xcorrect,ycorrect);
        if (xcorrect==0&&ycorrect==0)return;
        scroll();
    }
    Class[] world = {Matter.class,Terrain.class,Shadow.class,Enemy.class,XExplosionGenerator.class,XBuster.class};
    public void scroll(){
        for (int i=0; i<world.length; i++){
            List objects = getObjects(world[i]);
            if (! objects.isEmpty()){
                for (int o=0; i<objects.size(); i++){
                    Actor object = (Actor) objects.get(o);
                    object.setLocation(object.getX()+xcorrect,object.getY()+ycorrect);
                }
            }
        }
    }
    
    public void fadeOverlayEngine(){
        
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/9/14

#
I think the problem is the second for loop in your scroll method (line 47). You probably meant:
for (int o=0; o<objects.size(); o++){

}

//instead of:
for (int o=0; i<objects.size(); i++) {

}
Entity1037 Entity1037

2013/9/24

#
Thank you so much! Wow... I feel like an idiot... :P
danpost danpost

2013/9/25

#
Usage of the other 'for' method, the List iterator 'for', could have avoided this:
// starting at line 45
for (Object obj : getObjects(world[i]))
{
    Actor actor = (Actor) obj;
    actor.setLocation(actor.getX()+xcorrect, actor.getY()+ycorrect);
}
// ending at line 51
You need to login to post a reply.