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

2016/5/3

Object don't spawn in scrolling world

TheBigBossJoshi TheBigBossJoshi

2016/5/3

#
Hi! I am working on a project, but I have hit a bump in the road. I have created a scrolling world, but nothing other than my Platform and Wall classes spawn past the first screen. I have spent way too long on this, and I need help. Here is my world class and my scrolling classes. WORLD:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * @co-author Govind Joshi
 * @co-author Parker Bracamontez
 * @co-author Joshua Franklin
 * @version 4/26/16
 */
public class MyWorld extends World
{
    GreenfootSound sound;
    private final int SPAWN_DISTANCE = 115;
    private Player thePlayer;
    private LevelEnd le; 
    private WorldScroll ws;
    private boolean levelEnded = false;
    
    public MyWorld()
    {    
        super(600, 400, 1, false); 
        
        addThings();
        
        thePlayer = new Player();
        addObject(thePlayer, 100, 20);
        
        ws = new WorldScroll();
        addObject(ws, 0, -10);   
        
        le = new LevelEnd();
        
        addObject(new AirDrop(), 50, 20);
        
        addObject(new Instructions(), 300, 100);
        
        sound = new GreenfootSound("contra1.wav");
        sound.play();
    }

    public void addThings()
    {
        for(int i = 0; i < 40; i++)
        {
            addObject(new Platform(), i*SPAWN_DISTANCE, 390);
        }
        for(int i = 1; i < 15; i++)
        {
            addObject(new Platform(), i*(SPAWN_DISTANCE * 2), 275);
        }
        for(int i = 1; i < 20; i++)
        {
            addObject(new Alien2(), i * 350, 150);
        }
        for(int i = 0; i < 10; i++)
        {
            addObject(new Wall(), 50 + (i * 500), 350);
        }     
        for(int i = 2; i < 20; i++)
        {
            addObject(new Alien(), i * 225, 250);
        }   
    }
    
    public Player getPlayer()
    {
        return thePlayer;
    }
    
    public WorldScroll getWorldScroll()
    {
        return ws;
    }
    
}
WORLD SCROLL (ACTOR)
import greenfoot.*;  
import java.awt.Color;

public class WorldScroll extends Actor
{
    //Scroll Variables
    public boolean shouldScroll; 
    public int playerX; 
    //Object Variables
    private final int JUMP_HEIGHT = -20;
    private final int FALL_SPEED = 2;
    private final int MOVE_SPEED = 4;
    private boolean jumping = false;
    private int vSpeed; 
    
    public void act() 
    {
        updateShouldScroll(50, 400); 
    }
    
    public void updateShouldScroll(int minX, int maxX)
    {
        if(getWorld().getObjects(Player.class).size() > 0)
        {
            MyWorld theWorld = (MyWorld)getWorld();
            Player player = (Player) theWorld.getPlayer();
            playerX = player.getX();
            if(playerX <= minX || playerX >= maxX)
            {
                shouldScroll = true;
            }
            else
            {
                shouldScroll = false;
            }
            if(shouldScroll == true && (playerX >= maxX && Greenfoot.isKeyDown("left")) || (playerX <= minX && Greenfoot.isKeyDown("right")))
            {
                shouldScroll = false;
            }
        }
    }
    
    //Object Methods
    public void checkKey()
    {
        MyWorld theWorld = (MyWorld) getWorld();
        WorldScroll ws = (WorldScroll)theWorld.getWorldScroll();
        if(Greenfoot.isKeyDown("left") && ws.shouldScroll == false)
        {
            move(MOVE_SPEED * -1);
        }
        else if(Greenfoot.isKeyDown("right") && ws.shouldScroll == false)
        {
            move(MOVE_SPEED);
        }
        else if("z".equals(Greenfoot.getKey()))
        {
            getWorld().addObject(new Bullet(), getX(), getY());
        }
    }
    
