Shuffle Grid Demo

Drag & Drop the images to shuffle them around…

A friend of mine, Dale Sattler over at No Ponies recently posted a grid sorting class. He said it was based on my old dynamic stacking menu; but that was pure modesty as his script is based on a 2D grid and much more sophisticated.

Anyway – it inspired me to create a grid sorting / shuffling algorithm of my own. It behaves differently to Dales but I think it works pretty well.

The basic logic of the algorithm works by determining how far a dragged item travels along the grid (rows and columns) and then shuffling the surrounding items in the opposite direction by these values, therefore creating space for the dragged item to slot into when released.

It currently only works for grids which are fully populated, but adding more flexibility for incomplete grids will only be a matter of adding a few conditions – functionality which I’ll add when I get a moment.

You can extend the ShuffleGridItem class to add the functionality you need. In the demo I’ve made a simple image class which grabs a photo from Flickr.

Creating a ShuffleGrid is very simple, and can be done like so:

var grid:ShuffleGrid = new ShuffleGrid(4,7,40,40,1);

for (var i : int = 0; i < grid.numCells; i++)
{
	grid.addItem (new ShuffleGridItem());
}

addChild (grid);

The constructor takes the following arguments:

new ShuffleGrid(rows, cols, cellWidth, cellHeight, spacing);

Feel free to download the class and a simple demo and have a play around. I can imagine many uses for it, so hopefully it will come in handy.

Download: Shuffle Grid Class & Demo
Posted on 04 Mar 2009
41 Comments
6 Trackbacks

Meta

AS3 Shuffle Grid Class was posted on March 4th 2009 in the category Code / Actionscript 3.0, Code, Flash, Lab, Open Source and tagged; , , , , , , .

You can Leave a comment.

Twitter <follow>

March 18th 2010 - 5:29pm

We've missed you Morris: RT @memotv: This guy is nuts. New trailer for Chris Morris' jihadist comedy Four Lions http://is.gd/aNzki

Discussion

47 Responses to AS3 Shuffle Grid Class

Leave a Reply

Pingbacks / Trackbacks

  1. 1 year ago Bookmarks for March 5th from 15:03 to 15:06 « what i say // jon burger

    [...] AS3 Drag and Drop Shuffle Grid Menu Interface – [...]

  2. 10 months ago My First Game | PUBLIC SERVICE

    [...] soulwire’s Shufle Grid Class we present you PUZZLE NO [...]

  3. 10 months ago AntScript » 一个不错的效果收藏

    [...] 原文地址:http://blog.soulwire.co.uk/flash/flash-interfaces/as3-drag-drop-shuffle-grid-menu/#more-311 [...]

  4. 9 months ago Adobe — наш верный друг » AS3 Shuffle Grid Class — блог о air, flash, flex и других технологиях Adobe

    [...] делится своим видением области применения данной штуки. Можете поддержать [...]

  5. 1 month ago [Actionscript 3] Menu tipo iPhone con icone spostabili

    [...] ricerca.. ecco dove l’avevo visto noponies AS3 Drag Sort Grid Experiment e qui ce n’

  1. junyflip 10 months ago

    I love this….Thanks for posting…
    Is there any help that you can provide with placing the images in the rectangle. That would help me out alot as this code is exactly what i needed.

    Reply to this comment

  2. junyflip 10 months ago

    I love this….Thanks for posting…
    Is there any help that you can provide with placing the images in the rectangle. That would help me out alot as this code is exactly what i needed.

    Reply to this comment

  3. joe 10 months ago

    i couldn’t figure out how to place images in the rectangle either. please help!! this is exactly what i need!! thanks in advance! i love your website!! it’s awesome!!

    Reply to this comment

  4. Endoplasmic 10 months ago

    Hey Justin,

    First off this is a really fantastic little tid-bit of code.

    I was curious though… I have a situation where I only have 1 column and rows that may be of different sizes. Is there a way to get around having to use _rowHeight when calculating the current cell? I thought about using the _currentItem, but then if a row above that one is smaller the cell will report back too early (before getting to the row it will report that it has already got there).

    I hope that makes sense and that you possibly have a quick fix. (I saw what you mentioned above, but I think I don’t know your code well enough yet to figure that bad boy out).

    Reply to this comment

  5. Dave Stewart 9 months ago

    Wicked.

    Just completed a reorderable row of cells of my own, then thought “this would be cool as a grid!”, and wondered what what out there already. Typed in “AS3 shuffle grid”, and here we are!

    Going to check it out now

    :D

    Reply to this comment

  6. Dave Stewart 9 months ago

    Mainly, love it.

    Can think of a couple of improvements though:

    1) Don’t force users to extend from ShuffleGridItem. Either have items implement an interface, say iShuffleable, or just ensure that a DisplayObject is passed. Otherwise, if their items are already extending another class, they can’t use the
    2) restrict dragging to the grid area using a bounds object on startDrag(), and remove the onItemRelease() handler when you mouseOut of an item by dragging too far

    Nice, surprisingly clean, class though!

    I’m just working on having unshuffleable items, for example the job I’ve literally just finished we needed a timeline of video items, but we always had the last 2 items fixed. Was easy at the end of a 1D row but will be interesting to see how it works in 2D!

    Cheers,
    Dave

    Reply to this comment

  7. Mark 9 months ago

    Really cool piece of code… I understand a bit of Flash, but my action script knowledge is really bad.
    Maybe you could give as some instructions how to add images to the demo version?

    Preferably I would like to load all the images by xml. Maybe your flickr demo here could be of some help?

    Hope to hear from you !

    Cheers !

    Reply to this comment

  8. luke 8 months ago
  9. luke 7 months ago

    Hi
    How can i access the current position of each item? it seems as if getItemAtPosition and getItemAt always return the same values.
    after after re-ordering, i am trying to loop over the grid and find the current position or index of each item.

    thanks
    lm

    Reply to this comment

  10. Nicholas 4 months ago

    Hello,
    I tried to tweek al little the code so that each shufflegrid item grows bigger on each rollover and back to normal on the rollout event. The shufflegrid behaves very very very abnormally. Any ideas why is that so, or how can I fix that issue?

    Thank you, very nice class indeed!

    Reply to this comment

  11. drewm 4 months ago

    Great tool! But one bug I noticed is that if you click on a tile, then drag it with your mouse quickly, the tile drops.

    Reply to this comment

  12. Fuzu 4 months ago

    Ooh shoot i just typed a big comment and as soon as i hit reply it come up blank! Please tell me it worked properly? I do not want to write it again if i dont have to! Either the blog glitced out or i am just stuipd, the second option doesnt surprise me lol.

    Reply to this comment