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

2012/5/13

static images - multiple tile sets

matt.milan matt.milan

2012/5/13

#
i have 5 images that i want to rotate and flip to make a total of 19. i want to put them in a GreenfootImage i want to fill the array something like the following
    public static GreenfootImage[] loadImages(String whichFolder)
    {
        GreenfootImage[] images = new GreenfootImage[TOTAL_MUTATIONS];
        images[0] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 0 + ".png");
        images[1] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 1 + ".png");
        images[5] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 2 + ".png");
        images[9] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 3 + ".png");
        images[10] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 4 + ".png");
        images[14] = new GreenfootImage("tileset\\" + whichFolder + "\\image" + 5 + ".png");            
        return images;

    }



    public static GreenfootImage[] expand(GreenfootImage[] imgs)
    {
        GreenfootImage[] expandedImages = imgs;

        imgs[0] = expandedImages[0];
        imgs[1] = expandedImages[1];
        imgs[2] = expandedImages[1];
        imgs[3] = expandedImages[1];
        imgs[4] = expandedImages[1];
        imgs[5] = expandedImages[2];
        imgs[6] = expandedImages[2];
        imgs[7] = expandedImages[2];
        imgs[8] = expandedImages[2];
        imgs[9] = expandedImages[3];
        imgs[10] = expandedImages[3];
        imgs[11] = expandedImages[4];
        imgs[12] = expandedImages[4];
        imgs[13] = expandedImages[4];
        imgs[14] = expandedImages[4];
        imgs[15] = expandedImages[5];

        imgs[2].rotate(-90);
        imgs[3].rotate(-180);
        imgs[4].rotate(-270);

        imgs[6].rotate(-90);
        imgs[7].rotate(-180);
        imgs[8].rotate(-270);

        imgs[10].rotate(-90);
        
        imgs[12].rotate(-90);
        imgs[13].rotate(-180);
        imgs[14].rotate(-270);

        
         return imgs;
        }
when call only loadImages, 5 of the tiles are the correct image. when i call expand on it, all of the tiles become the same. not sure if i'm missing something. i want to get it working before i condense it.
kiarocks kiarocks

2012/5/13

#
Is it being called a bit like
expand(loadImages("afolder"));
danpost danpost

2012/5/13

