Match 3

Embed Size (px)

Citation preview

  • 8/7/2019 Match 3

    1/21

    Hey! Well I am new here and I am also new to programming in flash as well. However, asidefrom that I was wondering if anyone could help me. Im working on a game the operates verysimilar to Peggle and Plinko, except that it is also a Match 3 type game.

    Heres a quick run down: The player drops a marble which bounces its way to the bottom.When 3 or more of the same color marbles are touching they should disappear and based on the #

    of Marbles points are added respectively. There are also little wedge like objects that periodicallypop up from the ground and can either help the player make a match or split the marbles up.Either way thats the basics. Box2D Provides the Physics.

    On to my Question: Can anyone help me figure out the best way to check if 3 or more samecolored marbles are touching? My first approach was to use nested for loops that ran through thearray of all marbles. It checked each marble against all others without repeating or checkingitself. Using Pixel perfect collision detection I looked for any marble that was touching 1 or moreof the same colored marble and added all of the marbles that were touching to an array. If thelength of the array was 3 or greater it would undergo the process of calculating points andeventually the removal of the marbles(for testing purposes i set it 2 instead of 3).

    This however proved quite buggy and i found myself for some reason almost never removingany marbles from the screen although the match showed true. in some cases a match of 2 marbleswould show true but only one marble would disappear and the other would have its image splitfrom the body. In either of the two cases it was highly noticeable that after about 20-25 marblesappeared on screen (although i dont foresee anyone to ever have that many except on highdifficulties) the performance slowed considerably.

    I love box2D from what i have done with it so far and i think a custom contact listener wouldwork much better i just dont know exactly how i should implement it so it works correctly. Ithink it would offer better performance but im not sure if it would be 100% better. LOL So far alli have learned from this is that saying remove three or more objects of the same color that aretouching is a lot easier than coding it.

    Any ideas or help would be much appreciated. I read through the Flash Game University chapteron Match 3. While it provided me the first used idea it didnt have the solution i required becausemy game doesnt operate on a grid. the marbles move freely. Thanks again for any help.

    ok a few quick notes:

    1) You dont have to check EVERY SINGLE marble every frame. Heres an alternative. For each marble, have something that tracks whether it has moved since the last check, andonly check the marbles that have moved. And then you would check to see what marbles that

    marble is touching by running through your array of marbles, and repeat. If Box2D depatches a Collision Event or something when two objects collide then that will bea great way to decide which marbles to check. But if the collision event keeps being dispatchedwhen two marbles are already touching and not moving around thats a bit useless.

    2) Im assuming your marbles are perfect circles/spheres. Then, for the marble vs marble hittestsyou can use complete code:

  • 8/7/2019 Match 3

    2/21

    if the distance between the centers of the marbles are smaller or equal to the sum of the radii ofthe marbles, they are colliding.But for the marble vs environment youll have to keep using pixel perfect

    As for your bugs. the first one sounds like you are forgetting to remove the other one orsomething, and the second one i just dont understand.

    And yeah the biggest problem is that your marbles move freely, which means you must loopthrough all of the marbles in order to see which ones are colliding with your marble. But with the2 changes I posted above you should notice quite a bit of decrease in lag.

    26,218 online playing 38,735 free games!

    Kongregate

    Top of Form

    Sign In orRegister

    Remember me

    Username Password

    Sign In

    Forgot password?

    Bottom of Form

    Part of the Gamestop NetworkTop of Form

    see more results {"url":"/search"}

    Search

    Bottom of Form

    Home

    Games

    Achievements

    Community

    Developers

    Help

    http://www.kongregate.com/http://www.kongregate.com/accounts/newhttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.gamestop.com/http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/http://www.kongregate.com/gameshttp://www.kongregate.com/badgeshttp://www.kongregate.com/forumshttp://www.kongregate.com/developer_centerhttp://www.kongregate.com/pages/helphttp://www.kongregate.com/accounts/newhttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.gamestop.com/http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/http://www.kongregate.com/gameshttp://www.kongregate.com/badgeshttp://www.kongregate.com/forumshttp://www.kongregate.com/developer_centerhttp://www.kongregate.com/pages/helphttp://www.kongregate.com/
  • 8/7/2019 Match 3

    3/21

    Current Challenges

    Kongai

    Get Kreds

    Kongregate Arcade for Android

    Unity Game Contest New

    Top Rated

    My Favorites

    MMO

    Strategy/Defense

    Adventure/RPG

    Shooter

    Puzzle

    Action

    Multiplayer

    More

    Current Challenges

    My Cards

    Badges

    My Point History

    Leaders

    My Profile

    Change Avatar

    My Friends

    Forums

    Art & Sounds

    News

    Invite More Friends

    Earn Money

    My Revenue Contests

    Upload Game

    Kongregate APIs

    Collabs

    Tutorials

    http://www.kongregate.com/current_challengeshttp://www.kongregate.com/games/Kongregate/kongaihttp://www.kongregate.com/kredshttp://www.kongregate.com/androidhttp://www.kongregate.com/unity_game_contesthttp://www.kongregate.com/games?sort=newesthttp://www.kongregate.com/top-rated-gameshttp://www.kongregate.com/session/new?nav_target=my_favoriteshttp://www.kongregate.com/mmo-gameshttp://www.kongregate.com/strategy-defense-gameshttp://www.kongregate.com/adventure-rpg-gameshttp://www.kongregate.com/shooter-gameshttp://www.kongregate.com/puzzle-gameshttp://www.kongregate.com/action-gameshttp://www.kongregate.com/multiplayer-gameshttp://www.kongregate.com/more-gameshttp://www.kongregate.com/current_challengeshttp://www.kongregate.com/accounts/Guest/card_albumhttp://www.kongregate.com/badges/getting_startedhttp://www.kongregate.com/session/new?nav_target=points_historyhttp://www.kongregate.com/accounts/leadershttp://www.kongregate.com/session/new?nav_target=profilehttp://www.kongregate.com/session/new?nav_target=avatarhttp://www.kongregate.com/session/new?nav_target=friendshttp://www.kongregate.com/forumshttp://www.kongregate.com/collabshttp://www.kongregate.com/announcementshttp://www.kongregate.com/plugs/newhttp://www.kongregate.com/pages/earnhttp://www.kongregate.com/session/new?nav_target=revenuehttp://www.kongregate.com/contestshttp://www.kongregate.com/games/newhttp://www.kongregate.com/developer_center/docs/kongregate-apihttp://www.kongregate.com/collabshttp://www.kongregate.com/labshttp://www.kongregate.com/current_challengeshttp://www.kongregate.com/games/Kongregate/kongaihttp://www.kongregate.com/kredshttp://www.kongregate.com/androidhttp://www.kongregate.com/unity_game_contesthttp://www.kongregate.com/games?sort=newesthttp://www.kongregate.com/top-rated-gameshttp://www.kongregate.com/session/new?nav_target=my_favoriteshttp://www.kongregate.com/mmo-gameshttp://www.kongregate.com/strategy-defense-gameshttp://www.kongregate.com/adventure-rpg-gameshttp://www.kongregate.com/shooter-gameshttp://www.kongregate.com/puzzle-gameshttp://www.kongregate.com/action-gameshttp://www.kongregate.com/multiplayer-gameshttp://www.kongregate.com/more-gameshttp://www.kongregate.com/current_challengeshttp://www.kongregate.com/accounts/Guest/card_albumhttp://www.kongregate.com/badges/getting_startedhttp://www.kongregate.com/session/new?nav_target=points_historyhttp://www.kongregate.com/accounts/leadershttp://www.kongregate.com/session/new?nav_target=profilehttp://www.kongregate.com/session/new?nav_target=avatarhttp://www.kongregate.com/session/new?nav_target=friendshttp://www.kongregate.com/forumshttp://www.kongregate.com/collabshttp://www.kongregate.com/announcementshttp://www.kongregate.com/plugs/newhttp://www.kongregate.com/pages/earnhttp://www.kongregate.com/session/new?nav_target=revenuehttp://www.kongregate.com/contestshttp://www.kongregate.com/games/newhttp://www.kongregate.com/developer_center/docs/kongregate-apihttp://www.kongregate.com/collabshttp://www.kongregate.com/labs
  • 8/7/2019 Match 3

    4/21

    FAQ

    Developer Forums

    About Us

    Earning Cards & Badges

    What Are Points? Conduct Guidelines

    FAQ

    Contact Us

    Privacy

    Forums Programming

    Match 3 Type Game With Box2D - Trouble

    with the match 3[SOLVED]34 posts

    Sign in to reply

    Prev

    1

    2

    Next

    Mar 17, 20106:35pm

    Epic428322 postsFlag Post

    Hey! Well I am new here and I am also new to programming in flash as well.However, aside from that I was wondering if anyone could help me. Im workingon a game the operates very similar to Peggle and Plinko, except that it is also aMatch 3 type game.

    Heres a quick run down: The player drops a marble which bounces its way tothe bottom. When 3 or more of the same color marbles are touching they shoulddisappear and based on the # of Marbles points are added respectively. There arealso little wedge like objects that periodically pop up from the ground and caneither help the player make a match or split the marbles up. Either way thats thebasics. Box2D Provides the Physics.

    On to my Question: Can anyone help me figure out the best way to check if 3or more same colored marbles are touching? My first approach was to use nestedfor loops that ran through the array of all marbles. It checked each marble againstall others without repeating or checking itself. Using Pixel perfect collisiondetection I looked for any marble that was touching 1 or more of the samecolored marble and added all of the marbles that were touching to an array. If thelength of the array was 3 or greater it would undergo the process of calculatingpoints and eventually the removal of the marbles(for testing purposes i set it 2

    http://www.kongregate.com/forums/developershttp://www.kongregate.com/pages/abouthttp://www.kongregate.com/pages/conducthttp://www.kongregate.com/pages/helphttp://www.kongregate.com/feedbacks/newhttp://www.kongregate.com/pages/privacyhttp://www.kongregate.com/forumshttp://www.kongregate.com/forums/4-programminghttp://www.kongregate.com/session/newhttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3?page=2http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3?page=2http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3-solved.rsshttp://www.kongregate.com/forums/developershttp://www.kongregate.com/pages/abouthttp://www.kongregate.com/pages/conducthttp://www.kongregate.com/pages/helphttp://www.kongregate.com/feedbacks/newhttp://www.kongregate.com/pages/privacyhttp://www.kongregate.com/forumshttp://www.kongregate.com/forums/4-programminghttp://www.kongregate.com/session/newhttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3?page=2http://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3?page=2http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    5/21

    instead of 3).

    This however proved quite buggy and i found myself for some reason almostnever removing any marbles from the screen although the match showed true. insome cases a match of 2 marbles would show true but only one marble woulddisappear and the other would have its image split from the body. In either of the

    two cases it was highly noticeable that after about 20-25 marbles appeared onscreen (although i dont foresee anyone to ever have that many except on highdifficulties) the performance slowed considerably.

    I love box2D from what i have done with it so far and i think a custom contactlistener would work much better i just dont know exactly how i shouldimplement it so it works correctly. I think it would offer better performance butim not sure if it would be 100% better. LOL So far all i have learned from this isthat saying remove three or more objects of the same color that are touching is alot easier than coding it.

    Any ideas or help would be much appreciated. I read through the Flash GameUniversity chapter on Match 3. While it provided me the first used idea it didnthave the solution i required because my game doesnt operate on a grid. themarbles move freely. Thanks again for any help.

    Mar 17, 20109:09pm

    jonathanasdf3122 postsFlag Post

    ok a few quick notes:

    1) You dont have to check EVERY SINGLE marble every frame. Heres analternative. For each marble, have something that tracks whether it has moved since thelast check, and only check the marbles that have moved. And then you wouldcheck to see what marbles that marble is touching by running through your arrayof marbles, and repeat.

    If Box2D depatches a Collision Event or something when two objects collidethen that will be a great way to decide which marbles to check. But if thecollision event keeps being dispatched when two marbles are already touchingand not moving around thats a bit useless.

    2) Im assuming your marbles are perfect circles/spheres. Then, for the marble vsmarble hittests you can use complete code:

    if the distance between the centers of the marbles are smaller or equal to the sumof the radii of the marbles, they are colliding.But for the marble vs environment youll have to keep using pixel perfect

    As for your bugs. the first one sounds like you are forgetting to remove the

    other one or something, and the second one i just dont understand.And yeah the biggest problem is that your marbles move freely, which meansyou must loop through all of the marbles in order to see which ones are collidingwith your marble. But with the 2 changes I posted above you should notice quitea bit of decrease in lag.

    Mar 17, 201010:10pm

    @ jonathanasdf

    http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    6/21

    Epic428322 postsFlag Post

    I understand the first note. With Box2D i can find out if a particular body isawake or sleeping. if a body is awake then its physics is being simulated,if not it is being idle. So that would tell me which marbles are currently movingand which ones are not. However, while i think it could work, part of me says itwont because i have seen some objects show awake status while they werent

    necessarily moving only because their rate of movement was so slow or it wasrotating on its center. I get that idea though because if 3 same colored marblesare in contact with each other and not moving, technically they shouldnt exist,therefore it does make sense to check that.

    Using Box2Ds contact listener is a way of obtaining information regarding whatobjects are colliding with each other. However after reading the API, it seemslike i could use other contact functions as well. The contact listener though, wheni create a custom one that overrides the original, offers 4 different functions:

    BeginContact(contact:b2Contact):voidCalled when two fixtures begin to touch.

    EndContact(contact:b2Contact):voidCalled when two fixtures cease to touch.

    PostSolve(contact:b2Contact, impulse:b2ContactImpulse):voidThis lets you inspect a contact after the solver is finished.

    PreSolve(contact:b2Contact, oldManifold:b2Manifold):voidThis is called after a contact is updated.

    This class is called every time there is a contact and then depending on who iscolliding into who, different functions can be run. The problem though is thatoccasionally depending on the simulation of the physics the same contact mightbe called more than once in a step. I could check the complete contact list afterevery step but i may not receive a complete list due to some continuous physics

    leading to sub-stepping.

    I was using pixel perfect because although my marbles are perfect circles theyare about 1 2 pixels larger than their physics body to compensate for theimages center being slightly off. This prevented a visual gap from appearingbetween 2 marbles that were in fact touching. I think however i have correctedthis through some tweaking so its not noticeable unless being looked for.

    Mar 17, 201010:18pm

    Jabor11385 postsFlag Post

    Easy way:

    Each marble gets a BeginContact that, if the thing its contacting is anothermarble of the same colour, keeps a reference to it.

    Each marble also gets an EndContact that discards the reference.

    Also in the BeginContact, if the marble already has a reference (thats not thisone), then its now touching two other marbles of the same colour so you canremove all three.

    Shouldbe reasonably robust against redundant contacts.

    (Aside: You know what really sucks? When the restart to update your

    http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Jaborhttp://www.kongregate.com/users/46968/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Jaborhttp://www.kongregate.com/users/46968/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    7/21

    computer box pops up, and hitting space selects the default button on that box.)

    Mar 17, 201010:23pm

    jonathanasdf3122 postsFlag Post

    Well instead of using awake and sleeping itd make more sense to useBeginContact instead, as it seems perfect for this situation. As for the problem of

    the same contact being called more than once in a step, that is no problem ifyou set a property on the marble, say a property toBeRemoved, and at the end ofyour loop once everything has been resolved you loop through the list of marblesand remove all the marbles that have toBeRemoved set to true. This way youwont end up with any possibility of a null object reference in the middle of yourloops. The other 3 functions dont see as useful in this case.

    Another thing is that in these games generally it is not a good idea to remove themarbles on the exact frame that they touch. Its generally a good idea to wait for5 frames or so before removing them, so other marbles can also touch and beremoved together, otherwise youll end up with pretty much always groups of 3being removed. Which is also where the toBeRemoved property comes in handy it persists across multiple frames, and if another marble contacts a marble withtoBeRemoved set to true you dont need to do any more tests on that marble youcan just remove it.

    And your speed could be improved even farther if each marble stored whichother marbles its contacting. If thats built in to Box2d, then you have your workdone for you! Otherwise, you can push/pop them into arrays based onBeginContact and EndContact and create this functionality yourself. Then,instead of having to loop through all of the marbles, you just have to loopthrough that list.

    I dont understand your reason for using pixel perfect. If you wanted your hittestarea to be smaller than the actual graphics it would make more sense NOT to use

    pixel perfect wouldnt it? Anyways you can adjust for that by varying the radiusproperty of the marble the radius is the hittest radius you want to use, it doesnthave to be the radius of the actual graphics.

    Edit: Re Jabor: I agree with filtering the list of contacts to ones of the samecolour using BeginContact and EndContact although I had came up with theidea of keeping the list of contacts I didnt think of filtering it to only ones of thesame colour.

    And I hate that restart computer thing. I absolutely hate it. And also, why isthere only delay 10 minutes, 1 hour, or 4 hours? Why isnt there a I freakingknow, delay indefinitely, I will restart my computer when I get to it!!! button?!

    Mar 17, 201010:55pm

    Epic428322 postsFlag Post

    Heres my idea though, and Im not sure if it will work well or not. I like thecontact listener because it has already done some of the work for me and that isdetecting when two bodies are colliding every time they collide. The easiest wayto detect for a match of 3 or more is to find the marble who is touching morethan one of the same color marble. Im thinking if i have a blue marble, everytime it begins contact with another marble i set a flag and add both marbles to anarray. Every time they End Contact I turn the flag off and remove them from the

    http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    8/21

    array.

    Now Say i have 3 blue marbles: B1, B2, B3. B2 is touching both B1 and B3. Forcontact with B1 i set the first flag and add both to the array. For contact with B3 Iset a second flag and add B3 to the array. (I will have more flags though butcheck for min of 2). Basically after all contacts have been heard I need to check

    how many flags have been set and then if the flags are 2 or greater process thepoints and removal of marbles. I only check for min of 2 flags because only twocontacts are made between 3 objects.

    While this could work and is probably the best solution, there are some things tothink about.

    First where would the best place be to put these flags? Perhaps in my MarbleActor Class? I think that would work.

    Second where then would be the best place to put the array? And how would iseparate this array of marbles from another array without getting over crowded?

    My third question is what if somehow 4 or more same colored marbles were

    touching each other(whether it be in a line or a square, etc.)? How do i makecertain that all marbles are added to the same array without duplication.

    More so if each marble has its own flags which is how we knew to add them toan array, andwe check to see if multiple flags are set on each marble, whichdetermines removal and points, How do we prevent multiple arrays from beingcreated to contain these marbles which would be the cause of at least one or twomarbles being duplicated.

    To make this slightly more complicated consider that all these collisions wonthappen in any particular order due to free movement. With this in mind lets say ihave 4 marbles now.

    B1, B2, B3, B4. B1 makes and maintains contact with B2. B3 makes andmaintains contact with B4. suppose while these two groups are maintainingcontact, but not enough to be eliminated, B3 & B4 make their way over to B1 &B2. New contact list: B1B2B3B4.

    On to my point. what if when B3 makes contact with B2 it checks to see if B2 isalready apart of another array of contacts? if so, B3 and B4 then get added to thatarray, the flags get set, and then the flags and array created for the contact of B3and B4 get eliminated. Finally match of 4 is made, points are added, marbles areremoved, and arrays are cleared.

    Mar 17, 2010

    11:02pm

    jonathanasdf3122 postsFlag Post

    Actually, I just came up with an even better idea. Hard to explain in words so Illwrite some pseudocode. It solves all that you have posted.

    public static var MIN_GROUP_SIZE:int = 3; // maybe you'll want tochange this for harder difficultiespublic var group:Array;public var color;

    public function Marble() {group = new Array();group.push(this);

    http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    9/21

    }

    public function BeginContact(otherMarble) {// Since the group array is supposed to be identical if

    the marbles are in the same group just checking the first elementis good enough

    // Otherwise there's no way a marble can be in 2different groups at the same time, so the two groups must bemutually exclusive. Join them together to be a big group.

    if (otherMarble.color == this.color && this.group[0] !=otherMarble.group[0]) {

    this.group.concat(otherMarble.group);for (var i:int = 0; i < this.group.length; i++) {

    if (this.group[i] != this) {this.group[i].group = this.group;

    }}if (this.group.length >= MIN_GROUP_SIZE) {

    // start a timer to remove the entiregroup

    }}

    }

    public function EndContact(otherMarble) {// If >= MIN_GROUP_SIZE then the group will be removed in

    5 frames and the marble couldn't have rolled too far so remove itanyways

    if (this.group.length < MIN_GROUP_SIZE) {

    this.group.splice(this.group.indexOf(otherMarble));for (var i:int = 0; i < this.group.length; i++) {

    if (this.group[i] != this) {this.group[i].group = this.group;

    }}

    }}

    Hope that makes it clear if not ask questions.

    This is definitely the superior method to anything discussed above.

    Now about the timer removing the group, maybe the behaviour youd likeinstead is to only remove a group if everything is sleeping if thats the caseyou will have to remove marbles from the group on EndContact and also createyour own methods to determine if a marble is sleeping (x and y movement >

    epsilon, disregard rotation), and a check each frame for groups >=MIN_GROUP_SIZE to see if all of their members are sleeping. Depends on yourgame and your choice.

    If you implement this method as well as use math hittesting instead of pixelperfect hittesting, I can guarantee that you will be able to support up to 100marbles without lag if not more (if the BeginContact function doesnt get calledtoo often.)

  • 8/7/2019 Match 3

    10/21

    Mar 17, 201011:14pm

    Epic428322 postsFlag Post

    It is getting late for me so i wont exactly be able to run some tests on codetonight. But i do have a nice grounding base to work from. And while iunderstand the idea of removing the marbles a few frames/seconds down theroad, its not too feasible in this project because if my wedges pop up from out ofthe ground during that time the marbles will be split and make it look glitchy.

    But it is a nice idea. One thing i may not have mentioned is that when theseBegin contact and End contact functions are being called, no physics bodies canbe detroyed during that time otherwise it could cause problems. so i have amarkToRemove function, and then a removeMarbles function that are alreadycreated to safely complete this. Perhaps so you guys can get a betterunderstanding of the game (although it is by no means completed) you can go tomy website http://www.epictechworld.com/arbles and take a look what is doneso far. The only functions currently active are to create lots of marbles, pause thegame, go to the main menu, and start a new game. LOL its barely in an alphastage.

    EDIT: Also the game has been added to the site just for testing purposes, so

    dont mind that its breaking the layout lol

    Mar 17, 201011:34pm

    jonathanasdf3122 postsFlag Post

    well in 5 frames i dont think a wedge can suddenly pop out and destroy the linksthat much :)

    5 frames is like 1/6th of a second.

    But yeah just change this bit:

    if (this.group.length >= MIN_GROUP_SIZE) {// start a timer to remove the entire

    group}

    to:if (this.group.length >= MIN_GROUP_SIZE) {

    for (var i:int = 0; i < this.group.length;i++) {

    this.group[i].markToRemove();}

    }

    then.

    Mar 17, 201011:42pm

    Epic428322 postsFlag Post

    I will definitely give this all a try tomorrow and see what i can make of it.looking at it its by far better than what i did have which was nothing.. and

    something that didnt work lol. Ill let you know how i make out and if i haveany questions. Also i may end up uploading a .zip of my source files to mywebsite for download. Too much code across classes to just post it all ifnecessary lol. Anyways, its almost 3AM here so im going to go to bed :p

    Mar 17, 201011:44pm

    night.

    http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.epictechworld.com/arbleshttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.epictechworld.com/arbleshttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    11/21

    jonathanasdf3122 postsFlag Post

    Mar 18, 201011:03am

    Epic428322 postsFlag Post

    well so far i have been having a little trouble getting this code to work. I had tomake some variations to it so it would work with the way my code is laid out.My first issue before i can continue with the code is I am having trouble gettingthe Begin Contact function check and see if each object in the collision is aMarbleActor. MarbleActor is a class that extends my main Actor class and eachmarble consists of a new instance of this class. The class creates a link betweenmy skin graphic and the physics bodies. Below is two variations of code i havetried which one sort of works, and the other doesnt at all

    Sort of works:

    // getting the fixtures that collided

    var fixtureA:b2Fixture = contact.GetFixtureA();

    var fixtureB:b2Fixture = contact.GetFixtureB();

    trace(fixtureA.GetBody().GetUserData(), Get A UserData)

    trace(fixtureB.GetBody().GetUserData(), Get B UserData)

    if (fixtureA.GetBody().GetUserData().I_AM_A_MARBLE == true)

    {

    var marbleA:MarbleActor =fixtureA.GetBody().GetUserData();

    }

    if (fixtureB.GetBody().GetUserData().I_AM_A_MARBLE == true)

    {

    var marbleB:MarbleActor =fixtureB.GetBody().GetUserData();

    }

    if (marbleA.I_AM_A_MARBLE == true && marbleB.I_AM_A_MARBLE ==

    http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    12/21

    true)

    {

    trace(marbleA, MarbleA);

    trace(marbleB, MarbleB);

    //rest of code is unnecessary ATM

    Doesnt work

    var fixtureA:b2Fixture = contact.GetFixtureA();

    var fixtureB:b2Fixture = contact.GetFixtureB();

    if ( fixtureA.GetBody().GetUserData() == MarbleActor &&fixtureB.GetBody().GetUserData() == MarbleActor)

    {

    var marbleA:MarbleActor =fixtureA.GetBody().GetUserData();

    var marbleB:MarbleActor =fixtureB.GetBody().GetUserData();

    trace(marbleA);

    trace(marbleB);

    I have also uploaded a .zip of my source files (CS4 Version) to my website. Filescan be found here: http://www.epictechworld.com/arbles link is below thegame. Also the Code isnt heavily commented. so feel free to ask questions.

    Anyway, because the ContactListener Class is called every single time an objectcollides with another i need to filter out the objects that are just marbles, then runthe check against just those to see if they are beginning/ending contact, and act as

    appropriately.

    the first time i entered the code i didnt filter out just the marbles so while iwould get errors regarding certain objects, i noticed that 3 of a kind matchingwasnt working properly and it may have been due to a bug in the code.

    If anyone has a good idea on how to correct this. It would be much appreciated :)

    Mar 18, 2010 I dont think you can trust the contact listener events from box2d completely

    http://www.epictechworld.com/arbleshttp://www.epictechworld.com/arbles
  • 8/7/2019 Match 3

    13/21

    11:35am

    CuriousGaming325

    postsFlag Post

    here. An object that appears to be at rest might be continously bouncing, with amovement too small to see. Im also not sure if events are reported for sleepingobjects try setting allowSleep to false, and see if you get better results that way.

    Since all your objects are perfect circles, I would try to solve this one bychecking the distance between your objects. Build groups of the same colour

    using an A* style algorithm. It might get quite expensive to check every ball thisway, but you wont need to do it every frame, especially for stationary balls.

    Mar 18, 201012:29pm

    Epic428322 postsFlag Post

    with box2d if i turn debugDraw on so that i can see the physics objects i canview the color color change between awake and sleeping objects. from what ican tell i have not yet encountered a marble that appeared to be sleeping butreally wasnt. also i was able to get the first group of trace statements (before theif statement) to work and after several run-throughs of dropping about 15marbles i never saw any trace statements loop to indicate an object wascontinuously bouncing. with these results i havent had a reason to not trust thecontact listener.

    I understand the idea that because they are perfect circles if i were to calculatethe distance between the center points a certain given number would indicate thetwo objects are touching. However im slightly confused by how i wouldimplement that or how the A* style algorithm works ( which i could probablygoogle). Aside from that i plan on making harder difficulties incorporate the useof different sized marbles. The sizes though wont need to be matched. just color.so i think when that happens my # of calculations with that method would getmultiplied to account for different variations of 2 like-colored marbles touching.

    Mar 18, 20101:27pm

    jonathanasdf3122 postsFlag Post

    no, different size marble collisions wont matter. you can still check if the

    distance between their centers is less than their combined radii.

    But I think we should be able to trust contact listeners enough here.

    Can you post what the traces in your first code snippet returns?

    and can you try this?

    var fixtureA:b2Fixture = contact.GetFixtureA();var fixtureB:b2Fixture = contact.GetFixtureB();

    if ( fixtureA.GetBody().GetUserData() is MarbleActor &&fixtureB.GetBody().GetUserData() is MarbleActor){var marbleA:MarbleActor =

    MarbleActor(fixtureA.GetBody().GetUserData());var marbleB:MarbleActor =MarbleActor(fixtureB.GetBody().GetUserData());

    trace(marbleA);trace(marbleB);

    Mar 18, 20101:51pm

    Here is what my begin contact function looks like now. i tweaked it and got moretrace statements to appear and it seems like i might be having an issue with the

    http://www.kongregate.com/accounts/CuriousGaminghttp://www.kongregate.com/accounts/CuriousGaminghttp://www.kongregate.com/users/324941/postshttp://www.kongregate.com/users/324941/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/CuriousGaminghttp://www.kongregate.com/accounts/CuriousGaminghttp://www.kongregate.com/users/324941/postshttp://www.kongregate.com/users/324941/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/Epic428http://www.kongregate.com/users/3047677/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3http://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/accounts/jonathanasdfhttp://www.kongregate.com/users/113097/postshttp://www.kongregate.com/forums/4-programming/topics/81112-match-3-type-game-with-box2d-trouble-with-the-match-3
  • 8/7/2019 Match 3

    14/21

    Epic428322 postsFlag Post

    group arrays. Also for testing purposes i set it to only create 1 color marble.

    override public function BeginContact(contact:b2Contact):void{

    // getting the fixtures that collidedvar fixtureA:b2Fixture = contact.GetFixtureA();var fixtureB:b2Fixture = contact.GetFixtureB();

    if ( fixtureA.GetBody().GetUserData() is MarbleActor &&fixtureB.GetBody().GetUserData() is MarbleActor)

    {var marbleA:MarbleActor =

    fixtureA.GetBody().GetUserData();var marbleB:MarbleActor =

    fixtureB.GetBody().GetUserData();

    trace(marbleA, "MarbleA");trace(marbleB, "MarbleB");

    if (marbleB._marbleType == marbleA._marbleType &&

    marbleA.group[0] != marbleB.group[0]){

    trace(marbleA.group.concat(marbleB.group),"combining groups");

    trace(marbleA.group.length, "length ofMarbleA.group");

    for (var i:int = 0; i =MIN_GROUP_SIZE)

    {// start a timer to remove the

    entire groupfor ( i = 0; i