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

2015/4/23

Moving right past a point, then moving left and stopping at the point?

shooterbooth shooterbooth

2015/4/23

#
I have a title text string that moves across the screen from left to right (starting off the screen). I wrote code for it to move right and then stop in the middle of the world but I now want to add code that makes it move right all the way to the edge of the world (getX()==460) and then move left until it reaches the middle of the world (230,230). I tried to use booleans but can't get it to work. Thanks for any help **The problem seems to be with the booleans because it gets to getX()==460 and then moves left to getX()==459. The problem is the boolean only changes when the object is at getX()==460 and then returns to what it was before.
import greenfoot.*;
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class Title here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Title extends Actor
{
    private float fontSize;
    private GreenfootImage img;
    public boolean move, move2;
    public Title()
    {
        fontSize = 20.0f;
        img = new GreenfootImage(230, 100);
        img.setFont(new Font("Courier New", Font.PLAIN, 40));
        img.setColor(Color.WHITE);
        img.drawString("Breakout", 25, 40);
        setImage(img);
        
        move = false;
        move2 = true;
    }
    
    /**
     * 
     */
    public void act() 
    {
        if (getX()<460&&move==false)
        {
            moveRight();
        }
        if(getX()<460&&move==true)
        {
            moveLeft();
        }
    }    
    
    public void moveRight()
    {
        if(move2==true)
        {
            move(2);
        }
    }
    public void moveLeft()
    {
        if(move2==false)
        {
            move(-2);
        }
    }
}
danpost danpost

2015/4/23

#
I was going to suggest adding a direction field to the class, setting its value to 1 for right, -1 for left, and zero for stopped; using it as a multiplier to the speed value (2). However, this is actually not needed because you are moving the title at the speed of 2. If you place your title into the world at an even X coordinate location, then it will maintain an even X coordinate location until it reaches the right edge of the world (the title can never be at an X coordinate value of 460 as 459 is the last X coordinate value that can be reached). This even/odd change can be used to determine the direction of movement for the title and the only time the title will be at the X coordinate value of 229 is after going all the way to the right and then back to it. Therefore, we have this:
public void move()
{
    if (getX() == 229) return; // at end location
    int direction = 1-2*(getX()%2); // odd/even determines direction
    move(2*direction); // movement
}
You need to login to post a reply.