This site requires JavaScript, please enable it in your browser!
Greenfoot back
Back to discussions
darkstargamer
darkstargamer wrote ...

2017/11/11

objects not spawning in world

darkstargamer darkstargamer

2017/11/11

#
Ok, so about a week or two ago, I posted about a problem with world generation, and here I am again, posting about yet another error. I'm trying to make a java version of Mario bros, and when I compile and shift the screen, only QuestionMarkBlock and GroundBlock spawn, and I can't get the other two (BrickBlock and HillA) to spawn. the world's code, which I'll show here, is supposed to identify specific colors on a map and spawn objects if it detects those colors on the map. before we begin, I'd like to mention that while there is a GroundBlockMap actor, it has no code. here's the world's code:
import greenfoot.*;
import java.awt.Color;
import java.util.List;
import java.util.ArrayList;
public class Sky extends World
{
    GroundBlockMap map = new GroundBlockMap(); //new groundblock map
    GreenfootImage mapImg = map.getImage(); 
    final int MAPIMGWIDTH = mapImg.getWidth();
    final int MAPIMGHEIGHT = mapImg.getHeight();
    GroundBlock groundBlockTemplate = new GroundBlock(0,0); //groundblocktemplate placed offscreen
    BrickBlock brickBlockTemplate = new BrickBlock(0,0); //brickblocktemplate placed offscreen
    QuestionMarkBlock questionMarkBlockTemplate = new QuestionMarkBlock(0, 0); //questionmarkblocktemplate placed offscreen
    HillA hillATemplate = new HillA(0, 0); //hilla template placed offscreen
    GreenfootImage pfImg = groundBlockTemplate.getImage(); //gets image of groundblock
    GreenfootImage bbImg = brickBlockTemplate.getImage(); //gets image of brickblock
    GreenfootImage qmbImg = questionMarkBlockTemplate.getImage(); //gets image of questionmarkblock
    GreenfootImage haImg = hillATemplate.getImage(); //gets image of hilla
    final int GROUNDBLOCKHEIGHT = pfImg.getHeight(); //the height of the groundblock
    final int GROUNDBLOCKWIDTH = pfImg.getWidth(); //the width of the groundblock
    final int BRICKBLOCKHEIGHT = bbImg.getHeight(); //the height of the brickblock
    final int BRICKBLOCKWIDTH = bbImg.getWidth(); //the width of the brickblock
    final int QUESTIONMARKBLOCKHEIGHT = qmbImg.getHeight(); //the height of the questionmarkblock
    final int QUESTIONMARKBLOCKWIDTH = qmbImg.getWidth(); //the width of the questionmarkblock
    final int HILLAHEIGHT = haImg.getHeight(); //the height of hill a
    final int HILLAWIDTH = haImg.getWidth(); //the width of hill a
    final int MAPWIDTH = MAPIMGWIDTH * GROUNDBLOCKWIDTH; //sizes the world's width, does not need counterparts
    final int MAPHEIGHT = MAPIMGHEIGHT * GROUNDBLOCKHEIGHT; //sizes the world's height, does not need couterparts
    private List<GroundBlock> theGroundBlocks = new ArrayList<GroundBlock>(); // thegroundblocks, arraylist for groundblock
    private List<BrickBlock> theBrickBlocks = new ArrayList<BrickBlock>(); //thebrickblocks, arraylist for brickblock
    private List<QuestionMarkBlock> theQuestionMarkBlocks = new ArrayList<QuestionMarkBlock>(); //theQuestionMarkBlocks, arraylist for questionmarkblock
    private List<HillA> theHillAs = new ArrayList<HillA>(); //the HillAs, arratlist for hilla
    int leftBound = 0;
    int bottomBound = MAPHEIGHT;
    int topBound = MAPHEIGHT - getHeight();
    int rightBound = getWidth();