#
In lines 20 through 35, try this: imgs = new GreenfootImage(expandedImages) Also, instead of juggling the names (parameter imgs >> expandedImages >> imgs), which probably will cause an IndexOutOfBounds error, just use for lines 16 through 18:
public static GreenfootImage[] expand(GreenfootImage[] expandedImages)  
{
    GreenfootImage[] imgs = new GreenfootImage[16]; // 16 is what I see right now
matt.milan matt.milan

2012/5/13

#
thanks for the tip kia, i'm gonna implement that what the code looks like (what it does, after)
    public static void initializeImages()
    {
        //createDefaultPlatforms(color01,color02,imgToUse);
        imgLib0 = loadImages("grass");
        imgLib1 = loadImages("rock");
        imgLib2 = loadImages("dungeon");
        imgLib3 = loadImages("town");
        imgLib4 = loadImages("palace");

        imgToUse = imgLib0;
    }

    public static GreenfootImage[] loadImages(String whichFolder)
    {
        GreenfootImage[] images = new GreenfootImage[TOTAL_MUTATIONS];
        for (int i = 0; i < NUM_OF_IMGS; i++)
        {
            images[i] = new GreenfootImage("tileSet\\" + whichFolder + "\\image" + i + ".png");
        }
        return images;
    }

    public void chooseImage(String whichTile)
    {
        if (whichTile == "center") img = imgToUse[0];

        else if (whichTile == "tFace")
        {
            img = imgToUse[1];
            
        }
        else if (whichTile == "rFace") 
        {
            img = imgToUse[1];
        }

        else if (whichTile == "bFace") 
        {
            img = imgToUse[1];  // these four work, none else
        }
        else if (whichTile == "lFace")
        {
            img = imgToUse[1];
        }

        else if (whichTile == "TL")
        {
            img = imgToUse[2];
        }
        else if (whichTile == "TR") 
        {
            img = imgToUse[2];
        }
        else if (whichTile == "BR") 
        {
            img = imgToUse[2];
        }
        else if (whichTile == "BL")
        {
            img = imgToUse[2];
        }

        else if (whichTile == "horizontal")
        {
            img = imgToUse[3];
        }
        else if (whichTile == "vertical")
        {
            img = imgToUse[3];
        }

        else if (whichTile == "top")    
        {
            img = imgToUse[4];   
        }
        else if (whichTile == "right") 
        {
            img = imgToUse[4];   
        }
        else if (whichTile == "bottom")
        {
            img = imgToUse[4];                        
        }
        else if (whichTile == "left")   
        {
            img = imgToUse[4];         
        }
        else if (whichTile == "")
        {
            img = imgToUse[5];
        }

        setImage(img);
    }
}
    public void addedToWorld(World w)
    {
        Outline o = new Outline(imgToUse[5]); // the extra-dot square
        Outline o2 = new Outline(imgToUse[5]);

        int rotationResult = 0;
        if (myType != null) //not interested in some squares
        {       
            //blob of adding the 1 extra-dot square to the cornerpiece outline
            if (myType == "center")
            {
                //add four corner pieces. still need to do
            }
            else if (myType == "TL") 
            {
                //setRotation(0);
                getWorld().addObject(o,getX(),getY());
                o.setRotation(90);
            }

            else if (myType == "TR")
            {
                rotationResult = 90;                
                getWorld().addObject(o,getX(),getY());
                o.setRotation(180);                
            }

            else if (myType == "BR") 
            {
                rotationResult = 180;
                getWorld().addObject(o,getX(),getY());
                o.setRotation(270);                
            }

            else if (myType == "BL") 
            {
                rotationResult = 270;
                getWorld().addObject(o,getX(),getY());
            }
            else if (myType == "horizontal") {}//do Nothing
            else if (myType == "vertical") rotationResult = 90;
            else if (myType == "tFace") 
            {
                getWorld().addObject(o,getX(),getY());
                getWorld().addObject(o2,getX(),getY());    
                o.setRotation(180);
                o2.setRotation(90);
            }
            else if (myType == "rFace")
            {
                rotationResult = 90;                
                getWorld().addObject(o,getX(),getY());
                getWorld().addObject(o2,getX(),getY());       
                o.setRotation(270);
                o2.setRotation(180);                
            }
            else if (myType == "bFace") 
            {
                rotationResult = 180;
                getWorld().addObject(o,getX(),getY());
                getWorld().addObject(o2,getX(),getY());     
                o.setRotation(270);
                o2.setRotation(0);                
            }
            else if (myType == "lFace") 
            {
                rotationResult = 270;
                getWorld().addObject(o,getX(),getY());
                getWorld().addObject(o2,getX(),getY());      
                o.setRotation(0);
                o2.setRotation(90);                
            }
            else if (myType == "left"){}//do nothing, dont change it's rotation
            else if (myType == "top")   rotationResult = 90;
            else if (myType == "right") rotationResult = 180;     
            else if (myType == "bottom")rotationResult = 270;
            //Wrap-up code
            Outline myOutline = new Outline(getImage());    //important: passes proper image to outline
            getWorld().addObject(myOutline,getX(),getY());  // then rotates it
            myOutline.setRotation(rotationResult);
            setImage(imgToUse[0]);//platform passes its grass to outline, and becomes a block
        }
    }


trying to implement a "masking" system that will paint my blocks based on an integer choice of tile set. making 6 new images and adding code to a couple methods will allow all new images to be placed automatically. havent tested with a second set yet.
matt.milan matt.milan

2012/5/13

#
edit - http://www.greenfoot.org/scenarios/5119 scenario now works, had an issue with greenfoot not being able to find all the image files it needed to properly package the visuals.
davmac davmac

2012/5/13

#
Just to explain something else you may have missed, line 18 from your original post: GreenfootImage expandedImages = imgs; ... doesn't make a copy of the array, it just creates another reference to it. So the following lines: imgs = expandedImages; imgs = expandedImages; ... are either redundant (first line above) or modify the original array (second line above).
You need to login to post a reply.