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

2015/10/3

Moving Platform

Miggyxd Miggyxd

2015/10/3

#
import greenfoot.*;

/**
 * Write a description of class moving here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class moving extends boxplatform
{
    /**
     * Act - do whatever the moving wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act ()
    {
        checkWalls();
        back();
    }
   
    public void back()
    {
        if(checkWalls() == false)
        {
         move(2);
        }
        
        else{
            move(-2);
        }
    }
    
    public boolean checkWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int) (spriteWidth/2);
        
        Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(rightWall == null)
        {
         return false;   
        }
        else
        {
            stopByRightWall(rightWall);
            return true;
        }
    }
       
    public void stopByRightWall(Actor rightWall)
        {
         int wallWidth = rightWall.getImage().getWidth(); 
         int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2;
         setLocation (newX - 5, getY());
        }
    
    
}

You need to login to post a reply.