Hi!
Well, i have a problem with my aliens moving Function.
I use an Iterator to move the Array with the aliens, but, i have a problem when i touch the Left part, or the Right part of the screen.
What Hapends?
Well this:
My code is in Spanish (im from venezuela), but i used this as a base:
My actual code is a Modification of this code, and it looks like this
also, this is the Space Invaders that i used as a base: http://www.greenfoot.org/scenarios/984
Hope you understand whats my problem.
My code is in Spanish (im from venezuela), but i used this as a base:
public void move() //move function, called in step
{
if (dir == 1 && shouldCar == 1) {
if (getX() + 5 >= getWorld().getWidth()) { //if the next move would put us out of the map
carridgeRight(); //call the carride function
carridgeAlliesRight(); //bring our buddies
movetimer = 0; //reset timer
shouldMove = 0; //dont move
}
} else if (dir == 2 && shouldCar == 1) {
if (getX() - 5 <= 0) {
carridgeLeft();
carridgeAlliesLeft();
movetimer = 0;
shouldMove = 0;
}
}
if (dir == 1 && shouldMove == 1) { //if we are moving left and we can move
if (getImage() == img1) { //change animation
setImage(img2);
} else {
setImage(img1);
}
setLocation(getX()+5, getY()); //move
shouldMove = 0; //we cant move again yet
} else if (dir == 2 && shouldMove == 1) { //same stuff as before
if (getImage() == img1) {
setImage(img2);
} else {
setImage(img1);
}
setLocation(getX()-5, getY()); //move
shouldMove = 0;
}
else if (shouldMove == 0) { //if we cant move
movetimer++; //add one to the timer
if (movetimer == 24) {//if its at 24 steps
shouldCar = 1; //check for caridge returns
} else if (movetimer == 25) {
shouldMove = 1; //we can move again
shouldCar = 0; //dont want to caridge return now.
movetimer = 0;
}
}
}
private void carridgeRight() //moving down and switching direction function
{
setLocation(getX(), getY() + 5); //move down
dir = 2;//change direction
chance = chance+1; //increases our chance to fire
if (getY() > 60) {
((Space) getWorld()).gameOver();
}
}
private void carridgeLeft()//same as above
{
setLocation(getX(), getY() + 5);
dir = 1;
chance = chance+1;
}
private void carridgeAlliesRight()//executes the preceding function on the other enemies
{
List allies = getWorld().getObjects(Enemy1.class);//list of all the other enemies
Iterator i = allies.iterator();//create an iterator in that list
while(i.hasNext()) {//if theres more enemies to move
Enemy1 a = (Enemy1) i.next();//put the enemy into a variable
if ( a != this) {//if they arent us (cuz we all ready moved)
a.carridgeRight();//move them down
a.setLocation(a.getX(), a.getY());
a.shouldMove = 0;
a.movetimer = 0;
if (getImage() != a.getImage()) {
setImage(a.getImage());
}
}
}
}
private void carridgeAlliesLeft()//same as above
{
List allides = getWorld().getObjects(Enemy1.class);
Iterator i = allides.iterator();
while(i.hasNext()) {
Enemy1 a = (Enemy1) i.next();
if ( a != this) {
a.carridgeLeft();
a.setLocation(a.getX(), a.getY());
a.shouldMove = 0;
a.movetimer = 0;
if (getImage() != a.getImage()) {
setImage(a.getImage());
}
}
}
}public void mover(){
if (direccion == 1 && Mover == 1) { //Cuando va hacia la derecha, y tiene permiso de moverse
if (getX()>= 560) { //Cuando llega al final de la Pantalla
AbajoPosicionDerecha();
AbajoDerecha();
movetiempo = 0;
Mover = 0;
} else {
if (getImage() == img1) {
setImage(img2);
} else {
setImage(img1);
}
setLocation(getX()+20, getY());
Mover = 0;
movetiempo = 0;
}
}
if (direccion == 2 && Mover == 1) {
if (getX()<= 40) {
AbajoPosicionIzquierda();
AbajoIzquierda();
movetiempo = 0;
Mover = 0;
} else {
if (getImage() == img1) {
setImage(img2);
} else {
setImage(img1);
}
setLocation(getX()-20, getY());
Mover = 0;
movetiempo = 0;
}
}
else if (Mover == 0) {
movetiempo++;
if (movetiempo == 24) {
Mover = 1;
} else if (movetiempo == 25) {
Mover = 1;
movetiempo = 0;
}
}
}
/**
controla el movimiento de los marcianos HACIA ABAJO cuando llegan al final de la pantalla, a la derecha
*/
private void AbajoDerecha()
{
setLocation(getX(), getY()+40);
direccion = 2;
chanceParaDisparar = chanceParaDisparar;
}
/**
controla el movimiento de los marcianos HACIA ABAJO cuando llegan al final de la pantalla, a la izquierda
*/
private void AbajoIzquierda()
{
setLocation(getX(), getY()+40);
direccion = 1;
chanceParaDisparar = chanceParaDisparar;
}
/**
* AbajoPosicionDerecha permite mover toda la matriz de los Marcianos en conjunto cuando llegan al final de la pantalla, a la derecha;
*/
private void AbajoPosicionDerecha()
{
List allies = getWorld().getObjects(Maicol.class);
Iterator n = allies.iterator();
while(n.hasNext()) {
Maicol a = (Maicol) n.next();
if ( a != this) {
a.AbajoDerecha();
a.setLocation(a.getX(), a.getY());
a.Mover = 0;
a.movetiempo = 0;
if (getImage() != a.getImage()) {
setImage(a.getImage());
}
}
}
}
/**
* AbajoPosicionDerecha permite mover toda la matriz de los Marcianos en conjunto cuando llegan al final de la pantalla, a la Izquierda;
*/
private void AbajoPosicionIzquierda()
{
List allies = getWorld().getObjects(Maicol.class);
Iterator n = allies.iterator();
while(n.hasNext()) {
Maicol a = (Maicol) n.next();
if ( a != this) {
a.AbajoIzquierda();
a.setLocation(a.getX(), a.getY());
a.Mover = 0;
a.movetiempo = 0;
if (getImage() != a.getImage()) {
setImage(a.getImage());
}
}
}
}