    /**
     * Constructor for objects of class Sky.
     * 
     */
    public Sky()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(512, 480, 1); 
        makeMap();
        update();
        
    }

    public void makeMap()
    {
        for(int y=0;y<MAPIMGHEIGHT;y++)
        {
            for(int x=0;x<MAPIMGWIDTH;x++)
            {
                int colorRGB = mapImg.getColorAt(x,y).getRGB(); 
                /*NOTE: colors below are listed in the order seen when viewing the
                default colors through "edit colors" in ms paint */
                Color hillA = new Color(255, 128, 128); //scenery, hill tile 1, color 1
                Color hillB = new Color(255, 255, 128); //scenery, hill tile 2, color 2
                //Color hillT3 = new Color(0, 0, 255); //scenery, hill tile 3, Color 29
                //Color hillT4 = new Color(0, 0, 160); //scenery, hill tile 4, color 30
                //Color hillT5 = new Color(128, 0, 128); //scenery, hill tile 5, color 31
                //Color hillT6 = new Color(128, 0, 255); //scenery, hill tile 6, Color 32
                Color cloudA = new Color(128, 255,128 ); //scenery, one cloud, color 3
                Color cloudB = new Color(0, 255, 128); //scenery, two clouds, color 4
                Color cloudC = new Color(128, 255, 255); //scenery, three clouds, color 5
                Color bushA = new Color(0, 128, 255); //scenery, one bush, color 6
                Color bushB = new Color(255, 128, 192); //scenery, two bushes, color 7
                Color bushC = new Color(255, 128, 255); //scenery, three bushes, color 8
                Color questionMarkBlock = new Color(255, 0, 0); //block, ? block, color 9
                Color hardBlock = new Color(255, 255, 0); //block, hard block, color 10
                Color hiddenBlock = new Color(128, 255, 0); //block, hidden block, color 11
                Color brickBlock = new Color(0, 128, 64); //block, brick block, color 28
                Color mushroom = new Color(0, 255, 64); //powerup, mushroom, color 12
                Color star = new Color(0, 255, 255); //powerup, starman, color 13
                Color oneUp = new Color(0, 128, 192); //item, 1-up, color 14
                Color coin = new Color(128, 128, 192); //item, coin, color 15
                Color goomba = new Color(255, 0, 255); //enemy, goomba, color 16
                Color koopa = new Color(128, 64, 64); //enemy, koopa, color 17
                Color pipeA = new Color(255, 128, 64); //pipe, small, color 18
                Color pipeB = new Color(0, 255, 0); //pipe, medium, color 19
                Color pipeC = new Color(0, 128, 128); //pipe, large, color 20
                Color warpPipeA = new Color(0, 64, 128); //warp pipe, small, color 21
                Color warpPipeB = new Color(128, 128, 255); //warp pipe, medium, color 22
                Color warpPipeC = new Color(128, 0, 64); //warp pipe, large, color 23
                Color flag = new Color(255, 0, 128); //flagpole, flag, color 24
                Color pole = new Color(128, 0, 0); //flagpole, pole, color 25
                Color castle = new Color(255, 128, 0); //castle, castle, color 26
                Color flagCastle = new Color(0, 128, 0); //castle, flag, color 27
                Color mario = new Color(0, 0, 255); //mario, color 29
                if(colorRGB==Color.BLACK.getRGB()) //spawns groundblocks from the map
                {
                    int mapGBX = x * GROUNDBLOCKWIDTH + GROUNDBLOCKWIDTH/2;
                    int mapGBY = y * GROUNDBLOCKHEIGHT + GROUNDBLOCKHEIGHT/2;
                    theGroundBlocks.add(new GroundBlock (mapGBX, mapGBY));
                }

                if(colorRGB==brickBlock.getRGB())
                {
                    int mapBBX = x * BRICKBLOCKWIDTH + BRICKBLOCKWIDTH/2;
                    int mapBBY = y * BRICKBLOCKHEIGHT + BRICKBLOCKHEIGHT/2;
                    theBrickBlocks.add(new BrickBlock (mapBBX, mapBBY));
                }

                if(colorRGB==questionMarkBlock.getRGB())
                {
                    int mapQMBX = x * QUESTIONMARKBLOCKWIDTH + QUESTIONMARKBLOCKWIDTH/2;
                    int mapQMBY = y * QUESTIONMARKBLOCKHEIGHT + QUESTIONMARKBLOCKHEIGHT/2;
                    theQuestionMarkBlocks.add(new QuestionMarkBlock(mapQMBX, mapQMBY));
                }
                
                if(colorRGB==hillA.getRGB())
                {
                    int mapHAX = x * HILLAWIDTH + HILLAWIDTH/2;
                    int mapHAY = y * HILLAHEIGHT + HILLAHEIGHT/2;
                    theHillAs.add(new HillA(mapHAX, mapHAY));
                }
            }
        }
    }

    public void shiftScreen(int changeX, int changeY)
    {
        leftBound += changeX;
        rightBound += changeX;
        if(leftBound<0)
        {
            leftBound =0;
            rightBound = getWidth();
        } else if(rightBound >= MAPWIDTH)
        {
            rightBound = MAPWIDTH;
            leftBound = MAPHEIGHT - getWidth();
        }
        topBound -= changeY;
        bottomBound -= changeY;
        if(topBound<0)
        {
            topBound = 0;
            bottomBound = getHeight();
        } else if (bottomBound > MAPHEIGHT)
        {
            bottomBound = MAPHEIGHT;
            topBound = MAPHEIGHT - getHeight();
        }
        update();
    }


    public void update()
    {
        GroundBlock thisGroundBlock;
        BrickBlock thisBrickBlock;
        QuestionMarkBlock thisQuestionMarkBlock;
        HillA thisHillA;
        int thisGroundBlockX;
        int thisGroundBlockY;
        int thisBrickBlockX;
        int thisBrickBlockY;
        int thisQuestionMarkBlockX;
        int thisQuestionMarkBlockY;
        int thisHillAX;
        int thisHillAY;
        int screenGBX;
        int screenGBY;
        int screenBBX;
        int screenBBY;
        int screenQMBX;
        int screenQMBY;
        int screenHAX;
        int screenHAY;
        for(int gbi=0; gbi<theGroundBlocks.size(); gbi++)
        {
            thisGroundBlock = theGroundBlocks.get(gbi);
            thisGroundBlockX = thisGroundBlock.gbmapX;
            thisGroundBlockY = thisGroundBlock.gbmapY;
            if(thisGroundBlockX>=leftBound && thisGroundBlockX<=rightBound && thisGroundBlockY>=topBound && thisGroundBlockY<=bottomBound)

            {
                screenGBX = thisGroundBlockX - leftBound;
                screenGBY = thisGroundBlockY - topBound;
                if(thisGroundBlock.getWorld()==null)
                {
                    addObject (thisGroundBlock, screenGBX, screenGBY);
                } else
                {
                    thisGroundBlock.setLocation(screenGBX, screenGBY);
                }
            }else 
            {
                if(thisGroundBlock.getWorld()!=null)
                {
                    removeObject(thisGroundBlock);
                }
            }
        }

        for(int bbi=0; bbi<theBrickBlocks.size(); bbi++)
        {
            thisBrickBlock = theBrickBlocks.get(bbi);
            thisBrickBlockX = thisBrickBlock.bbmapX;
            thisBrickBlockY = thisBrickBlock.bbmapX;
            if(thisBrickBlockX>=leftBound && thisBrickBlockX<=rightBound && thisBrickBlockY>=topBound && thisBrickBlockY<=bottomBound)
            {
                screenBBX = thisBrickBlockX - leftBound;
                screenBBY = thisBrickBlockY - topBound;
                if(thisBrickBlock.getWorld()==null)
                {
                    addObject (thisBrickBlock, screenBBX, screenBBY);
                }else
                {
                    thisBrickBlock.setLocation(screenBBX, screenBBY);
                }
            }else 
            {
                if(thisBrickBlock.getWorld()!=null)
                {
                    removeObject(thisBrickBlock);
                }
            }
        }

        for(int qmbi=0; qmbi<theQuestionMarkBlocks.size(); qmbi++)
        {
            thisQuestionMarkBlock = theQuestionMarkBlocks.get(qmbi);
            thisQuestionMarkBlockX = thisQuestionMarkBlock.qmbmapX;
            thisQuestionMarkBlockY = thisQuestionMarkBlock.qmbmapY;
            if(thisQuestionMarkBlockX>=leftBound && thisQuestionMarkBlockX<=rightBound && thisQuestionMarkBlockY>=topBound && thisQuestionMarkBlockY<= bottomBound)
            {
                screenQMBX = thisQuestionMarkBlockX - leftBound;
                screenQMBY = thisQuestionMarkBlockY - topBound;
                if(thisQuestionMarkBlock.getWorld() == null)
                {
                    addObject (thisQuestionMarkBlock, screenQMBX, screenQMBY);
                }else
                {
                    thisQuestionMarkBlock.setLocation(screenQMBX, screenQMBY);
                }
            }else 
            {
                if(thisQuestionMarkBlock.getWorld()!=null)
                {
                    removeObject(thisQuestionMarkBlock);
                }
            }
        }
        
        for(int hai=0; hai<theHillAs.size(); hai++)
        {
            thisHillA = theHillAs.get(hai);
            thisHillAX = thisHillA.hamapX;
            thisHillAY= thisHillA.hamapY;
            if(thisHillAX>=leftBound && thisHillAX<=rightBound && thisHillAY>=topBound && thisHillAY<=bottomBound)
            {
                screenHAX = thisHillAX - leftBound;
                screenHAY = thisHillAY - topBound;
                if(thisHillA.getWorld() == null)
                {
                    addObject (thisHillA, screenHAX, screenHAY);
                }else
                {
                    thisHillA.setLocation(screenHAX, screenHAY);
                }
            }else
            {
                if(thisHillA.getWorld()!= null)
                {
                    removeObject(thisHillA);
                }
            }
        }
    }
}
as a sidenote, those other colors are simply for later, so don't mind them (unless they affect world generation) and here's code for each of these entities (don't worry, they're not nearly as long). first is the GroundBlock, which works:
public class GroundBlock extends Actor
{
    int gbmapX;
    int gbmapY;
    
