Game Development Community

Match 3 code available as resource

by Isaac Barbosa · in Torque Game Builder · 08/17/2008 (12:28 pm) · 58 replies

Hello,

I have programmed a prototype (only one level of match 3 recognition) of a 8 x 8 grid match 3 game. I'm wondering if

1. May I be able to upload a TGB project as resource including source code? -question to GG staff-
2. Is somebody interested in getting this file?

Let me know to upload it.

PS. The only negative point is that the code is almost not commented since I still have the da habit of not comment my scripts.
#21
09/27/2009 (1:52 pm)
Yep. He said he had to wait for Michael perry to give him access to upload the graphics in the post above.
#22
09/27/2009 (2:34 pm)
A couple of issues with this...

The "board" wasn't loading. I had to make a new tileMap, name it "grid" and change the class to "Board". I also changed the tilesize to 6 to match the size of the gems.

Clicking on a gem as it's moving will make it stop moving and permanently messes up. To prevent this it may be good to disallow mouse input while a move is going on. At least for gems that are moving.

I'm able to move some gems diagonally, but some don't. I'm not sure if this was intended or not but it'd be nice to either prevent diagonally moves or allow all the gems to move diagonally.
#23
09/27/2009 (8:16 pm)
You're right, it has to be named Grid. I forgot to mention about the tileMap. Thanks. Will edit the tutorial asap.

Haha, and that's right, you could swap gems diagonally, but I'm sure you can easily prevent that from happening yourself. But swapping gems diagonally could result in some new gimmick - has this been tried before in the gamut of casual games released?

Um... Now, how does one directly email to Michael Perry without him having to look through comments on his blog posts?
#24
09/27/2009 (8:33 pm)
OK, Joe. Changes done to disable diagonal movement as well as disabling of the mouse while the gems are moving.

Any other glaring things to report?

And by the way, the original graphics can be downloaded from Isaac's link found earlier in this discussion. That is until I get in touch with Michael.

Thanks. Hope it helps all understand TGB better.
#25
09/28/2009 (2:49 pm)
Thanks Gavin!

With your latest changes the matching works as expected of a typical match3, no diagonal movements, and that bug where I was able to click and move a moving gem seems to be fixed.. awesome!

Another thing I forgot to mention earlier...the board seems to start off with matches in place, that clear only after you make your first match.

The problem is in Board::onLevelLoaded. The gem class doesn't have a "checkmatches" function, the grid does. So this essentially does nothing.
//Schedule the following functions 10 milliseconds later
//%this.pieces[%a,%b].schedule(10,checkmatches);

To remedy the situation I added this call to the very end of the Board::onLevelLoaded function.

//Clear any initial matches before we let the player see the board
grid.schedule(10,checkmatches);

And voila, now the board starts off without any preexisting matches in place.
#26
09/28/2009 (4:07 pm)
Actually I'm finding that moving gem bug still persists..if you just click all over the place and click on moving gems too, you should hopefully see what I'm talking about.

Lol I just added a particle effect for the matched gems with 3 lines of code :) Looks a lot cooler with that in there. At the end of the Board::checkmatches function in the last for loops, change this part:

