Hi,
I've been using this project : Click as a basis for creating a game that has a helicopter that flies around saving people. It has a 2D-sidescrolling effect.
For the Y, I made sure the Character does not go below or above the screen. So I check the Y of the Character and make sure it does not go below or above certain values. The problem is that I don't know how to check this with the X. Because the Object is always centered in the middle, it will always be 375. Even though it does actually move about a thousand pixels to the left. For the background it actually did work, but that same check does not on the Character.
The goal of checking X is making sure it does not go beyond the background. If the Character has reached the end of the background I want it to stop scrolling and the Character to move until the border so that you will never see beyond the background.
I was wondering if somebody could help me, thanks in advance.
Here is the code:
World class : achtergrond (Dutch for background):
The background actor (background image is 2000x750):
And finally the code of the Character:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class achtergrond here. * * @author (your name) * @version (a version number or a date) */ public class achtergrond extends World { /** * Constructor for objects of class achtergrond. * */ public achtergrond() { super(750, 750, 1, false); populate(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ public void act() { uman um=(uman) getObjects(uman.class).get(0); //GETS THE ACTOR int dx = um.getX()-getWidth()/2; // CALCULATES THE MIDDLE for(Object obj : getObjects(null)) // NO IDEA { Actor actor=(Actor)obj; // NO IDEA actor.setLocation(actor.getX()-dx, actor.getY()); // KEEPS SETTING THE ACTOR IN THE MIDDLE } } public void populate() //CREATES BACKGROUND AND NEW ACTOR { background background = new background(); addObject(background, 600, 375); uman uman = new uman(); addObject(uman, 200, 335); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class background here. * * @author (your name) * @version (a version number or a date) */ public class background extends Actor { private int speed = 6; //SETS SPEED public void act() { checkKeys(); } private void checkKeys() //MOVES VIA KEYBOARD INPUT { int checkX = getX(); System.out.println(checkX); if(checkX >= -240){ if (Greenfoot.isKeyDown("right")) { setLocation(getX()-speed,getY()); } } if(checkX <= 985){ if(Greenfoot.isKeyDown("left")) { setLocation(getX()+speed,getY()); } } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class uman here. * * @author (your name) * @version (a version number or a date) */ public class uman extends Actor { private int speed = 6; //SETS SPEED public void act() { imageChanger(); checkKeys(); } private void imageChanger() { if (Greenfoot.isKeyDown("left")) { setImage("Player_groot_links.png"); } if (Greenfoot.isKeyDown("right")) { setImage("Player_groot_rechts.png"); } } private void checkKeys() // MOVES VIA KEYBOARD INPUT { int checkY = getY(); if (Greenfoot.isKeyDown("left")){ setLocation ( getX() - speed, getY() ); setImage("Player_groot_links.png"); } if (Greenfoot.isKeyDown("right")){ setLocation ( getX() + speed, getY() ); setImage("Player_groot_rechts.png"); } if(checkY >= 45){ if(Greenfoot.isKeyDown("up")){ setLocation ( getX(), getY() - speed ); } } if(checkY <= 710){ if( Greenfoot.isKeyDown("down")){ setLocation (getX(), getY() + speed ); } } } }