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

2014/3/24

Scrolling glitch that I can't seem to figure out.

Entity1037 Entity1037

2014/3/24

#
In my Megaman EX scenario, if I respawn to a checkpoint that's positioned so that if you centered the screen on it, a scrolling stopper goes on screen, the screen spazzes uncontrollably due to it trying to center the player, and then immediately move the screen because of the scrolling stopper, then repositioning due to the player ect.. The main issue, though, is not knowing why it does this. It shouldn't move the screen back to reposition the player because the scrolling stopper is in the way, but it does anyway even though it works just fine when the screen is just normally scrolling into a scroll stopper. And also, You can even just have a scrolling stopper just off the screen but not on the screen when respawning, and it will do this. Does anyone have any ideas as to why this happens? I'll post the code.
public void act(){
        scrollingEngine();
        setPaintOrder(Fps.class,MouseButtons.class,GridCoordinates.class,Score.class,FadeOverlay.class,healthPoint.class,HealthMeter.class,XExplosion.class,X.class,BossDoor.class,Enemies.class,xbullets.class,shot3.class,shot2.class,shot1.class,Terrain.class);
        if (Greenfoot.isKeyDown("l"))loadStage();
    }
    
    boolean autoScroll=true;
    public void autoScroll(boolean a){autoScroll=a;}
    int xc=getWidth()/2;//X center of screen
    int yc=getHeight()/2;//Y center of screen
    int xX=xc;//X's X coordinate
    int xY=yc;//X's Y coordinate
    public void getCoordinates(int x, int y){xX=x; xY=y;}
    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
    int llimit=500;
    int rlimit=500+llimit;
    int ulimit=200;
    int dlimit=200+ulimit;
    int xst=llimit;//How far the screen's x coordinate is away from start
    int yst=ulimit;//How far the screen's y coordinate is away from start
    public void setScrollingLimits(int left, int right, int up, int down){scrollToBeginning(); xst=left; yst=up; rlimit=right+left; llimit=left; ulimit=up; dlimit=down+up;}
    
    public void scrollingEngine(){scrollingEngine(0,0);}
    /**If you want to scroll the screen in a specific area, then call scrollingEngine(xcorrect,ycorrect), and this will cause the screen to scroll
       accordingingly and ignore all scrollstoppers while doing so.*/
    public void scrollingEngine(int x,int y){
        xcorrect=-x;
        ycorrect=-y;
        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;}
        }
        //Test if the scrolling boundries have been reached, and stop scrolling if they have
        List scrollstoppers = getObjects(ScrollStopper.class);
        if (xst+-xcorrect>rlimit)xcorrect=xst-rlimit;
        if (xst+-xcorrect<0)xcorrect=xst;
        if (yst+-ycorrect>dlimit)ycorrect=yst-dlimit;
        if (yst+-ycorrect<0)ycorrect=yst;
        if (!scrollstoppers.isEmpty()&&x==0&&y==0&&autoScroll==true){
            for (int s=0; s<scrollstoppers.size(); s++){
                Actor st = (Actor) scrollstoppers.get(s);
                if (st.getX()+25+xcorrect>=0&&st.getX()-25+xcorrect<=getWidth()&&st.getY()+25+ycorrect>=0&&st.getY()-25+ycorrect<=getHeight()){
                    if (st.getY()+25>0&&st.getY()-25<getHeight()){
                        if (st.getX()>getWidth()/2)xcorrect=-(st.getX()-25-getWidth());
                        if (st.getX()<getWidth()/2)xcorrect=-(st.getX()+25);
                    }
                    if (st.getX()+25>0&&st.getX()-25<getWidth()){
                        if (st.getY()>getHeight()/2)ycorrect=-(st.getY()-25-getHeight());
                        if (st.getY()<getHeight()/2)ycorrect=-(st.getY()+25);
                    }
                }
            }
        }
        //Track the screen's scrolling
        xst-=xcorrect; //x-axis tracking
        yst-=ycorrect; //y-axis tracking
        //Scroll world, if at all
        ((GridCoordinates)getObjects(GridCoordinates.class).get(0)).giveGridCoordinates(xcorrect,ycorrect);
        if (xcorrect==0&&ycorrect==0)return;
        scroll();
    }
    Class[] world = {SpriteSheet.class,Terrain.class};
    public void scroll(){
        //Scroll World
        for (int i=0; i<world.length; i++){
            List objects = getObjects(world[i]);
            if (! objects.isEmpty()){
                for (int o=0; o<objects.size(); o++){
                    Actor object = (Actor) objects.get(o);
                    object.setLocation(object.getX()+xcorrect,object.getY()+ycorrect);
                }
            }
        }
        //Update checkpoint coordinates
        if (checkpoints[0]==null)return;
        for (int i=0; i<=ccp; i+=2){
            if (checkpoints[i]==null){System.out.println("REACHED NULL BEFORE PASSING CCP"); return;}
            checkpoints[i]+=xcorrect;
            checkpoints[i+1]+=ycorrect;
        }
    }
    public void scrollToBeginning(){
        autoScroll=false;
        xcorrect=xst-llimit;
        ycorrect=yst-ulimit;
        xst=llimit;
        yst=ulimit;
        scroll();
        autoScroll=true;
    }