    public GroundBlock(int getMapX, int getMapY)
    {
        gbmapX = getMapX;
        gbmapY = getMapY;
    }
}
second is the HillA, which doesn't work:
public class HillA extends Actor
{
    int hamapX;
    int hamapY;
    public HillA(int getMapX, int getMapY)
    {
        hamapX = getMapX;
        hamapY = getMapY;
    }
}
3rd is the BrickBlock which, again, doesn't work:
public class BrickBlock extends Actor
{
   int bbmapX;
   int bbmapY;
   
   public BrickBlock(int getMapX, int getMapY)
   {
       bbmapX = getMapX;
       bbmapY = getMapY;
   }
}
and last but not least, the QuestionMarkBlock, which works (surprisingly):
public class QuestionMarkBlock extends Actor
{
    int qmbmapX;
    int qmbmapY;

    public QuestionMarkBlock(int getMapX, int getMapY)
    {
        qmbmapX = getMapX;
        qmbmapY = getMapY;
    }
}
if anyone knows how to fix this, please let me know. I'm using greenfoot 2.4.2, if that helps. if there is a more efficient method I could use, please tell me, I would be interested in knowing. thank you!
danpost danpost

2017/11/12

#
I will supply what I think should do exactly what you currently have, but in a more compact way. Currently, your 'for' loops in the 'update' method are set up exactly the same, but for different lists. So, here goes. Try replacing lines 152 through 274 (the 'update' method) with the following:
public void update()
{
    updateListedActors(theGroundBlocks);
    updateListedActors(theBrickBlocks);
    updateListedActors(theQuestionMarkBlocks);
    updateListedActors(theHillAs);
}

