I’m a newbie trying to make a game that has the traits of a bejeweled/matching game. How do I set up the gamespace to:
-
Drag and drop objects to make a sequence
-
Snap objects to predetermined coordinates when moved
-
Recognize that the objects are in a string/array to then have them deleted
I found a tetris example here on the forums but I think those codes will only take me so far in the matching game category.
I’d begin by making a colored cube prefab that will be used as your moveable tiles. This prefab would have a script attached to it that would contain all of its behavior (such as detecting when you click it for dragging purposes) as well as a variable that would keep track of what color the tile it is. There’s a lot of ways you could detect when different tiles adjacent to each other are matching, but perhaps the simplest way would be to have invisible collision boxes that are parented to and border your cube prefab on all four sides. You could use these collision boxes to determine at any time what game objects are adjacent to each other, and by checking their color variable, determine whether they should disappear.
At the beginning of a game, you would instantiate a grid of these tiles programmatically, randomizing the color variable on each. You’d also need to have some kind of ‘gravity’ as part of the prefab’s script, so that these tiles fall when tiles below them are cleared. Anyway, that’s how I’d begin, hopefully that gives you some ideas on how to approach what you’re shooting for.
Mr Animator – thats an interesting approach –
I personally took the approach that involves a static array in a class, which contains a reference to every ‘grid location’ and then i just keep track of what objects are in each grid location, and then have simple helper routines such as ‘checkLeft’ and ‘checkRight’, etc … which return a numeric value … the value being the number of objects in that direction that matched – so if ‘checkLeft’ returns 2, then there’s 2 objects to the left that match the ‘center’ object (center being the object in question – the one that was just moved, or just dropped down).
I run these check routines every time an ‘event’ occurs (an object is moved – which causes a single event to fire, which could then in effect, cause multiple events to fire after things are moved around).