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.

Newcomers AppGameKit Corner / Moving Sprites to Specific Coordinates

Author
Message
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 8th Jun 2019 00:58
I am developing a card game and I initiate the game by dealing 6 cards to each player. I start by having the deck in the center of the screen and then move the top card in 6 rounds dealing one card at a time to each player. The cards should align in a 3 x 2 matrix in front of each player position. I have successfully coded the routine to deliver the 6 cards but I cannot get them to align properly. For players at the right or left sides of the screen I use the virtual screen width plus or minus an amount to ensure the sprite is not off screen or 0 and for top and bottom I use either 0 or the virtual screen height plus or minus an amount to accommodate the sprite. The cards all wind up in almost the correct location but I can't seem to get the edges to line up neatly.

Also, I'm not sure what the x,y values of sprites are measured from. Are they at the center of the sprite or top left corner or ???

Thanks much for any assistance.
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 8th Jun 2019 05:11
SetSpritePosition( iSpriteIndex, fX, fY ) positions sprite by the top left corner
SetSpritePositionByOffset( iSpriteIndex, fX, fY ) positions sprite by its centre
unless SetSpriteOffset( iSpriteIndex, x, y ) has been used
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 8th Jun 2019 05:44 Edited at: 8th Jun 2019 06:28
Please forgive me but I only just purchased AppGameKit Studio in May and I haven't programmed in Basic in about 25-30 years. My most recent programming efforts used either Visual Pascal or Cold Fusion. So, does fx indicate that the value of x is a floating point number? Why in many examples provided for AppGameKit Studio are parameters used with a # in front of the name?

Here's pieces of the code that is used to position each of the six cards on the right side of the screen. For the life of me, I can't see why the cards won't align properly. I have attached a screen shot to show the situation. The deck is spread out at the moment so I can watch the cards being taken one by one.

Attachments

Login to view attachments
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 8th Jun 2019 07:38
fx = float x
fx = float y

haven't tested your code its only part of and no media

buf if for example each card was 64 * 128


would position your sprites 70 pixels apart starting at location 100
which would mean they a 6 pixels away from the card to their left
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 9th Jun 2019 00:49
Being totally new to this, am I asking too much if I submit all the files so you can run the program and see how the cards fail to align?
JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 10th Jun 2019 01:09
It is very difficult to work with the code you provided as it does not include all the data, variables, and numbers that you use. I put together something that might help you, using some of your code. I hope it helps.

Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 10th Jun 2019 02:32 Edited at: 10th Jun 2019 05:01
dunno what "move" is, for sure. as stated above, we need more info, but:

...could easily send the card past your target/cause it to overshoot because you're not checking if the distance is less than 2, re:

that's a guess 'cause ur code isn't clear to me (i'm assuming that's the distance you want to move it?)

meanwhile, please don't make us download a word file with a pic inside; we aim to help just about anyone, but it's not very inviting when we have to go through "extra" steps to do so


from the pic, i'm imagining 4 players sitting around a rectangular table. and, when the cards are dealt, i'm assuming you want them rotated so they "face" each player?

Attachments

Login to view attachments
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 11th Jun 2019 23:37
Thanks to all for the feedback and code snippets. Since I am new and don't know all the protocols for the forum, I apologize for making it difficult for you to help me. I am attaching all the files so you can see everything while I try to understand the great suggestions that have been offered.

Attachments

Login to view attachments
JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 12th Jun 2019 04:04
Thank you. I worked with your code a bit and suggest a few changes as below to your DealCards.agc file, the DealCards subroutine. There are likely several ways to do this and everyone has their own coding style, so this is just one possibility. Basically, for me, you need to establish the starting card position for each player and then have all the remaining cards offset from that position. That is what I have suggested in the following code that I modified from your original code. Your code is all there, but I just changed the offsets and added a couple of extra lines:


Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 12th Jun 2019 04:33 Edited at: 12th Jun 2019 04:40
i got lost in your code sorry for not spending too much time in it

in the end, i do believe you are simply over-shooting your target x,y and need to see if it's "close enough". i see some of your code checking distances but not sure what you're comparing it against (precisely).

anyway, take a look at this and play with it. maybe it will help?:


now, i don't know how precise things should be which is why i set a generic if distance# > moverate# limit but i am sure someone more-knowledgeable can guide you, there. alas, my method pulls off the illusion well enough?

