I'm making a small game where the player uses the crank to rotate an atom that has "catchers", that can catch electrons that fly from the left side of the screen to the right. To make this work, I need to be able to detect when an electron collides with an atom's catcher. However, I'm not sure how I can run a function when this happens. I've set the collisions of the electrons and the catchers to both be overlap, but I can't figure out how I can use that to detect a collision when it happens.
(I'm developing with the SDK on a windows machine)
The example for moveWithCollisions shows how to loop through the returned collision info and conditionally call print(“Coin collected”) or print(“Collided with an obstacle”) and so on if it collides with certain sprites. You could call whatever function you want like that, not just print()
ooooh OK so as I understand it, MoveWithCollisions returns multiple arrays for each collision it does, which is how I can read info from that and call methods. I'll try to implement this in my code, and I'll see if it works. Thanks!!
I have a follow-up question (sorry). So as I understand it MoveWithCollisions (or CheckCollisions) will intake a point, and calculate how many collisions the sprite will come in contact with there. However, I am moving my sprites using a sprite Animator, and I was wondering if there was a way to have the sprite send a signal right when it is caught by the catcher. With MoveWithCollisions, it seems like it only calculates collisions right when the electron is spawned, which won't work for my game. Do you know if there is any way for the animator or sprite to calculate a collision after it has spawned?
Hmm as far as I know, you have to call moveWithCollisions or checkCollisions every frame (either in playdate.update or in each individual sprite’s update method) in order to get any collisions that happen later. sprite:setAnimator has a parameter for moveWithCollisions, but I’m not sure how you would get the resulting collision info from that.
I think you’ll have to move the sprite in your own update method instead of using setAnimator. You could still use an animator, you’d just have to have your update method get the animator’s value (e.g. local goalPoint = yourAnimator:currentValue()), then call moveWithCollisions on the sprite using that value, then check the collision info.
Ah I see. I changed my code so that it moved frame-by-frame in the updates function with MoveWithCollisions, and I'm trying to read the data from the output so I can despawn the sprites. Thanks!