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

2013/10/19

Background crashing

ddvink ddvink

2013/10/19

#
Here's a bunch of code for my background. I tried to move it in relation to a (temp) fish class... Can someone tell me why I have this strnge result?
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 World
{

    // variables
    private GreenfootImage bgImage;

    // Breedte en hoogte van de background-image
    private int imageHeight, imageWidth;

    // breedte en hoogte van het canvas
    private int canvasHeight, canvasWidth;

    // Startpositie van het image
    private int xStartImage, yStartImage;

    // x en y-positie van de background-image
    private int xPosImage, yPosImage;

    // x and y path over which the background image moves
    private int xPathImage, yPathImage;
    private double xPathFish, yPathFish;

    // Snelheid waarmee background beweegt.
    private double xSpeedImage, ySpeedImage;

    // Temp vars

    private Fish frenzy;
    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1); // Catch the width and height of the canvas with getWidth() and getheight().

        frenzy = new  Fish(5);

        bgImage = getBackground();

        // Breedte en hoogte van de background-image
        imageHeight = bgImage.getHeight();
        imageWidth = bgImage.getWidth();

        // breedte en hoogte van het canvas
        canvasHeight = getHeight();
        canvasWidth = getWidth();

        // Starting position of image
        xPosImage = 0;
        yPosImage = canvasHeight - imageHeight;

        addObject(frenzy, getWidth()/2, getHeight()/2);
    }

    public void act(){
        moveWithSpriteAnimation();   
    }

    public void moveWithSpriteAnimation(){

        if (Greenfoot.isKeyDown("right")){
            moveBackgroundToSprite();
        }
        if(Greenfoot.isKeyDown("left")){
            moveBackgroundToSprite();
        }
        if(Greenfoot.isKeyDown("up")){
            moveBackgroundToSprite();
        }
        if(Greenfoot.isKeyDown("down")){
            moveBackgroundToSprite();
        }

    }

    public void moveBackgroundToSprite(){
        // Movement speed over X        
        xPathFish = ((double)3/4 - (double)1/4)*canvasWidth;
        xPathImage = canvasWidth - imageWidth;

        xSpeedImage = xPathImage / xPathFish;

        if(frenzy.getX() > ((double)1/4)*getWidth() && frenzy.getX() < ((double)3/4)*getWidth()){
            int newX = frenzy.getX()*(int)xSpeedImage;
            int thisY = frenzy.getY();
            getBackground().drawImage(bgImage, -newX, thisY);
        } else {
            // getBackground().drawImage(bgImage, 0,0 );
        }

        // Movement speed of Y
        yPathFish = ((double)3/4 - (double)1/4)*canvasHeight;
        yPathImage = canvasHeight - imageHeight;

        ySpeedImage = yPathImage / yPathFish;

        if(frenzy.getY() > ((double)1/4)*getHeight() && frenzy.getY() < ((double)3/4)*getHeight()){
            int thisX = frenzy.getX();
            int newY = frenzy.getY()*(int)ySpeedImage;
            getBackground().drawImage(bgImage, thisX, newY);
        } else {
            System.out.println("Vis is buiten");            
        }
    }
}   
And here's the code for my fish (nothing really special)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Fish here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fish extends Actor
{
    private int speed;
    private int xValueFish;
    private int yValueFish;
    // The empty fish constructor
    public Fish(){

    }

    // The empty fish constructor
    public Fish(int speed){
        this.speed = speed;

    }

    public void act() 
    {
        moveTheFish();
    }   

    public void moveTheFish(){
        if(Greenfoot.isKeyDown("left")){
            move(-speed);
        }

        if(Greenfoot.isKeyDown("right")){
            move(speed);
        }

        if(Greenfoot.isKeyDown("up")){
            int up = getY();
            up = up - speed;
            setLocation(getX(), up);
       }

        if(Greenfoot.isKeyDown("down")){
            int down = getY();
            down = down + speed;
            setLocation(getX(), down);
        }
    }

    public void setSpeed(int speed){
        this.speed = speed;
    }

    public int getSpeed(){
        return speed;
    }
}
danpost danpost

2013/10/19

#
If you are getting an error message of any kind, you need to copy/paste the entire message here.
danpost danpost

2013/10/19

#
I think what you are experiencing is related to line 48, which saves the (visual) image of the background (it does not save the image from the file that was used to create the background). Replace that line with:
bgImage = new GreenfootImage(/* image filename */);
ddvink ddvink

2013/10/19

#
You're right about the filename, but I still have the same problem. I can move the fish with my arrow, and the background should be moving as well..... but it seems that doesn't refresh correctly or something.... it's giving very strange results.
danpost danpost

2013/10/19

#
I know well what results you are getting. The coding for scrolling is very 'touchy' and must be coded with exactness. Relying on the keys that are down to run the scrolling does not keep the scrolling in sync with the location of the player (if the player is at an edge or you restrict the movement of the player while a key is down, but still try to scroll...well...). Best is to scroll by relying on the position of the sprite. I came up with the following:
public void moveBackgroundToSprite()
{
    int scrX = 0, scrY = 0; // to hold current scroll changes
    // check standard horizontal limits of sprite
    if (frenzy.getX() < canvasWidth/4) scrX = canvasWidth/4-frenzy.getX();
    if (frenzy.getX() > canvasWidth*3/4) scrX = canvasWidth*3/4-frenzy.getX();
    // check horizontal limits of image
    if (scrX+xPosImage > 0) scrX = -xPosImage;
    if (scrX+xPosImage < canvasWidth-imageWidth) scrX =(canvasWidth-imageWidth)-xPosImage;
    // check standard vertical limits of sprite
    if (frenzy.getY() < canvasHeight/4) scrY = canvasHeight/4-frenzy.getY();
    if (frenzy.getY() > canvasHeight*3/4) scrY = canvasHeight*3/4-frenzy.getY();
    // check vertical limits of image
    if (scrY+yPosImage > 0) scrY = -yPosImage;
    if (scrY+yPosImage < canvasHeight-imageHeight) scrY = (canvasHeight-imageHeight)-yPosImage;
    // adjust scroll values and scroll 
    xPosImage += scrX;
    yPosImage += scrY;
    getBackground().drawImage(bgImage, xPosImage, yPosImage);
    frenzy.setLocation(frenzy.getX()+scrX, frenzy.getY()+scrY);
}
This should be called once in the constructor after the sprite is entered into the world and directly from the act method (remove the 'moveWithSpriteAnimation' method and its call from the act method).
You need to login to post a reply.