Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Joining Sprites

Author
Message
Uechi
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location:
Posted: 4th Jul 2012 20:54
I want to create a jigsaw puzzle game just using square pieces for now. Once I know a piece has been put next to another piece where it belongs how can I then treat those two sprites as one so they can be moved together. And continue to put pieces together to make bigger pieces until the puzzle is complete. I thought physics weldjoint might work but once I attach them I can't move them as one sprite with a mouse. Any suggestions?
Rich Dersheimer
AGK Developer
15
Years of Service
User Offline
Joined: 1st Jul 2009
Location: Inside the box
Posted: 4th Jul 2012 23:32
With the new image commands, it's possible to paste down two sprites, then grab the image to use for a new sprite. See the help files on the new image commands.

Some days, the bear will eat you.
Some days, you'll eat the bear.
Today, I'm eating the bear!
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Jul 2012 13:40
Personally I would just manually set the positions. Store which pieces are connected in an array and move them all by the amount the mouse has moved. You'll be certain of accurate movement and you won't use extra memory by creating extra images.


this.mess = abs(sin(times#))
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 5th Jul 2012 15:57
Yeah, I would chain the pieces together. Like, use a variable to signify when a piece is joined with another piece, store the index, sprite number, or whatever of the connected piece. Then make sure that all pieces that are connected have this variable set - the last piece, maybe just the lowest sprite number that is connected, should be connected to itself - so you know whether or not a piece is part of a set when you move a piece.

Then, when you click on a sprite to move it - you continually check the connection, and base the position on that linked sprite. Only allow user movement on sprites that have no set link, or are linked to themselves...

[0][1][2][3]
[4]

So if you had the above pieces, you would set the link variable on 1 to 0, always looking for the lowest sprite index to maintain continuity, avoid loops in your linking data. Anyway, 1 links with 0 instead of 2, and 2 links with 1 instead of 2, and 0 is linked, but has no lower index next to it, so it gets set to 0. Then, when you move sprite 2, you know that it's linked to sprite 1, so position it next to sprite 1. When you move sprite 1, you find that it's linked to sprite 0, so position it accordingly. Then, when you move sprite 0, you see that it is linked, but it's the 'key' link, it has it's link value set to 0, so move that. Then all the resulting checks will make the piece move logically with piece 0, no matter which sprite you are moving.

You never need to break the puzzle apart, so the links can stay permanent. You can always check the link number, then check the solution to see where each sprite should be in relation to it's link. Each time you try a link, you can just check each sprite and see where they should be on the solution. I imagine the solution as a simple 2D grid, an indice for each piece which contains the sprite index that should go there. Once the solution array is setup, randomly position all the sprites, and that's it, job done - well, apart from a lot of coding. Hopefully you understand what I'm on about - just remember that because you don't break the succesful links, each sprite only really needs to link to 1 other sprite, in a chain, all the way down to the lowest sprite number, which is allowed to move because it isn't linked to anything apart from itself.

Health, Ammo, and bacon and eggs!
Uechi
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location:
Posted: 5th Jul 2012 16:19
Thank you all for the suggestions. I understand better what I have to do and will attempt to implement these.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Jul 2012 16:56 Edited at: 5th Jul 2012 16:57
I just started writing a bunch of code for you then thought of an even simpler method. Use sprite group numbers!

Initialise:

Start a new group:

Add selected sprite to group of another sprite (or start new group automatically):


EDIT: I have a few uses for this already...


this.mess = abs(sin(times#))
Uechi
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location:
Posted: 5th Jul 2012 21:35
I looked a little bit at groups but I don't see how I can move the group using mouse or touch. Am I missing something?
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 5th Jul 2012 22:44
There aren't built in commands for moving groups, it just makes them easier to select and organise.


this.mess = abs(sin(times#))
Uechi
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location:
Posted: 5th Jul 2012 23:55
So I could press a Sprite, determine what group it belonged to, and then move all the sprites in that group the same amount and direction as the pressed Sprite?
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 6th Jul 2012 00:26
Yup, that sounds like a good way to use it.

Cheers,
Ancient Lady
AGK Community Tester
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 6th Jul 2012 10:41
Quote: "So I could press a Sprite, determine what group it belonged to, and then move all the sprites in that group the same amount and direction as the pressed Sprite?"

That was the way I imagined it working yes.


this.mess = abs(sin(times#))
Uechi
18
Years of Service
User Offline
Joined: 2nd Sep 2006
Location:
Posted: 8th Jul 2012 03:50
Using groupid's worked great. Can actually move the entire puzzle around as one piece when the puzzle is completed.
When I press on a puzzle piece and move it, I check for all pieces in the pressed piece group and move them the same amount in the x,y direction as the press piece moved.

I think I will find many uses for groupid's in the future.

@baxslash - thanks for the help
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 8th Jul 2012 08:57
My pleasure


this.mess = abs(sin(times#))

Login to post a reply

Server time is: 2024-11-23 17:47:27
Your offset time is: 2024-11-23 17:47:27