otherwise, i'm simply showing a way to get a card from point a to b while spinning the card (hence, all the SetSpritePositionByOffset() usage and the fact that you can define point a for each card dealt (i THINK you've got the deck (a # of sprites) all spread out in the middle of the table?)

meanwhile, 2 more thoughts:
think about programmatically setting target x,y for each card using a base location for each player? IE, apply whatever x,y offset from the base for subsequent cards?

in general, it's a bad habit to make as many (identical) calls as you seem to be. IE the multitude of: GetVirtualWidth()/2 + (GetVirtualHeight()/2 * tan(PlayerAngle[i])) - GetSpriteWidth(i) -type lines, for example .
do the virtual screen dimensions and sprite widths (of the cards) change? if not, set up some variables & define them, then utilize the variables throughout the rest of the loop (at least). otherwise, you stand to return some unexpected results (a common one is to grab the mouse x & y more than once per loop which can return different values each time within the same loop).

hope some of that helps

add: JosephB addressed some of my thoughts while i was posting:
Quote: "establish the starting card position for each player and then have all the remaining cards offset from that position"


and, yes, i do tend to deal off the bottom of the deck; an old habit of mine...

CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 12th Jun 2019 23:45
Thank you Joseph B and Virtual Nomad. I will need some time to digest your suggestions but I can already see that they are much closer to what I was seeking. I didn't really expect anyone to rewrite the code. I had hoped to get some understanding as to where I was going wrong. I believe that both of your last posts accomplish this and I will let you know how it works out for me. I especially liked the comment about getting the value once and setting a constant for it instead of repeating the call.
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 13th Jun 2019 08:15
I have reworked a lot of the code and cleaned things up so it will be easier to follow. I understand the suggestion that the first card be dealt to the player position and subsequent cards be offset from the first. However, to programmatically allow for anywhere from 2 to 10 players, I prefer to have the 2nd and 5th cards align with the player position and set the offsets up, down or sideways from there. I have identified the section of code in DealCard.agc where there is something wrong with my logic. I just can't see what it is. I am uploading the three changed files and the 2 media files in hopes that I am making it easier to figure out where I am going wrong. Thanks much.

Attachments

Login to view attachments
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 13th Jun 2019 08:17
I forgot that I also placed the entire deck in the center without spreading them out as they were before.
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 13th Jun 2019 20:14
I have solved the alignment problem by adding a line of code after the sprite has been moved to its supposed destination. I am not happy with the solution as I had hoped I could use the move process and I am still looking for help in figuring out why it didn't work. In the meanwhile, here is what I did. I added the line: SetSpritePosition((NumberOfCards + 1 - i), PlayerPosX[i] + OffsetX[j], PlayerPosY[i] + OffsetY[j])
just before incrementing to the next player.

SetSpritePosition((NumberOfCards + 1 - i), NewX, NewY)
SetSpriteAngle(NumberOfCards + 1 - i, CardAngle[i])

endif


// This marks the end of the section where I believe the problem of the card location is contained

Sync()
until (Move[i] = 0)
Move[i] = 1
SetSpritePosition((NumberOfCards + 1 - i), PlayerPosX[i] + OffsetX[j], PlayerPosY[i] + OffsetY[j]) // NEW LINE OF CODE ADDED TO FIX ALIGNMENT ISSUE
Next i
JosephB
18
Years of Service
User Offline
Joined: 12th Sep 2006
Location:
Posted: 13th Jun 2019 20:58 Edited at: 13th Jun 2019 21:16
CandyMan,

As you mentioned that you did not expect anyone to rewrite the code, so I'll try to make some comments on what I noticed while reviewing your code; however, sometimes writing some changes to the code seems more helpful, especially to me. When I look over your DealCards subroutine, it seems that you have identified the four player positions, so I am guessing that is where a card will be placed and that others cards will be offset from that position or all cards will be offset from that position (both x and y positions) that is used to place the cards, i.e. the reference X and Y positions that are used to place the cards through the offsets to that that position. I could not determine if you were actually referencing that location when finally placing the cards. For example, if you want to place a card next to the reference position you could identify that position in the SetSpritePosition command and then add or subtract the offset to/from that position, both the x and y offsets to/from the x and y positions.

I also see that you set the OffsetX and Y arrays 24 times (once for each j loop and once for each i loop) during the subroutine. I recommend that you set these arrays just once in another section. It also might be possible to use a multi-dimensional array, such as OffsetX [5,7] and OffsetY[5,7] . I suggest arrays of 5 and 7 because you do not use zero, but 1 through four and 1 through 6, for number of players and number of cards. In addition, I am not sure your offsets and player positions assist in properly placing the cards as you calculate a NewX and NewY without determining if those NewX and NewY positions are equal to or exceed the PlayerPosX and Y positions, so it is possible for the final positions to not be equal to the offsets from the playerPosX and Y positions. This could result in cards not being aligned.

You posted while I was writing and identified part of the problem. You may still use the move process, just place this line: SetSpritePosition((NumberOfCards + 1 - i), PlayerPosX[i] + OffsetX[j], PlayerPosY[i] + OffsetY[j]) right after the lines until (Move[I] = 0) and Move[I] = 1. I think a couple of your Offset calculations might need some tweaking to get the alignment correct, but it is looking nice.

I hope this is helpful and not confusing and as I mentioned in another post, there are likely several ways to code what you want to occur.
CandyMan
5
Years of Service
User Offline
Joined: 13th May 2019
Location: Orange County, California, USA
Posted: 14th Jun 2019 00:52
Wow, Joseph!! You have truly overwhelmed me with your fantastic analysis. It's going to take me several readings to grasp all the comments but I shall certainly do so and I am truly appreciative for the amount of time you spent analyzing the code. I hope that, with experience, I will be able to pay it forward with other programmers. I will post how I make out after absorbing your suggestions.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Jun 2019 17:49
Saw this post a few days ago, figured there were enough replies already helping you. But then I thought of spinning the cards as they're dealt out and decided to make a small demo for you.




Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-21 13:06:59
Your offset time is: 2024-11-21 13:06:59