I've been watching Mr Stewart's lesson on how to make a scrolling platform game, and I've used all his variables and codes. However, when writing the code for the lesson on 'Falling', nothing happened with my character. It didn't move at all.
I have limited time to finish my game and need help as fast as possible. I am aware that the source I'm using might not be relevant anymore since Greenfoot has been updated since 2014, but everything else seemed to work fine (all the other codes for the other videos published by Jim Stewart on Youtube) so I don't get what the problem is.
Here's the code I've added to my Actor (side note: I went on to the next lesson - Greenfoot Scrolling Platform World 5: Landing on platforms and added the codes from there to see if that would help my character falling, but there was no use):
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Avatar here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Avatar extends Actor
{
boolean inTheAir = false;
int groundHeight = getImage().getHeight()/2;
int deltaX = 0;
int deltaY = 0;
/**
* Act - do whatever the Avatar wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (inTheAir)
{
fall();
}else
{
if(Greenfoot.isKeyDown("left"))
{
deltaX = -5;
}else if(Greenfoot.isKeyDown("right"))
{
deltaX = +5;
}else
{
deltaX = 0;
}
}
move();
}
public void move()
{
int newX = getX() + deltaX;
int newY = getY() - deltaY;
Actor platformBelow = getOneObjectAtOffset(0, groundHeight + 5, Platform.class);
if(platformBelow!=null)
{
if(deltaY<0)
{
deltaY = 0;
inTheAir = false;
GreenfootImage platformImage = platformBelow.getImage();
int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2;
newY = topOfPlatform - groundHeight;
}
} else
{
inTheAir = true;
}
setLocationWithScroll(newX, newY);
}
public void fall()
{
deltaY--;
}
public void setLocationWithScroll(int newX, int newY)
{
Background myBackground = (Background)getWorld();
int shiftX = newY - getY();
int shiftY = newX - getX();
myBackground.shiftScreen(shiftX, -shiftY);
}
}
And here is the code for my background:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
/**
* Write a description of class Background here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Background extends World
{
PlatformMap map = new PlatformMap();
GreenfootImage mapImg = map.getImage();
final int MAPIMGWIDTH = mapImg.getWidth();
final int MAPIMGHEIGHT = mapImg.getHeight();
Platform platformTemplate = new Platform(0,0);
GreenfootImage pfImg = platformTemplate.getImage();
/*
* This conditional will compare the map X and Y coordinate to the left, right, top and
* bottom bounds of the screen to see if it should be on the map.
*/
if(thisPlatformX>=leftBound && thisPlatformX<=rightBound && thisPlatformY >=topBound && thisPlatformY <= bottomBound)
{
screenX = thisPlatformX - leftBound;
screenY = thisPlatformY - topBound;
if(thisPlatform.getWorld()==null)
{
addObject(thisPlatform, screenX, screenY);
} else {
thisPlatform.setLocation(screenX, screenY);
}
}else {
if(thisPlatform.getWorld()!=null)
removeObject(thisPlatform);
}
}
}
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
}
}