//Respawn coding


Integer[] checkpoints = new Integer[100];
    static int ccp=0;
    public void giveCheckpointCoordinates(int x, int y){
        int i=0;
        for (i=i; i<checkpoints.length; i+=2){
            if (checkpoints[i]==null)break;
            if (x==checkpoints[i])return;
            if (y==checkpoints[i+1])return;
        }
        ccp=i;
        checkpoints[ccp]=x;
        checkpoints[ccp+1]=y;
    }
    
    public void eraseCheckpoints(){
        for (int i=0; i<checkpoints.length; i++){
            if (checkpoints[i]!=null){
                checkpoints[i]=null;
            }else{
                return;
            }
        }
    }
    
    public void respawn(){
        try{
            deleteCurrentLevel();
            loadStage();
            if (checkpoints[0]!=null){
                scrollingEngine(checkpoints[ccp],checkpoints[ccp+1]);
                int rx=checkpoints[ccp];
                int ry=checkpoints[ccp+1];
                addObject(new X("XEngine"),rx,ry);
                scrollingEngine(rx-xc+1,ry-yc+1);
            }else{
                scrollToBeginning();
                addObject(new X("XEngine"),xc,yc);
            }
            try{((FadeOverlay)getObjects(FadeOverlay.class).get(0)).fadeIn();}catch(IndexOutOfBoundsException e){}
            /**
            Record x and y coordinates for checkpoints of where it is on screen everytime x touches the checkpoint, and track it's positions by
            adding xcorrect and ycorrect from the scrolling engine to them. Then, when the player dies, delete/reload the level, then scroll to
            the checkpoint, and get it's exact location (have to use any leftover value from the x and y coordinates in case the screen can't
            scroll all the way so that the checkpoint is in the center of the screen), then teleport Megaman X into the screen based on checkpoint
            coordinates.
            */
        }catch(Exception e){e.printStackTrace();}
    }

This is all in the world subclass "XEngine" by the way.
Entity1037 Entity1037

2014/3/24

#
I should also mention: the screen is blacked out while the glitch starts for the first few frames, so the initial repositioning of the screen is unimportant. The problem is that is tries to scroll so the player is in the center of the screen for no reason.
Entity1037 Entity1037

2014/3/25

#
I fixed the problem myself by re-calculating the screen's position a bunch of times before adding the player. So.... never mind.
Entity1037 Entity1037

2014/3/25

#
Actually the glitch seems to still be present, however it only happens when the scroll stopper is a good ways onto the screen.
You need to login to post a reply.