Hello! I'm quite new with coding, and I'm making a platformer game wherein a bear is jumping on some icecaps. My problem is that I can't seem to make my bear stay on the platform and move along with it. Here is the code of my world, bear, and icecaps. How will this be resolved?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Game here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Game extends World
{
Counter counter = new Counter();
/**
* Constructor for objects of class Game.
*
*/
public Game()
{
// Create a new world with 600x450 cells with a cell size of 1x1 pixels.
super(600, 450, 1);
prepare();
addObject(counter, 560, 40);
}
public Counter getCounter()
{
return counter;
}
public void act()
{
if(Greenfoot.isKeyDown("P"))
{
addObject(new Pause(), 300, 225);
}
if(Greenfoot.getRandomNumber(100) < 2)
{
addObject(new Ice(), 599, Greenfoot.getRandomNumber(270)+250);
}
}
/**
* Prepare the world for the start of the program.
* That is: create the in/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public int sampleMethod(int y)
{
return y;
}
private void prepare()
{
Counter counter = new Counter();
addObject(counter,515,51);
counter.setLocation(540,26);
counter.setLocation(529,30);
counter.setLocation(530,30);
Ice ice = new Ice();
addObject(ice,71,364);
Bailey bailey = new Bailey();
addObject(bailey,87,320);
bailey.setLocation(80,315);
counter.setLocation(578,26);
counter.setLocation(570,30);
Coin coin = new Coin();
addObject(coin,210,230);
Coin coin2 = new Coin();
addObject(coin2,320,150);
Coin coin3 = new Coin();
addObject(coin3,460,92);
Coin coin4 = new Coin();
addObject(coin4,415,290);
removeObject(coin);
removeObject(coin4);
removeObject(coin2);
removeObject(coin3);
Coin coin5 = new Coin();
addObject(coin5,201,252);
Coin coin6 = new Coin();
addObject(coin6,337,201);
Ice ice2 = new Ice();
addObject(ice2,319,340);
ice2.setLocation(297,332);
Ice ice3 = new Ice();
addObject(ice3,510,390);
coin5.setLocation(306,287);
coin6.setLocation(521,349);
Bes bes = new Bes();
addObject(bes,79,56);
bes.setLocation(71,40);
removeObject(counter);
removeObject(bes);
}
} import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bailey here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bailey extends Actor
{
int dy = 0;
int g = 2;
public boolean safe;
/**
* Act - do whatever the Bailey wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
lose();
control();
setLocation(getX(), getY() + dy);
if(!isTouching(Ice.class))
{
dy = dy + g;
}
}
public void control()
{
if (Greenfoot.isKeyDown("up")&&isTouching(Ice.class))
{
dy = -15;
}
if (Greenfoot.isKeyDown("right"))
{
move(10);
}
if (Greenfoot.isKeyDown("left"))
{
move(-10);
}
}
public void lose()
{
if (getY() == 449)
{
Greenfoot.setWorld(new Lose());
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Ice here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ice extends Actor
{
/**
* Act - do whatever the Ice wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX(), getY());
move(-2);
if (getX() == 0)
{
getWorld().removeObject(this);
}
}
}
