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

2012/12/19

Getting X of centered Object

MrBunni MrBunni

2012/12/19

#
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):
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);
        
        
    }
}
The background actor (background image is 2000x750):
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());
                }
            } 
    }
}
And finally the code of the Character:
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 );
            }
        }
    }
   

  
}
danpost danpost

2012/12/19

#
An example to go by would be
import greenfoot.*;

public class Canvas extends World
{
    Background back=new Background(); // background image object
    Robot robot=new Robot(); // main object
    int rangeX=200, rangeY=150; // internal range of centered object
    // (centerX-rangeX, centerY-rangeY) to (centerX+rangeX, centerY+rangeY)

    public Canvas()
    {    
        super(600, 400, 1, false);
        addObject(back, 300, 200);
        addObject(robot, 300, 200);
    }

    public void act()
    {
        // move all object so that the main object is in main window range
        int dx=0, dy=0;
        if(robot.getX()<getWidth()/2-rangeX) dx=getWidth()/2-rangeX-robot.getX();
        if(robot.getX()>=getWidth()/2+rangeX) dx=getWidth()/2+rangeX-robot.getX();
        if(robot.getY()<getHeight()/2-rangeY) dy=getHeight()/2-rangeY-robot.getY();
        if(robot.getY()>=getHeight()/2+rangeY) dy=getHeight()/2+rangeY-robot.getY();
        for(Object obj : getObjects(null))
        {
            Actor actor=(Actor)obj;
            actor.setLocation(actor.getX()+dx, actor.getY()+dy);
        }
        // move all objects so background covers the window
        dx=0; dy=0;
        if(back.getX()>=back.getImage().getWidth()/2) dx=back.getImage().getWidth()/2-back.getX();
        if(back.getX()<getWidth()-back.getImage().getWidth()/2) dx=getWidth()-back.getImage().getWidth()/2-back.getX();
        if(back.getY()>=back.getImage().getHeight()/2) dy=back.getImage().getHeight()/2-back.getY();
        if(back.getY()<getHeight()-back.getImage().getHeight()/2) dy=getHeight()-back.getImage().getHeight()/2-back.getY();
        for(Object obj : getObjects(null))
        {
            Actor actor=(Actor)obj;
            actor.setLocation(actor.getX()+dx, actor.getY()+dy);
        }
        // move main actor back in window
        if(robot.getX()<0) robot.setLocation(0, robot.getY());
        if(robot.getX()>=getWidth()) robot.setLocation(getWidth()-1, robot.getY());
        if(robot.getY()<0) robot.setLocation(robot.getX(), 0);
        if(robot.getY()>=getHeight()) robot.setLocation(robot.getX(), getHeight()-1);
    }
}
You need to login to post a reply.