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

2016/6/30

movement problems

CE3A CE3A

2016/6/30

#
I am working on a space invaders style game, and I am having trouble getting the enemies to move correctly. I am trying to get them to move back and forth across the screen, but for some reason the enemies just go to the right side and stay there. this is the code i currently have:
if (movementDirection == "right") 
        {
            setLocation(getX() + 1, getY());
        }else if (movementDirection == "left") 
        {
            setLocation(getX() - 1, getY());
        }
        if (isAtEdge()) 
        {
            if(movementDirection == "right") {movementDirection = "left";}
            if (movementDirection == "left") {movementDirection = "right";}
        }
i also tried this (FYI world size is 800x600):
if(movementDirection == "right" && getX() < 800) 
        {
            setLocation(getX() + 1, getY());
        } else if(movementDirection == "right" && getX() >= 800)
        {
            movementDirection = "left";
        } else if (movementDirection == "left" && getX() > 0) 
        {
            setLocation(getX() - 1, getY());
        }else if (movementDirection == "left" && getX() <= 0) 
        {
            movementDirection = "right";
        }else 
        {
            move(-100);
        }
and the whole class (currently) is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class enemyShip extends Actor
{
    public static int health = 3;
    public String movementDirection = "right";
    
    public void act() 
    {
        /*
        if(movementDirection == "right" && getX() < 800) 
        {
            setLocation(getX() + 1, getY());
        } else if(movementDirection == "right" && getX() >= 800)
        {
            movementDirection = "left";
        } else if (movementDirection == "left" && getX() > 0) 
        {
            setLocation(getX() - 1, getY());
        }else if (movementDirection == "left" && getX() <= 0) 
        {
            movementDirection = "right";
        }else 
        {
            move(-100);
        }*/
        if (movementDirection == "right") 
        {
            setLocation(getX() + 1, getY());
        }else if (movementDirection == "left") 
        {
            setLocation(getX() - 1, getY());
        }
        if (isAtEdge()) 
        {
            if(movementDirection == "right") {movementDirection = "left";}
            if (movementDirection == "left") {movementDirection = "right";}
        }
        
        if(isTouching(spaceShip.class))
        {
           spaceShip.ded = true;
           getWorld().getBackground().drawImage(new GreenfootImage("GAME OVER", 50, Color.blue, Color.black), 350, 250);
        }
        
        if (health <= 0) 
        {
            getWorld().removeObject(this);
        }
    }
    
    public static void hit() 
    {
        health--;
    }
}
danpost danpost

2016/6/30

#
Use 'else if' at the beginning of line 38. Otherwise you are never getting "left" to be the direction value after it is "right". That is, if line 37 ever changes the value to "left", line 38 will turn it right back to "right" (without the 'else').
danpost danpost

2016/6/30

#
Another issue is with your string comparisons. "right" and "left" are String objects and you can have multiple String objects that contain the same character sequence. When comparing a current String object to a literally given (new) String object, they are different objects and the evaluation of the comparison will be false -- they are not refer to the same object. To compare the contents of the two different String objects, you need to use the 'equals' method instead of the conditional equality symbol '=='. For example:
if ("left".equals(movementDirection))
CE3A CE3A

2016/6/30

#
Thanks a lot! i cant believe i didn't realize the ifs would do that. i also didn't know that java did that with strings, so thanks for letting me know!
danpost danpost

2016/7/1

#
CE3A wrote...
Thanks a lot! i cant believe i didn't realize the ifs would do that. i also didn't know that java did that with strings, so thanks for letting me know!
Yeah. Well, to show how you could do it using the conditional equality symbol:
// with fields
private static final String LEFT = "left", RIGHT = "right";
private String movementDirection = RIGHT; // setting value to a specific String object

// the comparison
if (movementDirection == RIGHT)
This would work because the object referred to by movementDirection is either that of LEFT or RIGHT (provided any future changes in its value would set it to LEFT or RIGHT). Me ... I prefer to make direction fields of int type because a vector is a composite of the speed and direction where speed is the value and direction is determined by the sign,, '+1 * speed' or '-1 * speed'. That way, only one statement is needed for movement 'direction * speed'. One more thing you should be aware of is that using 800 (the world width) as the cut-off for changing directions will not work. The maximum x-coordinate value any actor can be in a bounded world is one less than the world width (rememeber that an x-coordinate of zero is possible and one of the possible 800 values that the coordinate can be, so 799 would be the last of the 800 possible coordinates. You should make the comparison as follows:
if (getX() == getWorld().getWidth()-1)
As coded, if you decide to change the world width for any reason, the code will still work properly. There is no sense in asking if the value is greater than that as in a bounded world, any movement that tries to put the actor beyond will have the actor set AT the bounded limit.
You need to login to post a reply.