Id much rather the coding is in the enemy class, as it would get too complicated for me if the coding and the methods were inside the platform class.
import greenfoot.*;
public class Saibaman extends Actor
{
private int direction = 1;
public Saibaman()
{
// constructor code, if any
}
public void act()
{
if (getObjectAtOffset(direction, getY() + getImage().getHeight() + 1, Platform.class) == null)
{
direction = -direction;
GreenfootImage img = getImage();
img.mirrorVertically();
setImage(img);
}
setLocation(getX() + direction, getY());
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Platform here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Platform extends Actor
{
/**
* Act - do whatever the Platform wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private Saibaman enemy = null;
private int enemyDirection = 1;
public void act()
{
if (enemy != null)
{
boolean test1 = (enemy.getX() + 1 > getX() + getImage().getWidth() / 2 && enemyDirection == 1);
boolean test2 = (enemy.getX() - 1 < getX() - getImage().getWidth() / 2 && enemyDirection == -1);
if (test1 || test2) // if a either end of platform
{ // turn around
enemyDirection = -enemyDirection;
GreenfootImage img = enemy.getImage();
img.mirrorVertically();
enemy.setImage(img);
}
enemy.setLocation(enemy.getX() + enemyDirection, enemy.getY());
}
}
public void setEnemy(Saibaman enemy)
{
this.enemy = enemy;
}
}import greenfoot.*;
public class Saibaman extends Actor
{
private int direction = 1;
public Saibaman()
{
// constructor code, if any
}
public void act()
{
setLocation(getX() + direction * (1 + getImage().getWidth() / 2), getY());
if (getOneIntersectingObject(Platform.class) == null)
{
direction = -direction;
GreenfootImage img = getImage();
img.mirrorHorizontally();
setImage(img);
setLocation(getX() + (2 + getImage().getWidth() / 2) * direction , getY());
}
else
{
setLocation(getX() - direction * getImage().getWidth() / 2, getY());
}
}
}import greenfoot.*;
public class Platform extends Actor
{
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
addObject ( new Goku (), 19 , 348 ) ;
addObject ( new Platform(), 49 , 385 ) ;
addObject ( new Platform(), 149 , 385 ) ;
addObject ( new Platform(), 349 , 385 ) ;
addObject ( new Platform(), 449 , 385 ) ;
addObject ( new Platform(), 549 , 385 ) ;
addObject ( new Platform(), 142 , 147 ) ;
addObject ( new Platform(), 349 , 254 ) ;
addObject ( new Platform(), 554 , 135 ) ;
addObject ( new Senzu (), 584 , 103 ) ;
addObject ( new Saibaman(), 107 , 115 ) ;
addObject ( new Saibaman(), 518 , 103 ) ;
}
}