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

2015/1/15

Help with sprites and sidescrolling

n1nikko n1nikko

2015/1/15

#
I want my sprites to move along when i side scroll, but I dont know how to do it. when my character reaches the border to start scrolling, he freezes it place and move along with the screen. Code import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Superclass of all actors. * * @author Bertie Wheen * @version 0.1 */ 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 Erm(){ } public void act() { updateShouldScroll(100, 900); //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) { Background theWorld = (Background) getWorld(); Player player = (Player) theWorld.getPlayer(); playerX = player.getX(); if(playerX <= minX || playerX >= maxX) { shouldScroll = true; } else { shouldScroll = false; } if(shouldScroll == true && (playerX >= 900 && Greenfoot.isKeyDown("a")) || (playerX <= 100 && Greenfoot.isKeyDown("d"))) { shouldScroll = false; } } }
You need to login to post a reply.