flash as Q
- Started
- Last post
- 41 Responses
- stewart
maybe someone can help me a little with a function for this:
i have a number of movieclips. let's say 20.
every movieclip contains one black block - and they're all different in size.i want to put them one by one in random order on the stage until the space on the stage is filled like this:
in other words: when only 4 movieclips fits on the stage it shows 4 movieclips, when 8 movieclips fits... etc etc
and i don't want them to overlap each other...
how could this be done?
- qruise0
stage.width * stage.height = your real estate.
for each element {
blockspace += element.width * element.height;
if blockspace
- qruise0
fucknig moronic motherfucking newstoday...
if blockspace IS LESS THAN OR EQUAL TO your real estate {
make the block appear
}and as for the not overlapping part, im at work and dont have enough time to figure that out now.
- blaw0
to avoid the overlapping, do a hitTest to check for a collision, then adjust the _y and check again until the hitTest returns false.
- qruise0
ok, i have just enough time to say that doing a recursive function to constantly adjust the position is a ridiculously bad way of doing it
- rson0
just check the size of each MC then reset the x coord as they are placed on the stage
- blaw0
never too busy to spread a little joy though, 'eh, sunshine?
i'm really interested in hearing the solution, so be sure to post it here.
- enjine0
no, the recursive function is definitely the only way to go, if you're not griding out the blocks in a predictable order.
google "jared tarbell box fitting". he's got a great example of how to do it.
or use hitTest to move the boxes before displaying them (i.e. use a for loop instead of a time-based interval):
- st33d0
Box fitting expands until it hits other boxes and then shrinks the box a little. Those movieclips look like they want to be a specific size.
Don't use hittest. Take the size of your boxes and check overlap at each corner. If you have an overlap at that corner, then you know the direction to push the clip in.
Then you just let all the boxes shove at each other until all are free of each other.
If you calculate it based on the clipsize plus a bit extra then you can put a margin around each movie and it will look neater.
- stewart0
thanks for the reactions.
this is miles from the desired result, but this is as far as i can get with actionscript:
http://www.kulsdom.com/client/se…st33d, all my boxes have different sizes, but each size is fixed.
does that make it easier??
- stewart0
and here's the crappy script i just wrote with a little of google's help...
- Crouwel0
works nicely on resize, sjoerd.
- acescence0
i'm not going to write the code, but here's the logic:
place first block at top left
place next block, make x position the same as x of last object plus its width (and a margin)
if the distance to the right edge is less than the width of the present block, reset the x position back to left edge
set y position at top and keep bumping down until it clears hittest
repeat
- e-pill0
pete and repeat were walking down the street. pete ran away who was left?
- stewart0
"if the distance to the right edge is less than the width of the present block, reset the x position back to left edge"
that's it!
now i 'only' have to read a little about hittest.
thanks acescene!
- stewart0
too bad that you're not going to write the code here, acescene :)
- tomkat0
I'd put the MCs into an array, looping through it, getting every width and height of and position with that
with the logic of acescence above
:)
- stewart0
let's give it a bump. maybe there's someone who can help me a little with this.
- st33d0
I have an idea:
http://pastebin.coconut.se/?id=4…
not a great solution, but it shows you what solving your question involves.
- stewart0
that's a funky script st33d.
the biggest problem at the moment is that i have about 20 different sized boxes (but the sizes are fixed because of the content in it...)
i want to randomly fill the stage till there's no more space to fill.
about 8, 10, or 12 boxes are on the stage then.
i also have a "next" button that does the function again that randomly fills the stage till there's no more space to fill etc etc.
man, difficult to explain what i want to achieve...