public void updateListedActors(List<?> actors)
{
        for (int i=0; i<actors.size(); i++)
        {
            Actor actor = actors.get(i);
            int actorX = actor.getX();
            int actorY = actor.getY();
            if (actorX >= leftBound && actorX <= rightBound && actorY >= topBound && actorY <= bottomBound)
            {
                actorX -= leftBound;
                actorY -= topBound;
                if (actor.getWorld() == null) addObject (actor, actorX, actorY);
                else actor.setLocation(actorX, actorY);
            }
            else if(actor.getWorld() != null) removeObject(actor);
        }
}
danpost danpost

2017/11/12

#
I would cut and paste line 58 through 92 in the Sky class, inserting it back in at line 53. You might try inserting the following at line 122 as well:
update();
darkstargamer darkstargamer

2017/11/12

#
I'm getting an error in the updateListedActors method. do you put something in the List<?> in line 9, or do you create a separate arraylist variable? if this is a standard thing that programmers should know about, I'm sorry, I'm a java student, and it would be nice to know these things, for future reference.
danpost danpost

2017/11/12

#
darkstargamer wrote...
I'm getting an error in the updateListedActors method. do you put something in the List<?> in line 9, or do you create a separate arraylist variable? if this is a standard thing that programmers should know about, I'm sorry, I'm a java student, and it would be nice to know these things, for future reference.
I tested this before posting. If you are getting an error, you need to let us know what kind of error (copy and paste error message) and exactly where the error occurs. Btw, the 'actors' List object in line 9 is assigned the lists passed to it from the 'update' method calling lines. Notice that all four of your lists are provided as arguments in the calling statements.
danpost danpost

