I'm working on a game, and in the first level, all the enemies move fine. Then, when I go to the next level, the enemies won't move. They turn like they should, but move(int) will not work in the second level for some reason.
Code for enemies:
And the world code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class E1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class E1 extends Enemies
{
double speed = 2;//this is a subclass of SmoothMover, so I use double just in case I want to set speed to a decimal number
int damage = 3;
int health = 10;
int pspeed = 6;//bullet speed
int fRate = 15;//firing rate
private EP1 ep_;//its bullet
int pxd; //x distance from Player
int pyd; //y distance from PLayer
int c; //counter for shooting
int mc = 0;//movement counter per turn, (will turn after mc > mcl)
int mcl = 20;//movement counter limit
/**
* Act - do whatever the E1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Wall w_ = (Wall)getWorld();
mover2();
}
void shoot(){
c = c + 1;
if(c>fRate){
Wall w_ = (Wall)getWorld();
ep_ = new EP1();
w_.addObject(ep_,getX(),getY());
ep_.speed = pspeed;
ep_.damage = damage;
ep_.setRotation(getRotation());
c = 0;
}
}
void mover2(){//
Wall w_ = (Wall)getWorld();
pxd = (w_.b().getX()-getX());
pyd = (w_.b().getY()-getY());
int px = getX();
int py = getY();
if(pyd<30&&pyd>-30){
if(pxd<0){
setRotation(180);
shoot();
}
if(pxd>0){
setRotation(0);
shoot();
}
}else if(pxd<60 && pxd>-60){
if(pyd<100 && pyd>0 && !atEdge()){
setRotation(270);
move(speed);
}else if(pyd>-100 && pyd<0 && !atEdge()){
setRotation(90);
move(speed);
}else if(pyd>0){
setRotation(90);
shoot();
}else if(pyd<0){
setRotation(270);
shoot();
}
}else{
mc = mc + 1;
move(speed);
if(mc>mcl){
int rot = (Greenfoot.getRandomNumber(4)*90);
setRotation(rot);
mcl = Greenfoot.getRandomNumber(20)+10;
mc = 0;
}
if(getY()>595 || getY()<5){
if(getY()>595){
setRotation(270);
}
if(getY()<5){
setRotation(90);
}
}else{
if(getX()>795){
setRotation(180);
}
if(getX()<5){
setRotation(0);
}
}
}
if(isTouching(Barrier.class)){
setLocation(px,py);
}
}
boolean atEdge(){
if(getX()>795 || getX()<5 || getY()>595 || getY()<5){
return true;
}else{
return false;
}
}
public void damage(int d){
health = health - d;
if(health<1){
Wall w_ = (Wall)getWorld();
w_.removeObject(this);
w_.enemyLost();
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Wall here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Wall extends World
{
private Blocky b_;
private Barrier ba_;
private E1 e1_;
public int level = 0;
public int enemyCount = 0;//enemies still alive in level
/**
* Constructor for objects of class Wall.
*
*/
public Wall()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
L1();
}
public void L1()//set up for level 1
{ level = 1;
b_ = new Blocky();
addObject(b_,50,300);
addBarrier(400,300,10,70);
e1_ = new E1();
addObject(e1_,550,400);
e1_ = new E1();
addObject(e1_,550,200);
enemyCount = 2;
}
public void L2()//set up for level 2
{ level = 2;
enemyCount = 2;
b().setLocation(50,300);
addBarrier(400,300,50,50);
addObject(new E1(),550,400);
addObject(new E1(),550,200);
}
public void enemyLost(){
enemyCount = enemyCount - 1;
if(enemyCount<1){
ba().deleteSelf();
if(level == 1){
L2();
}
}
}
public void addBarrier(int x, int y, int ix, int iy){
ba_ = new Barrier();
addObject(ba_,x,y);
ba_.getImage().scale(ix,iy);
}
public Blocky b(){
return b_;
}
public Barrier ba(){
return ba_;
}
}
