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 / Colliding with dynamically created sprites?

Author
Message
JLM
8
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 4th Oct 2015 21:58
I'm pumping out sprites left and right, here. Dynamically, so I have no idea what their incremental ID's are or will be or even how many will ultimately be created. How can I set up collision code for them?

This is the code that is creating them:

if getspritecollision(1,2)
inc newdeathblock,1
createsprite(newdeathblock,3)
SetSpritePosition(newdeathblock,getspritex(2),getspritey(2))

inc score,1
setspriteposition(2,random(5,95),random(5,95))
settextstring(1,str(Score))
endif


At app launch, newdeathblock is set to 4, to avoid croaking by attempting to take an existing sprite ID.

I can't make a massive pile of getspritecollision calls for sprites that don't exist pre-run. This is my dillema.
Is it possible to make a call something like this?:

if getspritecollision(1,4-or-higher)
//put collision reaction here
endif
-Writing programs for years, new to AGK2
Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 4th Oct 2015 22:20 Edited at: 4th Oct 2015 22:26
This may be what you are looking for.

Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 5th Oct 2015 08:24
You could also consider grouping your sprites (setspritegroup) when you create them, and then check collision against the group (getspritehitgroup).
V2 T1 (Mostly)
Phone Tap!
Uzmadesign
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Oct 2015 08:32
Just to add to what Crazy Programmer has posted...he is letting the system determine the sprite ID (no possibility of accidentally using the same ID twice). Then, he is saving it in an array so it can be referenced any time later for testing, deleting, mass updating etc...

You should use this method for all of your game entities...sprites, images, sounds etc...
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
JLM
8
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 7th Oct 2015 04:47
Looked promising, but doesnt seem to be working. It isn't firing the collision for some reason:

-Writing programs for years, new to AGK2
JLM
8
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 7th Oct 2015 05:17 Edited at: 7th Oct 2015 05:19
Adjusting code placement got it spawning sprites again, but I'm not getting collisions when colliding with the dynamically created sprites. I'm definately missing something simple here...

-Writing programs for years, new to AGK2
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 7th Oct 2015 12:07
It's hard to diagnose without seeing more complete code, but at a hunch, I'd make sure you're SpriteID is being incremented correctly, and not being reset anywhere else in your loop. Pop a "print SpriteID" above your "For SpriteNum = 1 to SpriteID" loop just to make sure. If it remains at 1, then you know something's up (you'll still be creating dynamic sprites, but only populating the first index in your Sprite[] array with the new dynamic sprite number each time).

V2 T1 (Mostly)
Phone Tap!
Uzmadesign
Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 7th Oct 2015 12:59 Edited at: 7th Oct 2015 13:01
Global Dim Sprite[1000] /// This should be placed with your variables at the top of your program, not In your loop
Global SpriteID = 1 /// This should be placed with your variables at the top of your program, not In your loop



Maybe this will help you.


You were checking for collision between object 1,2 to create sprites, this is why you were not detecting collision with Sprite[SpriteNum]
I changed your code with comments.
Hope it helps....Good Luck!

Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 7th Oct 2015 13:28
something to play with
AGK (Steam) V2 Beta .. : Windows 10 Pro 64 Bit : AMD (15.7.1) Radeon R7 265 : Mac mini OS X 10.10 (Yosemite)
JLM
8
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 7th Oct 2015 18:54
@Crazy,

Yea, I continued to work with it last night and got it working. Just need to see if I can delay the dynamic sprite's appearance, because it detects collisions like mad right when it spawns, lol. Score drops like 4 or 5 points each time sprite 1 collides with sprite 2, because sprite 3 spawns at sprite 2's location. I'll post my entire code at the bottom of this post, so you see what I'm doing.

@Markus,

Thanks for the snippet. A lot of what you have there is more efficient that my code, as you will see below, although the application is different. I may use parts of it, though, I'll have to mess with it tonight

Current project code:
-Writing programs for years, new to AGK2
Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 8th Oct 2015 03:57 Edited at: 8th Oct 2015 03:59
I don't know what you are trying to do but it seems like it will be an interesting game. I have been testing your code with out media so all I had to go off of was the X-out boxes.

Here is what I have come up with for you.

Complete source.



What I have changed.
First a few new variables at the top of your code, where you do your defining.


A timer call at the very top of your loop so we can use a timer function.
TIME# = TIMER()

When the player hits the object that will decrease your score we call a recover function. So that the player can go into recovery mode. Mainly to kill the object collision for 1 second so the score will not decrease rapidly and give the player the chance to catch up.



Also I have called the recover time when the player hits object 2. So the newly created sprite can be positioned on top of the player with out detecting collision until 1 second has passed. This give the player just enough time to get away with out decreasing his score.



I have added a new IF statement to check if the player is in recover mode.




And last but not least the recovering function to end the players recovery.




So to break down the timer function.

When the Player either hits object 2, or Sprite[SpriteNum] the recovery function is called for 1 second. Giving the player a short period of recovery mode.
How the timer works is when the player is in contact with either of these 2 sprites
RecoveryTime# = Time# + 1 which adds + 1 to the game time.
When Time# is greater than RecoveryTime# the recovery period is over turning the collision back on.


I hope this is what you need and im sure there is another way of doing it...But this is my only idea for the moment.

Keep up the work, it is looking very interesting.

Edit: I have also changed the size of Sprite[SpriteID] when it is created
Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim
JLM
8
Years of Service
User Offline
Joined: 21st Jul 2015
Location:
Posted: 8th Oct 2015 05:17
@Crazy,

Yea I was working on a timer function as well, to skirt the collision spam on the newly spawned items, but it wasn't working exactly as I wanted. Your timer function is light years better than mine and works perfectly That sprite3 size tho... evil lol. One thing I'm going to do is drop that recover time to about 250ms, but other than that, you obviously knew (or suspected) exactly what I was working towards. I appreciate the help!

This will be my second app published to Google Play, once it's done. (search "Streakback" to see the first, a companion app for Planetside 2)

back to work, for me

J
-Writing programs for years, new to AGK2
Crazy Programmer
AGK Developer
19
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 8th Oct 2015 13:31
Glad i could help, if you have any more questions dont hesitate to ask
Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim

Login to post a reply

Server time is: 2024-04-23 12:54:50
Your offset time is: 2024-04-23 12:54:50