2017/11/12

#
It may be that line 13 is at fault. Try this as a replacement there:
Actor actor = (Actor)actors.get(i);
I do not see any other place where an error might occur.
darkstargamer darkstargamer

2017/11/12

#
Oh, sorry! I've specified where the error is in the code below.
public void update()
    {
        updateListedActors(theGroundBlocks);
        updateListedActors(theBrickBlocks);
        updateListedActors(theQuestionMarkBlocks);
        updateListedActors(theHillAs);
    }
    
    public void updateListedActors(List<?> actors)
    {
        for (int i=0; i<actors.size(); i++)
        {
            Actor actor = actors.get(i); //this is where I'm getting the error, at the actors.get(i) part
            int actorX = actor.getX();
            int actorY = actor.getY();
            if (actorX >= leftBound && actorX <= rightBound && actorY >= topBound && actorY <= bottomBound)
            {
                actorX -= leftBound;
                actorY -= topBound;
                if (actor.getWorld() == null) addObject (actor, actorX, actorY);
                else actor.setLocation(actorX, actorY);
            }
            else if(actor.getWorld() != null) removeObject(actor);
        }
    }
this is the error message I'm getting: incompatible types: capture#1 of ? cannot be converted to greenfoot.Actor Sorry for not specifying this earlier!
danpost danpost

2017/11/12

#
Okay, I thought that is where it might have been. You can do as suggested here; or, you can change line 9 to this:
public void updateListedActors(List<? extends Actor> actors)
darkstargamer darkstargamer

2017/11/13

#
now the world constructor is throwing an exception at me, which states this:
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:681) at greenfoot.Actor.getX(Actor.java:157) at Sky.updateListedActors(Sky.java:172) at Sky.update(Sky.java:161) at Sky.<init>(Sky.java:53) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:607) at greenfoot.platforms.ide.WorldHandlerDelegateIDE$4.run(WorldHandlerDelegateIDE.java:441) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:494) at greenfoot.core.Simulation.maybePause(Simulation.java:299) at greenfoot.core.Simulation.runContent(Simulation.java:212) at greenfoot.core.Simulation.run(Simulation.java:205)
I'm sorry to ask again, but can you please help? thank you!
danpost danpost

2017/11/13

#
First, you will need to change the name of some fields. Actually, to make things easier, an intermediate class between Actor and the four object types will be helpful. Add the following class to your project:
public class MapActor extends greenfoot.Actor
{
    protected int mapX, mapY;
    
    public MapActor(int x, int y)
    {
        mapX = x;
        mapY = y;
    }
    
    public int getMapX()
    {
        return mapX;
    }
    
    public int getMapY()
    {
        return mapY;
    }
}
Next, change the GroundBlock class to this:
public class GroundBlock extends MapActor
{
    public GroundBlock(int x, int y)
    {
        super(x, y);
    }
}
Change the other three classes (HillA, BrickBlock and QuestionMarkBlock) similarly. Finally, change lines 13 through 15 in the code I supplied to this:
MapActor actor = (MapActor)actors.get(i);
int actorX = actor.getMapX();
int actorY = actor.getMapY();
That should avert the error and accomplish your goal here.
You need to login to post a reply.