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 / [SOLVED] creating sprites with rand position not touching

Author
Message
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 10th Jan 2018 12:41
so I can't even math today. I am tinkering with a little game for my kids to quickly identify quantities (an early math skill). I have used the following code to create my sprites with a random color and position.



The problem is that sometimes the sprites cover eachother. I tried modifying it to test for collisions and rearrange, but its still not 100 percent.



I know that if I had to, I could type out a bunch of nested if's to do the job, but im sure there is a much more efficient way. I would like to use a "for i =" statement with the "if spritecollision=0" condition required before next i can happen. Thanks for the help in advance!

if anyone is interested in the game, its built with no supporting files in the media folder, just copy, paste and Run!

One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...

The author of this post has marked a post as an answer.

Go to answer

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 10th Jan 2018 12:44
Also, as I am still learning the basics, I am completely open to any and all constructive criticism or feedback regarding my coding. Have a good day
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...
Supertino
6
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 10th Jan 2018 12:44
do you need to organise sprites draw order via SetSpriteDepth()?
Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 10th Jan 2018 13:21
If I can get them to not touch at all then the overlap wouldn't be an issue. Honestly I kinda like the look of them overlapping a little, but the problem is if a 50x50 width sprite is placed, then a 100x100 sprite placed on top, the player will not count properly (and thats not fair x_x).

I apologize if I have misinterpreted your suggestion, but I'm not sure if that would help.

also, on a side note, anyone know if there is a way to generate simple sound bits using agk's language. just curious, for the sake of making an app without adding any media, if I can make a high pitch ding or buzz for a right answer, and a low pitch for a wrong answer. Thanks for the feedback Supertino!
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 10th Jan 2018 15:34
This post has been marked by the post author as the answer.
Turn your screen into a grid (invisibly of course). Approach it like you would build an inventory system (or storage bag) in an RPG-style game. If you break the screen down into 16x16 grid squares, then flag which ones are occupied when a sprite is placed. Then when you randomly place the next sprite, ensure it has room to be placed there. If it doesn't fit, you have multiple options. Either repeatedly pick a new random position until it does fit (fine for a few sprite on a large area, but too many could potentially cause an infinite loop) or you do something more intelligent like shift the sprite over from that random spot until it does fit (less likely to cause an infinite loop)

The grid obviously puts a small limitation on positioning as you'd have to lock the sprite's location to the grid squares, but the severity depends on the size of the grid you choose.
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
Supertino
6
Years of Service
User Offline
Joined: 22nd Jun 2017
Location: Behind you!
Posted: 10th Jan 2018 15:40
Not much going on in work today, this is how I would do it, quick but functional.

Vladimuffin
7
Years of Service
User Offline
Joined: 27th Nov 2016
Location:
Posted: 11th Jan 2018 12:12
Wow, Good stuff guys. Phaelax I think that is a good idea. Supertino, Its awesome that you would take the time to rewrite my program! I read through your code a little before bed last night, and it makes great sense. I see a ton of things that I could have done differently. Im going to take some time and rework everything using your code as a guideline. Thanks again!
One smart fellow, he felt smart. Two smart fellows, both felt smart. Three smart fellows all felt smart...

Login to post a reply

Server time is: 2024-03-28 22:45:04
Your offset time is: 2024-03-28 22:45:04