    public void jump()
    {
        Actor platform = getOneIntersectingObject( Platform.class );
        if (Greenfoot.isKeyDown("SPACE") && platform != null )
        {
            vSpeed = JUMP_HEIGHT;
            jumping = true;
            fall();
        }
    }
 
    public boolean enemyOnPlatform()
    {
        Actor under = getOneObjectAtOffset(0, 
        getImage().getHeight()/2, Platform.class);
        
        return under != null;
    }
 
    public void checkLand()
    {
        if (enemyOnPlatform())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    public void enemyJump()
    {
        Actor platform = getOneIntersectingObject( Platform.class );
        if (platform != null )
        {
            vSpeed = JUMP_HEIGHT;
            fall();
        }
    }
    
    public int getMoveSpeed()
    {
        return MOVE_SPEED;
    }
        
    //Platform Methofds
        
        public boolean platformAbove()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/-2);
        Actor ceiling = getOneObjectAtOffset(0, yDistance, Platform.class);
        if(ceiling != null)
        {
            vSpeed = 1;
            bopHead(ceiling);
            return true;
        }
        else
        {
            return false;
        }
    }

    public boolean checkRightWalls()
    {
        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());

    }

    public boolean checkLeftWalls()
    {
        int spriteWidth = getImage().getWidth();
        int xDistance = (int)(spriteWidth/-2);
        Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class);
        if(leftWall == null)
        {
            return false;
        }
        else
        {
            stopByLeftWall(leftWall);
            return true;
        }
    }

    public void stopByLeftWall(Actor leftWall)
    {
        int wallWidth = leftWall.getImage().getWidth();
        int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2;
        setLocation(newX + 5, getY());
    }

    public void bopHead(Actor ceiling)
    {
        int ceilingHeight = ceiling.getImage().getHeight();
        int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
    }

    public void fall()
    {
        setLocation(getX(), getY() + vSpeed);
        if(vSpeed <=9)
        {
            vSpeed = vSpeed + FALL_SPEED;
        }
        jumping = true;
    }

    public boolean onGround()
    {
        int spriteHeight = getImage().getHeight();
        int yDistance = (int)(spriteHeight/2) + 5;
        Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, Platform.class);
        if(ground == null)
        {
            jumping = true;
            return false;
        }
        else
        {
            moveToGround(ground);
            return true;
        }
    }

    public void moveToGround(Actor ground)
    {
        int groundHeight = ground.getImage().getHeight();
        int newY = ground.getY() - (groundHeight + getImage().getHeight())/2;
        setLocation(getX(), newY);
        jumping = false;
    }

    public void checkFall()
    {
        if(onGround())
        {
            vSpeed = 0;
        }
        else
        {
            fall();
        }
    }
    
    private void turnTransparent(GreenfootImage img)
    {
        Color transparent = new Color(0, 0, 0, 0);
        for(int x = 0; x < img.getWidth(); x++)
        {
            for(int y = 0; y < img.getHeight(); y++)
            {
                if(img.getColorAt(x, y).equals(Color.WHITE))
                    img.setColorAt(x, y, transparent);
            }
        }
    }
}

OBJECT SCROLL (ACTOR)
import greenfoot.*;  

public class ObjectScroll extends WorldScroll
{
    private int scrollX; 

    public ObjectScroll()
    {
        scrollX = 0;
    }

    public void setScrollLocation()
    {
        setLocation(getX() + scrollX, getY());
    }
    
    public void checkKeyPress(int amount)
    {
        MyWorld theWorld = (MyWorld) getWorld();
        WorldScroll ws = (WorldScroll)theWorld.getWorldScroll();
        if(Greenfoot.isKeyDown("left") && ws.shouldScroll == true)
        {
            scrollX = amount;
        }        
        if(Greenfoot.isKeyDown("right") && ws.shouldScroll == true)
        {
            scrollX = -amount;
        }
        else
        {
            scrollX = 0;
        }
    }
    
    public void scrollingMethods()
    {
        checkKeyPress(getMoveSpeed());
        setScrollLocation();
    }
}
What am I doing wrong?!
You need to login to post a reply.