for (%i = 0; %i < %this.getTileCountX(); %i++)
  {
    for (%j = 0; %j < %this.getTileCountY(); %j++)
    {
      if (%this.pieces[%i,%j].match $= true)
      {
        $matchmade = true;
        %this.pieces[%i,%j].match = false;

        //ADD THIS :)
        //Show a particle effect  
        %boom = matcheffect.clonewithbehaviors();
        %boom.setEffectLifeMode( KILL, 5.0 );
        %boom.setposition( %this.pieces[%i,%j].getposition() );
        %boom.playEffect();

     (etc) ...

Oh and if you want to try this, don't forget to drag a particle effect into your level, make it of the effect mode "infinate" and name it "matcheffect".
#27
09/28/2009 (8:42 pm)
Ok, added the grid schedule thing to the tutorial. As for gems moving all over the place, LOL... let me try that out myself - I am using a lousy touchpad and don't have the agility to go tapping all over the place; will switch to a mouse for that.

If anyone has a solution, please go ahead and edit the tutorial.

This truly has been a fun experience!

Thanks.
#28
09/29/2009 (12:45 am)
I think what has to be done is disabling mouse clicks for the swapping gems and also all gems near those gems. Because if you swap one gem with another, then immediately swap the one on the opposite side of the one being swapped into into that gem, it causes an issue... I know it may be hard to follow that lol but hopefully I'm not the only one experiencing this...

Here is what I did to fix the issue. Note this will prevent players from moving ANY gems while a swap is taking place... some match games let you go crazy swapping things all over the board but this would force them to go one move at a time..anyway, beats the bug so here goes:

All the fixes happen in gems.cs

Add this function anywhere in gem.cs, I put mine at the very top.
//Used to prevent multiple swaps from occurring at the same time
function swaprelease(%this){ 
    $justswapped = false;
}

in function gem::onMouseDown add this at the top:
if ( $justswapped ) return;

in function gem::onMouseEnter add this at the top:
if ( $justswapped ) return; //Prevent multi swaps
also add this right above the call to %this.moveTo
$justswapped = 1;

in function gem::onpositiontarget find this line and add the call below it:
//aabracadabra - make the gems re-appear 
    $swappreviousGem.setsize( $gemsize , $gemsize ); 
    swaprelease();  //Prevent multi swaps
#29
09/29/2009 (1:02 am)
With that hopefully out of the way I'd like to point out another bug... when you make matches, sometimes the game will automatically generate a match in the spaces where you just matched some gems. So you are sitting there looking at 3 or 4 gems that match and aren't being triggered as a match :/ What I did to fix this was add another call to checkmatches inside the checkmatches function. This seems to have fixed the situation.

...
  %this.schedule(10,resize);
  %this.schedule(10, checkmatches); //add me
  $SCORE+=10;
  ...
#30
09/29/2009 (1:51 am)
Another bug.. it seems to be possible to "skip" gems, like if you are fast enough, you can swap a target gem with another gem 2 or even 3 gems away from it. I know this is just a simple example but something like that should probably be fixed. I'm not sure how to do that just yet.
#31
09/29/2009 (2:12 am)
So I found out how to fix this too. Pretty simple. Just look at the distance between the gems being swapped and if it's more than one gems worth of distance do not perform the swap.

NOTE I created a global called $gemsize and set it to %this.getTileSizeX() in Board::OnlevelLoaded.

In the function gem::onMouseEnter
...

    else
    {
      $swap = false;
      %speed = 20;
      
//Ensure the gems we are swapping are actually next to each other
if ( t2dVectorDistance( %this.getPosition() , $swappreviousX SPC $swappreviousY ) > $gemsize + 1 )
      {  error("gem swap malfunction!" SPC  t2dVectorDistance( %this.getPosition() , $swappreviousX SPC $swappreviousY ) );
         return; 
       }

 %this.moveTo($swappreviousX, $swappreviousY, %speed, true, true);  
 $swappreviousGem.moveTo($swapforthisX, $swapforthisY, %speed, true, true); 
    }
#32
09/29/2009 (7:50 am)
Wow, great stuff.
#33
09/29/2009 (10:56 am)
If i wanted to recreate this in 3d, what would i need to transfer over? I see few command calls that arent in the 3d version, but id like to look at creating a few mini games, thoughts?
#34
09/29/2009 (11:08 am)
I noted a bug while i was playing, the gems move offset in their "socket" and ghost out, but they still can be moved, any ideas?
#35
09/29/2009 (12:53 pm)
I think the bug you're referring to has been fixed by myself in the last few posts, glad to see I wasn't the only one who noticed that. To make it 3D you'd just have to make the gems as 3D objects instead of sprites in the Board::OnlevelLoaded function. The way the match3 code works doesn't care if the objects are 2D or 3D. You could also put 3D objects all around the board and in the background if you'd like. The sky is the limit :)
#36
10/02/2009 (9:18 pm)
Hey, i have a question, would any one of you guys be interesting in doing a 3d version of this with TGE instead of TGB? I have a project im working on, and i want to include this as a kinda minigame, but need to look into converting it? Interested.

Give me a email at racs333@hotmail.com.
#37
10/11/2009 (11:25 pm)
The links to the image and source code on TDN are dead links with no content.

Where are the source files described in this tutorial?

Image:Gems.zip
From TDN

No file by this name exists, you can upload it
Links

There are no pages that link to this file.
#38
11/02/2009 (8:52 am)
Go to the start of this thread and follow the link there. The graphics can be obtained from there. As for the code, just cut and paste it.

GG told me that there is no way for me to upload the code:

QUOTE -

"If you are putting up a TDN page, we cannot unlock the file upload system. TDN has been damaged by some malware, so we are unable to turn it back on. You can still host the images externally using the wiki tag [http://www.google.com Google]. Make sure you use the http:// , or else the image will not load. You just need a place to host the image files."

Heck... or, email me at necromaster@yahoo.com.
#39
11/25/2009 (12:49 am)
@Joe...

Dude, you have mention to add code to check that gems we are swapping are actually next to each other, is not working. Once I added these 2 lines, After mouse enter, it does nothing. First time it gives erroe "gem swap malfunction 6" and after second time onwards that error message also does not come and nothing happing also.


thnx


#40
11/25/2009 (12:51 am)
@ Gavin..

I have tried your tutorial . Its working fine after fixing some bugs mentioned by Joe too. I was wondering if we can give gravity feature to gems, how it will be. Any idea how we can implement gravity feature like conventional match 3 game.


Any idea pls.

thnx