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

2014/6/10

Have a question with sidescrolling

n1nikko n1nikko

2014/6/10

#
Whenever the game scrolls to either side, the sprites on my character does not show, and it looks like it is gliding across the screen. Please help me
danpost danpost

2014/6/11

#
You need to show what code you are using or we cannot help. It would be best to show the entire class that runs the scrolling.
n1nikko n1nikko

2014/6/11

#
danpost wrote...
You need to show what code you are using or we cannot help. It would be best to show the entire class that runs the scrolling.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Erm extends Actor { public boolean shouldScroll; //A boolean to determine if scrolling is needed, according to how far left/right across the screen the player is. public int playerX; //The player's x value public void act() { updateShouldScroll(100, 500); //This should be in the act method so that the shouldScroll boolean is constantly updated. } /** * This method checks the players x position and sets the shouldScroll boolean accordingly. * @param The x value limits within which scrolling is not required for left/right movement. */ public void updateShouldScroll(int minX, int maxX) { ScrollingWorld theWorld = (ScrollingWorld) getWorld(); Player player = (Player) theWorld.getPlayer(); playerX = player.getX(); if(playerX <= minX || playerX >= maxX) { shouldScroll = true; } else { shouldScroll = false; } if(shouldScroll == true && (playerX >= 400 && Greenfoot.isKeyDown("left")) || (playerX <= 200 && Greenfoot.isKeyDown("right"))) { shouldScroll = false; } } }
danpost danpost

2014/6/11

#
No. The scrolling is something that should be done in your subclass of world (your ScrollingWorld class) -- even determining if scrolling should be done. The player should be programmed as normal (without respect to scrolling at all). Your code:
import greenfoot.*;

public class Erm extends Actor
{
    public boolean shouldScroll; // do not need here (or anywhere)
    public int playerX; // un-necessary (getX() can be used for this)
    
    // this should be done in ScrollingWorld class
    // moving of the actor should be here
    public void act() 
    {
        updateShouldScroll(100, 500);
    }
    
    // (cont'd) should be done in ScrollingWorld class
    public void updateShouldScroll(int minX, int maxX)
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Player player = (Player) theWorld.getPlayer();
        playerX = player.getX();
        if(playerX <= minX || playerX >= maxX)
        {
            shouldScroll = true;
        }
        else
        {
            shouldScroll = false;
        }
        if(shouldScroll == true && (playerX >= 400 && Greenfoot.isKeyDown("left")) || (playerX <= 200 && Greenfoot.isKeyDown("right")))
        {
            shouldScroll = false;
        }
    }
}
From what I see here, your Erm class could be as simple as this:
import greenfoot.*;

public class Erm extends greenfoot.Actor
{
    public void act()
    {
        if (Greenfoot.isKeyDown("right")) move(/* speed */);
        if (Greenfoot.isKeyDown("left")) move(-/* speed */);
    }
}
That would be the entire class! (from what you have so far) Your scroll method in the world subclass should be something like:
private void scroll()
{
    if (getObjects(Erm.class).isEmpty()) return;
    Actor player = (Actor)getObjects(Erm.class).get(0);
    int dx = 0;
    if (player.getX() < 100) dx = 100-player.getX();
    if (player.getX() > 500) dx = 500-player.getX();
    if (dx == 0) return;
    // scroll background by 'dx'
    // move actors by 'dx'
}
You need to login to post a reply.