Hi everyone!
The player should be able to spawn three hallucinations and one wall. There is no problem in spawning hallucinations but I just don't get why I can't spawn a wall. The code is literally the same except for the classes used.
There's another problem. I tried around a little to fix this problem but I could only spawn the wall after all three hallucinations have been spawned and it didn't spawn in front of the player. It ignored the player's rotation.
I welcome any help. Thanks in advance.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Sentry extends Actor
{
private int halluNumber=0;
private int wallNumber=0;
private int halluTimer=10;
private char key;
public void act()
{
movement();
keyPressed();
}
public void movement()
{
/* Sentry's movement.
* Controls via Keyboard. (Up, Down, Left, Right)
* If there is an Hallucination in the game, the Sentry can't move during that time.
*/
if(getWorld().getObjects(Hallucination.class).size() == 0)
{
if(Greenfoot.isKeyDown("up"))
{
move(2);
}
if(Greenfoot.isKeyDown("down"))
{
move(-2);
}
if(Greenfoot.isKeyDown("left"))
{
turn(-2);
}
if(Greenfoot.isKeyDown("right"))
{
turn(2);
}
}
}
public void keyPressed()
{
if("h".equals(Greenfoot.getKey())) //Declares the char variable key to "h" in case the player pressed the h-key on the keyboard.
{
key = 'h';
return;
}
if("w".equals(Greenfoot.getKey())) //Declares the char variable key to "w" in case the player pressed the w-key on the keyboard.
{
key = 'w';
return;
}
switch(key)
{
case 'h': //In case "h" was pressed the spawnHallu method gets called up.
spawnHallu();
key = '0'; //Sets the char variable key to 0 in case the player wants to create another hallucination or wall.
break; //Else the programme is going to create hallucinations over and over again until the char variable key changes it's value.
case 'w': //In case "w" was pressed the spawnWall method gets called up.
spawnWall();
key = '0'; //Sets the char variable key to 0 in case the player wants to create an hallucination.
break; //Else the programme tries to create walls over and over again until the char variable key changes it's value.
}
}
public void spawnHallu()
{
if(halluNumber<3)
{
Hallucination hallucination = new Hallucination(); //Creates the Hallucination.
getWorld().addObject(hallucination, getX(), getY()); //Adds the Hallucination to the world.
hallucination.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
hallucination.move(40); //Adds the Hallucination 40 pixel in front of the player.
halluNumber++;
return;
}
}
public void spawnWall()
{
if(wallNumber<1)
{
Wall wall = new Wall(); //Creates the Wall.
getWorld().addObject(wall, getX(), getY()); //Adds the Wall to the world.
wall.setRotation(getRotation()); //Sets the rotation of the new object to be equal to the players rotation.
wall.move(40); //Adds the Wall 40 pixel in front of the player.
wallNumber++;
return;
}
}
}
