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?
And here's the code for my fish (nothing really special)
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");
}
}
}
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;
}
}

