How do I create a box that fills it with smaller boxes

0 favourites
  • 10 posts
From the Asset Store
An educational game for Fill in the Blanks. An easy to use template for developers to build larger games
  • So I am trying to get an empty box fill up one by one with smaller boxes and have a small space between them.

    It is a 4x4 box so there should be 16 small boxes inside. How do I make this?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I'm really not completely sure what it is you are asking. It would help if you explained a little more what you are trying to accomplish and what part you are having trouble doing.

    That being said, I threw together a quick example of what I think I understand your question to be. This example uses a mouse click to spawn a filler object. For the object to be spawned, the mouse must be over the background square. The background is 400 pixels and each filler box is 90 pixels. This will give you the space between you are looking for. The system also makes certain there is only 1 filler object at each location. It allows a second to be created but immediately removes it if there is already one where it spawns. It also clears filler boxes on right click.

    Pay particular attention to how the system picks where the filler is spawned.

    As I said, I am not completely sure I know what it is you are looking for but, hopefully this example gets you what you need to start. Good luck with your project.

  • That is nice but I want it to happen when an object gets destroyed and without having to mouse over the area so it would automatically spawn.

  • That's fine, all you need to do is change the event for spawning the filler object to the destruction of the object you are counting. You will want a variable to store the number of items spawned. Then, calculate the X and Y values from the number of items spawned, not the position of the click. My example should show you everything else you need.

    If the problem you are having is trying to figure out how to get the correct position of the object, you could use the following:

    X:

    ((NumberSpawned % 4) * Filler.Width) + Background.X - (Background.Width / 2) + (Filler.Width / 2)

    Y:

    ((floor(NumberSpawned / 4)) * Filler.Height) + Background.Y - (Background.Height / 2) + (Filler.Height / 2)

    In the above, the 4 represents the number of positions vertically or horizontally you want to fill. In your original question, you said you wanted a 4X4 fill area.

    As I stated in my first post, the question you asked did not explain enough about what you wanted to know. To get good answers in the forum, you need to be able to ask good, clear, descriptive questions. That way, those of us willing to help will know exactly what it is you need help doing.

    Once again, good luck with your project.

  • Ok sorry I will try to be more specific and clear in the future.

    This is so close to what I need but I cant get it to work properly as it starts creating a box a little outside the top-left corner and the 2nd spawns where the 3rd one should be but from there it goes like intended (Sort of) still needs to be placed right.. FYI the cargobox is 100x100px and the boxunit is 20x20px.. I have attached screenshots of what it looks like now with your code.

    Hopefully you can help me one last time to get it right and I sincerely appreciate your efforts. Thanks a lot.

  • I know the size of the units are smaller than they need to be but that is because i want free space around them, I tried making them fit perfectly in size but the problem still occurs with the spawning being off at the beginning.

    Anyone know what might cause this?

  • See the new attached example. It should solve the issue. When a red box is destroyed, it creates a blue box in the correct position. Also, it is built so you can completely change the size of the cargo box and (as long as it is a multiple of 20) it will create enough blue boxes to fill it. As for keeping a space between each object, You either need to add an offset of half the space you want between or give the sprite a transparent border. The way I did it was I gave my cargo image a 1 pixel transparent border. This allowed me to have a 20X20 sprite with only an 18X18 colored square. So, if you want your BoxUnits to be 20X20 items but separated by a few pixels, you could give each one a transparent border or, add an offset to the BoxUnits.

    On a side note, this question was a lot more specific. Thank you.

  • Works better now but sadly it still spawns the first one halfway out of the box..no idea why

    Thanks for taking your time to help

  • At this point, I would have to take a look at your project to know what is going on. I am still willing to help you solve this if you are willing to post the .capx file.

    [Edit]

    You know, I believe I just figured out the problem. Are you deleting the beginning BoxUnit on start of layout? If so, then when the system is trying to reference BoxUnit.Width or BoxUnit.Height, there is no object to reference so the value is 0. After the first BoxUnit is spawned in the wrong position, all the rest can reference it's size to get their positions. For my examples, I have a BoxUnit object in the layout but off the screen. The system is getting the size information from this object so my first visible BoxUnit is set correctly. I opened my project and created this event to test if it would give your results and it did.

    There are 2 ways to fix this:

    1: Don't delete the off-screen BoxUnit.

    2: If you need to delete the off-screen BoxUnit for some reason, create each BoxUnit at an arbitrary location (such as position 0,0) and then use the above calculation to set the position of the newly created object.

    The first option would be the easiest because it only requires removing 1 action. The second, you would have to add a new action positioning the new object. This action would have to happen in the same event as creating the object and would have to happen after the create action.

    I am fairly confident that if you make 1 of the above changes your project will work properly.

    Again, good luck with your project.

  • Ah yes the Destroy outside layout was indeed causing this issue, now it works as intended

    Thank you so much FragFather!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)