Unlimited wrote...
So to get that to work i need to remove PlayerY and the getX() after Azure actor and Vile actor. Is that correct?
public void checkPlayer()
{
if(Azure == null)
{
if (!getObjectsInRange(200, Azure.class).isEmpty()) Azure = (getObjectsInRange(200, Azure.class).get(0)) ;
}
else if (shooting() == true)
{
int playerX = Azure.getX();
if(Azure.getX() > getX() && animSet != shootAnim){
setAnim(shootAnim, 2);
speed = 0;
if(shootingCounter <= 0)
getWorld().addObject(new EnemyVilePlasma(), getX()+90, getY()-34);
shootingCounter = 5;
}
else if (Azure.getX() < getX() && animSet != shootLeftAnim ){
setAnim(shootLeftAnim, 2);
speed = 0;
if(shootingCounter <= 0)
getWorld().addObject(new EnemyVilePlasmaLeft(), getX()-90, getY()-34);
shootingCounter = 5;
}
}
else if (shooting() == false)
{
if(animSet != rightAnim && animSet == shootAnim){
setAnim(rightAnim, 2);
speed = 15;
}
if(animSet != leftAnim && animSet == shootLeftAnim){
setAnim(leftAnim, 2);
speed = -15;
}
}
}else if (Azure.getWorld() == null) return;
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Vile here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Vile extends Enemy
{
private int vSpeed = 0;
private int acceleration = 1;
private int shootingCounter = 5;
private int stopCounter = 200;
private int resumeCounter = 220;
private int spriteHeight = getImage().getHeight();
private int spriteWidth = getImage().getWidth();
private int lookForGroundDistance = (int)spriteHeight/2;
private int lookForEdge = (int)spriteWidth/2;
private int speed = 15;
private Actor Azure;
private int health = 50;
private int animCount;
private int animDelay = 6;
private static GreenfootImage[] vileStandAnim = new GreenfootImage[1];
private static GreenfootImage[] vileStandLeftAnim = new GreenfootImage[1];
private static GreenfootImage[] rightAnim = new GreenfootImage[2];
private static GreenfootImage[] leftAnim = new GreenfootImage[2];
private static GreenfootImage[] shootAnim = new GreenfootImage[1];
private static GreenfootImage[] shootLeftAnim = new GreenfootImage[1];
static
{
for (int i=0; i<vileStandAnim.length; i++) vileStandAnim[i] = new GreenfootImage("VileStand_0"+(i+1)+".png");
for (int i=0; i<vileStandLeftAnim.length; i++) vileStandLeftAnim[i] = new GreenfootImage("VileStandLeft_0"+(i+1)+".png");
for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("VileDash_0"+(i+1)+".png");
for (int i=0; i<leftAnim.length; i++) leftAnim[i] = new GreenfootImage("VileDashLeft_0"+(i+1)+".png");
for (int i=0; i<shootAnim.length; i++) shootAnim[i] = new GreenfootImage("VileShoot_0"+(i+1)+".png");
for (int i=0; i<shootLeftAnim.length; i++) shootLeftAnim[i] = new GreenfootImage("VileShootLeft_0"+(i+1)+".png");
}
private void setAnim(GreenfootImage[] anim, int frameRate)
{
animSet = anim;
animCount = -1;
animDelay = frameRate;
animate();
}
private GreenfootImage[] animSet = vileStandLeftAnim;
/**
* Act - do whatever the Vile wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
animate();
checkAnim();
checkFall();
canMoveRight();
canMoveLeft();
move();
stopCounter--;
shootingCounter--;
resumeCounter--;
destroy();
checkPlayer();
}
public void checkPlayer()
{
if(Azure == null)
{
if (!getObjectsInRange(500, Azure.class).isEmpty()) Azure = (getObjectsInRange(500, Azure.class).get(0)) ;
}
else if (Azure.getWorld() == null) return;
else if (shooting() == true)
{
int playerX = Azure.getX();
if(Azure.getX() > getX() && animSet != shootAnim){
setAnim(shootAnim, 2);
speed = 0;
if(shootingCounter <= 0)
getWorld().addObject(new EnemyVilePlasma(), getX()+90, getY()-34);
shootingCounter = 5;
}
else if (Azure.getX() < getX() && animSet != shootLeftAnim ){
setAnim(shootLeftAnim, 2);
speed = 0;
if(shootingCounter <= 0)
getWorld().addObject(new EnemyVilePlasmaLeft(), getX()-90, getY()-34);
shootingCounter = 5;
}
}
else if (shooting() == false)
{
if(animSet != rightAnim && animSet == shootAnim){
setAnim(rightAnim, 2);
speed = 15;
}
if(animSet != leftAnim && animSet == shootLeftAnim){
setAnim(leftAnim, 2);
speed = -15;
}
}
}
public void setHealth(int points) {
health += points;
}
public int getHealth() {
return health;
}
public void destroy()
{
World myWorld = getWorld();
AzureWorld azureworld = (AzureWorld)myWorld;
if(health==0)
{
GameOver gameover = new GameOver();
getWorld().removeObject(this);
myWorld.addObject(gameover, myWorld.getWidth()/2,myWorld.getHeight()/2);
}
}
public boolean shooting()
{
if (stopCounter <= 0)
{
if(resumeCounter <= 0){
stopCounter = 200;
resumeCounter = 220;
}
return true;
}
else
{
return false;
}
}
public void move()
{
if(canMoveRight() == true && canMoveLeft() == false )
{
speed = 15;
move(speed);
}
else if(canMoveLeft() == true && canMoveRight() == false)
{
speed = -15;
move(speed);
}
else
{
move(speed);
}
}
public boolean canMoveLeft()
{
boolean canMoveLeft = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Collider.class) !=null ||
getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2-65, Collider.class) !=null)
canMoveLeft = false;
return canMoveLeft;
}
public boolean canMoveRight()
{
boolean canMoveRight = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Collider.class) !=null ||
getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2-65, Collider.class) !=null)
canMoveRight = false;
return canMoveRight;
}
public void checkAnim()
{
if(speed == 15 && animSet != rightAnim)setAnim(rightAnim, 2);
else if(speed == -15 && animSet != leftAnim) setAnim(leftAnim, 2);
}
public void fall()
{
setLocation(getX(), getY() +vSpeed);
{
vSpeed = vSpeed + acceleration;
}
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int lookForGround = (int) (spriteHeight/2-30);
Actor ground = getOneObjectAtOffset(0, lookForGround, Collider.class);
if(ground == null)
{
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
setLocation(getX(), newY);
}
public void checkFall()
{
if(onGround())
{
vSpeed = 0;
}
else
{
fall();
}
}
private void animate()
{
animCount = (animCount+1)%(animSet.length*animDelay);
if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* This class is just an example. You can delete it or change the code.
* It's not necessary for the scrolling system.
*/
public class Azure extends ScrollingActor
{
private int vSpeed = 0;
private int acceleration = 1;
private boolean jumping;
private int shootingCounter = 10;
private int Counter = 1;
boolean hitInv = false;
private static GreenfootImage[] rightAnim = new GreenfootImage[10];
private static GreenfootImage[] leftAnim = new GreenfootImage[10];
private static GreenfootImage[] standAnim = new GreenfootImage[9];
private static GreenfootImage[] standLeftAnim = new GreenfootImage[9];
private static GreenfootImage[] runAnim = new GreenfootImage[10];
private static GreenfootImage[] runLeftAnim = new GreenfootImage[10];
private static GreenfootImage[] runBAnim = new GreenfootImage[10];
private static GreenfootImage[] runLeftBAnim = new GreenfootImage[10];
private static GreenfootImage[] jumpAnim = new GreenfootImage[3];
private static GreenfootImage[] jumpLeftAnim = new GreenfootImage[3];
private static GreenfootImage[] standBAnim = new GreenfootImage[1];
private static GreenfootImage[] standLeftBAnim = new GreenfootImage[1];
private static GreenfootImage[] rightBAnim = new GreenfootImage[11];
private static GreenfootImage[] leftBAnim = new GreenfootImage[11];
private static GreenfootImage[] jumpBAnim = new GreenfootImage[4];
private static GreenfootImage[] jumpLeftBAnim = new GreenfootImage[4];
private static GreenfootImage[] exAnim = new GreenfootImage[7];
private static GreenfootImage[] getHitAnim = new GreenfootImage[4];
private static GreenfootImage[] getHitLeftAnim = new GreenfootImage[4];
static
{
for (int i=0; i<standAnim.length; i++) standAnim[i] = new GreenfootImage("Stand_0"+(i+1)+".png");
for (int i=0; i<standLeftAnim.length; i++) standLeftAnim[i] = new GreenfootImage("StandLeft_0"+(i+1)+".png");
for (int i=0; i<rightAnim.length; i++) rightAnim[i] = new GreenfootImage("Walk-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<leftAnim.length; i++) leftAnim[i] = new GreenfootImage("WalkLeft-A_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<runAnim.length; i++) runAnim[i] = new GreenfootImage("Run_0"+(i+1)+".png");
for (int i=0; i<runLeftAnim.length; i++) runLeftAnim[i] = new GreenfootImage("RunLeft_0"+(i+1)+".png");
for (int i=0; i<runBAnim.length; i++) runBAnim[i] = new GreenfootImage("RunB_0"+(i+1)+".png");
for (int i=0; i<runLeftBAnim.length; i++) runLeftBAnim[i] = new GreenfootImage("RunLeftB_0"+(i+1)+".png");
for (int i=0; i<jumpAnim.length; i++) jumpAnim[i] = new GreenfootImage("Jump_0"+(i+1)+".png");
for (int i=0; i<jumpLeftAnim.length; i++) jumpLeftAnim[i] = new GreenfootImage("JumpLeft_0"+(i+1)+".png");
for (int i=0; i<standBAnim.length; i++) standBAnim[i] = new GreenfootImage("StandB_0"+(i+1)+".png");
for (int i=0; i<standLeftBAnim.length; i++) standLeftBAnim[i] = new GreenfootImage("StandLeftB_0"+(i+1)+".png");
for (int i=0; i<rightBAnim.length; i++) rightBAnim[i] = new GreenfootImage("WalkB_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<leftBAnim.length; i++) leftBAnim[i] = new GreenfootImage("WalkLeftB_"+(i < 10 ? "0" : "")+(i+1)+".png");
for (int i=0; i<jumpBAnim.length; i++) jumpBAnim[i] = new GreenfootImage("JumpB_0"+(i+1)+".png");
for (int i=0; i<jumpLeftBAnim.length; i++) jumpLeftBAnim[i] = new GreenfootImage("JumpLeftB_0"+(i+1)+".png");
for (int i=0; i<getHitAnim.length; i++) getHitAnim[i] = new GreenfootImage("Damage_0"+(i+1)+".png");
for (int i=0; i<getHitLeftAnim.length; i++) getHitLeftAnim[i] = new GreenfootImage("DamageLeft_0"+(i+1)+".png");
}
private int animCount;
private int animDelay = 6;
private void setAnim(GreenfootImage[] anim, int frameRate)
{
animSet = anim;
animCount = -1;
animDelay = frameRate;
animate();
}
private GreenfootImage[] animSet = standAnim;
/**
* Here you can tell your actor what he has to do.
*/
public void act() {
animate();
canMoveRight();
canMoveLeft();
horizontalMovement();
checkFall();
shootingCounter --;
getHit();
fallOutOfWorld();
}
public void fall()
{
setLocation(getX(), getY() +vSpeed);
{
vSpeed = vSpeed + acceleration;
}
jumping = true;
}
public boolean onGround()
{
int spriteHeight = getImage().getHeight();
int lookForGround = (int) (spriteHeight/2-30);
Actor ground = getOneObjectAtOffset(0, lookForGround, Collider.class);
if(ground == null)
{
jumping = true;
return false;
}
else
{
moveToGround(ground);
return true;
}
}
public boolean ceiling()
{
int spriteHeight = getImage().getHeight();
int yDistance = (int) (spriteHeight/-2);
Actor ceiling = getOneObjectAtOffset(0, yDistance, Collider.class);
if(ceiling != null)
{
vSpeed =0;
hitHead(ceiling);
return true;
}
else
{
return false;
}
}
public void hitHead(Actor ceiling)
{
int ceilingHeight = ceiling.getImage().getHeight();
int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
setLocation(getX(), newY);
}
public boolean canMoveLeft()
{
boolean canMoveLeft = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/-2, Collider.class) !=null ||
getOneObjectAtOffset(imageWidth/-2 -3, imageHeight/2-35, Collider.class) !=null)
canMoveLeft = false;
return canMoveLeft;
}
public boolean canMoveRight()
{
boolean canMoveRight = true;
int imageWidth = getImage().getWidth();
int imageHeight = getImage().getHeight();
if (getOneObjectAtOffset(imageWidth/2 +3, imageHeight/-2, Collider.class) !=null ||
getOneObjectAtOffset(imageWidth/2 +3, imageHeight/2-35, Collider.class) !=null)
canMoveRight = false;
return canMoveRight;
}
public void moveToGround(Actor ground)
{
int groundHeight = ground.getImage().getHeight();
int newY = ground.getY() - (groundHeight + getImage().getHeight())/2+30;
setLocation(getX(), newY);
jumping = false;
}
public void checkFall()
{
if(onGround())
{
vSpeed = 0;
}
else
{
fall();
}
}
public void horizontalMovement()
{
boolean onGround = false;
int boost = 6;
int dir = 0;
int vDir = (int)Math.signum(vSpeed);
if (Greenfoot.isKeyDown("left") && canMoveLeft()) dir--;
if (Greenfoot.isKeyDown("right") && canMoveRight()) dir++;
setLocation(getX()+(8)*dir, getY());
if(dir == -1){
Counter = 0;
}
if(dir == 1){
Counter = 1;
}
if (dash() == true)
setLocation(getX()+(8+boost)*dir, getY());
if (jump() == true)
setLocation(getX(), getY() - 20);
if (vDir > 0) onGround = true;
if (dir == -1 && animSet != leftAnim && dash() == false && shooting() == false && vSpeed <= 0 ) setAnim(leftAnim, 3);
else if (dir == 1 && animSet != rightAnim && dash() == false && shooting() == false && vSpeed <= 0) setAnim(rightAnim, 3);
else if (dir == 0 && animSet != standLeftAnim && shooting() == false && vSpeed <= 0 && Counter == 0) setAnim( standLeftAnim, 6);
else if (dir == 0 && animSet != standAnim && shooting() == false && vSpeed <= 0 && Counter == 1) setAnim( standAnim, 6);
else if (dir == 1 && animSet != jumpAnim && jump() == true && shooting() == false ) setAnim(jumpAnim, 16);
else if (dir == -1 && animSet != jumpLeftAnim && jump() == true && shooting() == false ) setAnim(jumpLeftAnim, 16);
else if (dir == 0 && animSet != jumpLeftAnim && jump() == true && shooting() == false && Counter == 0) setAnim(jumpLeftAnim, 6);
else if (dir == 0 && animSet != jumpAnim && jump() == true && shooting() == false && Counter == 1) setAnim(jumpAnim, 6);
else if ( dir == 1 && dash() == true && animSet != runAnim && jump() == false && shooting() == false && vSpeed <= 0 )setAnim(runAnim,2);
else if ( dir == -1 && dash() == true && animSet != runLeftAnim && jump() == false && shooting() == false && vSpeed <= 0 )setAnim(runLeftAnim,2);
else if ( dir == 1 && dash() == true && animSet != runBAnim && jump() == false && shooting() == true && vSpeed <= 0 )setAnim(runBAnim,2);
else if ( dir == -1 && dash() == true && animSet != runLeftBAnim && jump() == false && shooting() == true && vSpeed <= 0 )setAnim(runLeftBAnim,2);
else if (dir == 0 && animSet != standLeftBAnim && jump() == false && shooting() == true && vSpeed <= 0 && Counter == 0) setAnim(standLeftBAnim, 6);
else if (dir == 0 && animSet != standBAnim && jump() == false && shooting() == true && vSpeed <= 0 && Counter == 1) setAnim(standBAnim, 6);
else if (dir == 1 && animSet != leftBAnim && dash() == false && shooting() == true && vSpeed <= 0 ) setAnim(leftBAnim, 3);
else if (dir == -1 && animSet != rightBAnim && dash() == false && shooting() == true && vSpeed <= 0 ) setAnim(rightBAnim, 3);
else if (dir == -1 && animSet != jumpBAnim && jump() == true && shooting() == true ) setAnim(jumpBAnim, 8);
else if (dir == 1 && animSet != jumpLeftBAnim && jump() == true && shooting() == true ) setAnim(jumpLeftBAnim, 8);
else if (dir == 0 && animSet != jumpBAnim && jump() == true && shooting() == true && Counter == 0) setAnim(jumpBAnim, 8);
else if (dir == 0 && animSet != jumpLeftBAnim && jump() == true && shooting() == true && Counter == 1) setAnim(jumpLeftBAnim, 8);
if(dir == 1 && shooting() == true && shootingCounter <=0 )
{
getWorld().addObject(new PlasmaShot(), getX()+90, getY()-34);
shootingCounter = 10;
Greenfoot.playSound("Laser.mp3");
}
else if(dir == -1 && shooting() == true && shootingCounter <=0 )
{
getWorld().addObject(new PlasmaShotLeft(), getX()-90, getY()-34);
shootingCounter = 10;
Greenfoot.playSound("Laser.mp3");
}
else if (dir == 0 && (animSet == jumpLeftBAnim || animSet == standBAnim ) && shooting() == true && shootingCounter <=0 )
{
getWorld().addObject(new PlasmaShot(), getX()+90, getY()-34);
shootingCounter = 10;
Greenfoot.playSound("Laser.mp3");
}
else if (dir == 0 && (animSet == jumpBAnim || animSet == standLeftBAnim) && shooting() == true && shootingCounter <=0 )
{
getWorld().addObject(new PlasmaShotLeft(), getX()-90, getY()-34);
shootingCounter = 10;
Greenfoot.playSound("Laser.mp3");
}
else if ((dir == 1 || dir == 0) && animSet != getHitAnim && hitInv == true) setAnim(getHitAnim,2);
else if (dir == -1 && animSet != getHitLeftAnim && hitInv == true) setAnim(getHitLeftAnim,2);
}
public boolean jump()
{
if (Greenfoot.isKeyDown("space")){
return true;
}
else
{
return false;
}
}
public boolean dash()
{
if (Greenfoot.isKeyDown("s"))
{
return true;
}
else
{
return false;
}
}
public boolean shooting()
{
if (Greenfoot.isKeyDown("d"))
{
return true;
}
else
{
return false;
}
}
public void getHit()
{
Actor Enemy = getOneIntersectingObject(Enemy.class);
if(Enemy != null)
{
World myWorld = getWorld();
AzureWorld azureworld = (AzureWorld)myWorld;
HP healthbar = azureworld.getHP();
if (hitInv == false)
{
healthbar.loseHealth();
hitInv = true;
if(healthbar.health <=0)
{
GameOver gameover = new GameOver();
myWorld.addObject(gameover, myWorld.getWidth()/2,myWorld.getHeight()/2);
myWorld.removeObject(this);
}
}
}
else
{
hitInv = false;
}
}
public void fallOutOfWorld()
{
World myWorld = getWorld();
AzureWorld azureworld = (AzureWorld)myWorld;
if (getY()>300){
GameOver gameover = new GameOver();
getWorld().removeObject(this);
myWorld.addObject(gameover, myWorld.getWidth()/2,myWorld.getHeight()/2);
}
}
private void animate()
{
animCount = (animCount+1)%(animSet.length*animDelay);
if (animCount%animDelay == 0) setImage(animSet[animCount/animDelay]);
}
}
if (getWorld() == null) return;