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

2016/5/3

Objects dn't spawn in a scrolling world

TheBigBossJoshi TheBigBossJoshi

2016/5/3

#
Hi, I am asking this question specifically to Danpost, for I have seen he is an expert at the scrolling scenarios, but anyone can answer! I have a created a scrolling world, and I have a loop which spawns platforms. It looks like this:
for(int i = 0; i < 30; i++)
    addObject(new Platform(), i * SPAWN_DISTANCE, 390);

//Spawn Distance  = 115

As i gets bigger, the value gets way bigger than 600, my Screen's Width. But the platforms continue to spawn. But when I say something like
addObject(new Enemy(), 1250, 250);
This does not work. What is going wrong here? If you need, I can post my World and Scroll Classes. Thank you very much!
danpost danpost

2016/5/4

#
First, I would like to see your 'super' call in your World class constructor and the constructor(s) of its superclass if it is not that of World.
TheBigBossJoshi TheBigBossJoshi

2016/5/4

#
Yes sir: This class extends World
    public MyWorld()
    {    
        super(600, 400, 1, false); 
        
        addThings();
        le = new LevelEnd();
        
        thePlayer = new Player();
        addObject(thePlayer, 100, 20);
        
        ws = new WorldScroll();
        addObject(ws, 0, -10);   
        
        sound = new GreenfootSound("contra1.wav");
        sound.play();
    }


//This is the addThings method used above
    public void addThings()
    {
        addObject(new AirDrop(), 50, 20);        
        addObject(new Instructions(), 300, 100);
        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 = 0; i < 10; i++)
        {
            addObject(new Wall(), 50 + (i * 500), 350);
        }     
    }
TheBigBossJoshi TheBigBossJoshi

2016/5/4

#
Just In case, Here is the world scroll class of which the above class makes an object.
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;
            }
        }
    }
}
danpost danpost

2016/5/4

#
What is needed is the code in which you had tried to add the Enemy object into the world. Without it, how can it possibly be fixable! If you used the exact line above, at least indicate where you tried to use that line in your MyWorld class.
TheBigBossJoshi TheBigBossJoshi

2016/5/4

#
Hi Danpost, I removed that line of code because it does nothing at all. In fact, the majority of my program depends on my constructor being able to place objects way out of bounds, but apart from the platforms, it does not work! I had put that line in my constructor though. Thanks for the quick replies btw!
danpost danpost

2016/5/4

#
Because you are using an Actor object for the scrolling, it may be that the enemy is being create and added, but behind the scroll actor. Add this line to the world constructor and test it out:
setPaintOrder(Enemy.class);
TheBigBossJoshi TheBigBossJoshi

2016/5/4

#
Hi, Now the object spawns at the very edge of the current screen, still not where intended..
danpost danpost

2016/5/4

#
TheBigBossJoshi wrote...
Now the object spawns at the very edge of the current screen, still not where intended..
From what was given, I cannot see that as happening. If you could, upload the scenario with the 'Publish source code' checkbox checked so I (we) can take a look at it.
TheBigBossJoshi TheBigBossJoshi

2016/5/4

#
Ok, I will upload it in a few minutes. I have made some changes, and I no longer want my enemy to spawn in a certain place, instead, I want the LevelEnd (Helicopter) to do so. Not a big change, just thought I would let you know.
TheBigBossJoshi TheBigBossJoshi

2016/5/5

#
Hello, unfortunately, my scenario is too large to upload. Can I email you a project file?
You need to login to post a reply.