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

2015/2/2

Scrolling Background

1
2
Jellyfish Jellyfish

2015/3/28

#
@danpost I want to make my actor be the center of the screen at all times. How can I edit this code in the FatCat class to do that?
import greenfoot.*;

/**
 * A user-controlled actor that walks and jumps, and is pulled down by gravity.
 * <l><li>Left arrow moves actor left (toward left scroll limit)</li>
 * <li>Right arrow moves actor right (toward right scroll limit)</li>
 * <li>Up arrow makes the actor jump</li><l>
 */
public class FatCat extends Actor
{
    final int jSpeed = 25; // the initial 'jump' speed
    int ySpeed = 0, xSpeed = 0; // the initial vertical and horizontal speeds
    boolean aboutFace; // the direction (left or right) the actor is facing
    boolean onGround; // the state of the actor being set on an object or not

    /** 
     * Checks for changes in direction and moves the main actor.
     */
    public void act()
    {
        getDirection();
        move();
    }
    
    /**
     * Moves the actor with appropriate image.  Checks for obstacles and adjusts
     * the position of the actor accordingly.
     */
    private void move()
    {
        ySpeed++; // adds gravity
        if (xSpeed != 0 && onGround) xSpeed+=aboutFace?1:-1; // add friction
        setLocation(getX()+xSpeed/10, getY()+ySpeed/2);
        // check for change in horizontal direction
        if((xSpeed>0 && aboutFace) || (xSpeed<0 && !aboutFace)) 
        {
            getImage().mirrorHorizontally();
            aboutFace = !aboutFace;
        }
        
        // check for obstacles
        onGround=false; // initialize value
        // check below the actor
        while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
        {
            setLocation(getX(), getY()-1); 
            onGround=true; 
            ySpeed=0;
        }
        // check above the actor
        while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null) 
        {
            setLocation(getX(), getY()+1);
            ySpeed = 0;
        }
        // check to right of actor
        while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
        {
            setLocation(getX()-1, getY());
            xSpeed = 0;
        }
        // check to left of actor
        while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
        {
            setLocation(getX()+1, getY());
            xSpeed = 0;
        }
    }
    
    /**
     * Determines any changes in horizontal and vertical speeds for the actor.
     */
    private void getDirection()
    {
        //if (!onGround) return; // if not mid-air changes allowed
        // sets requested direction of move, or continues in current direction
        if (Greenfoot.isKeyDown("a") && xSpeed>-50) xSpeed-=2; // check left
        if (Greenfoot.isKeyDown("d") && xSpeed<50) xSpeed+=2; // check right
        if (Greenfoot.isKeyDown("space") && onGround) // check jump
        {
            ySpeed -= jSpeed; // add jump speed
        }
    }
}
danpost danpost

2015/3/28

#
Jellyfish wrote...
@danpost I want to make my actor be the center of the screen at all times. How can I edit this code in the FatCat class to do that?
The FatCat class does not need editing to perform the scrolling. Scrolling is much bigger than just the central character. It is something that encompasses the entire world (the background of the world AND all actors in that world that are allowed to scroll). Scrolling should done through process either performed in the world act method or in a method it calls. I created several fairly simple scroll support classes that can be used to produce scrolling in various ways. It would be difficult to say which one would best suit your needs without first knowing a bit more about the limitations you wish to have imposed on your scrolling. First, whether there is a background image that needs to be shifted during scrolling; and, whether that image is to repeat or not indefinitely, or will the limits of the image set the limits of the scrolling. Or, if the background does not scroll, will scrolling be unbounded or will limits be set. That information would be good to start with.
You need to login to post a reply.
1
2