This site requires JavaScript, please enable it in your browser!
Greenfoot back
alejandrodr1903
alejandrodr1903 wrote ...

2016/3/18

Problem with Moving Function on Space Invaders Project

alejandrodr1903 alejandrodr1903

2016/3/18

#
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:
 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());
                }
            }
        }
    }
My actual code is a Modification of this code, and it looks like this
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());
                }
            }
        }
    }
also, this is the Space Invaders that i used as a base: http://www.greenfoot.org/scenarios/984 Hope you understand whats my problem.
danpost danpost

2016/3/18

#
I do not know how it would be influenced by this and I have not tried either of the codes given -- but, line 10 in the original code uses an 'else if' where your line 20, which appears to be the cooresponding line, only uses 'if'.
alejandrodr1903 alejandrodr1903

2016/3/19

#
Thanks Danpost, but, i tried the mentioned code before i actually created the game. and, the Else If works same if i use only IF But anyway, the thing is that the Iterator (Misteriously) doesnt take the last Alien of the line (the one who got the error). Actually, the Aliens move perfectly, but they have that error. I checked the code who creates the array with the aliens
public void poblacion (){    
        Maicol[] enemies1 = new Maicol[41];
        for ( int i = 0; i <8; i++ ) {
            enemies1[i] = new Maicol();
            addObject(enemies1[i], (i*60)+40, 40);
        }
        for ( int i = 0; i <8; i++ ) {
            enemies1[i] = new Maicol();
            addObject(enemies1[i], (i*60)+40, 90);
        }
        for ( int i = 0; i <8; i++ ) {
            enemies1[i] = new Maicol();
            addObject(enemies1[i], (i*60)+40, 140);
        }
        for ( int i = 0; i <8; i++ ) {
            enemies1[i] = new Maicol();
            addObject(enemies1[i], (i*60)+40, 190);
        }
        for ( int i = 0; i <8; i++ ) {
            enemies1[i] = new Maicol();
            addObject(enemies1[i], (i*60)+40, 240);
        }
        }
and, i checked the Move function when the alien touch the left/right of the screen (wich is a 600*800*1 Screen)
public void mover(){
    if (direccion == 1 && Mover == 1) { 
         if (getX()>= 560) { 
                AbajoPosicionDerecha();
                AbajoDerecha(); 
                movetiempo = 0;
                Mover = 0;
         } else {
            if (getImage() == img1) {
                setImage(img2);
            } else {
                setImage(img1);
            } 
            setLocation(getX()+20, getY());
            Mover = 0;
            movetiempo = 0;
        }
    }
Why i say that it is the iterator Function? well, i turn off the Iterator Function and, logically, It shows how every column of aliens go down (Works Fine, it always respects the size of my screen, and no alien changes its position relative to the size of my screen. ). But, as i said, when I turned on that Iterator function in the code, its when the fail starts, because it doesnt take the last alien of the array... Maybe im doing something wrong while im declaring the array
danpost danpost

2016/3/19

#
I have a demo of my way of coding movement for a space invaders game -- it is here. Maybe you can download it and check it out. I noticed that the source was missing, so I will update it with the source available for you.
alejandrodr1903 alejandrodr1903

2016/3/28

#
danpost wrote...
I have a demo of my way of coding movement for a space invaders game -- it is here. Maybe you can download it and check it out. I noticed that the source was missing, so I will update it with the source available for you.
Hi dan! Sorry for the delay! Well, i saw your code, and i noticed that you declared the aliens inside a Row, wich is a pretty good idea, but, when you change the size of the screen, it only takes the workspace of your original project... if you want to check it, just set another size of screen. Also, when i try to add another line of aliens to it, that last line doesnt move, until another row touches it Hope you can help me
danpost danpost

2016/3/28

#
alejandrodr1903 wrote...
Well, i saw your code, and i noticed that you declared the aliens inside a Row, wich is a pretty good idea, but, when you change the size of the screen, it only takes the workspace of your original project... if you want to check it, just set another size of screen.
That demo scenario of mine was not meant to be modified by others. The concept of using Row objects to control the movement was just being demonstrated.
Also, when i try to add another line of aliens to it, that last line doesnt move, until another row touches it
You will need to show your world class code for help on this (and the other issue).
You need to login to post a reply.