Falling objects in SpriteKit? - ios

I have 20 different sprites. I essentially want a "flow" of them to be constantly falling from the top and disappearing when they reach the bottom. Essentially, imagine rain where each drop is a random sprite. I want each sprite to fall with a random rotation and each "drop" to be a random sprite from my selection of 20.
Can someone please point me in the right direction? I've never made a game before and this is my first time working with SpriteKit. I'm using Swift.

Make use of arc4random to get a random sprite from your selection of 20 sprites by adding those sprites to a group. arc4random will also be able to get a random rotation for you.
For them to fall down from the top to the bottom, you can apply -gravity to them or apply a -impulse.
To check whether they have reached the bottom, get the y value when they disappear off the bottom of the screen and use that as a comparision value and then use removeFromParent().
Alternatively the third method could be to use the movement of SKAction and simply adjust their Y coordinate. At the end of the completion block just use removeFromParent().

Related

Swift/Xcode "Snake Game"

I am a student learning how to code in Xcode using swift. I am currently trying to make an app similar to the snake game that hopefully everyone knows.
I am currently on a windows computer so I can't show my code at the moment. I doubt you will need it for what I'm asking.
I am trying to make my Sprite Node (Just a while square) move with a swipe of the screen in the up, down, left, right fashion just like the original snake game. I have the Gesture Recognizer set up so it prints out what direction your swiping.
How can I make the sprite node move with a swipe? I want it to move infinitely in that direction until you either 1.) hit a will and you respawn/game over or 2.) swipe in a different direction.
I assume you add velocity to the node when you swipe so you just add the code into that swipe snippet but how do I make it apply ONLY to the "snake"?
You can use an SKAction to move the snakeHead toward the wall that corresponds to the direction you want to move. This can be done in the action parameter when you declare your gestures.
I suggest using .move(to: duration:) and to maintain a constant speed toward each wall you can use the distance, speed, time formula to calculate the duration for the .move(to: duration:) action.
Check out https://developer.apple.com/documentation/spritekit/skaction
https://developer.apple.com/documentation/spritekit/skaction/1417768-move

How to detect intersecting nodes of children of other SKSpriteNodes

I hope you all can help with this. I'm working on app of a board game. I have hex shaped tiles which are called randomly and laid out at the start of the game. Each of these tiles has four sides with a value of 1 and the other two sides have values of 2 and 3.
Each tile is a SKSpriteNode with transparent rectangle Nodes on the edges. There are 5 different types of tiles and they need to be separate Sprites with child nodes because in addition to being randomly laid out they area also randomly rotated. So I need to know programmatically which tile edges are touching which edges of other tiles.
Like this:
https://app.box.com/s/nnym97st3xmrsx979zchowdq1qwsmpoo
(I tried to post an image of what I'm trying to accomplish, but apparently I don't have a high enough of a rating.) ;-)
For example: If a "2" is touching a "3", etc.
I first tried Collision detection, but of course that only works with dynamic, moving objects.
I tried an IF statement to compare if the other nodes were touching and then remembered that the coordinates where specific to the Parent Node, so that didn't work.
I then tried intersectsNode, but that seems to only work with nodes under the same parent.
I am currently working with convertPoint in order to get the coordinates to match the scene and thus be comparable. But I can't seem to get it work the way I need.
There must be something simple that I am not seeing. Any ideas?
Certainly not simple.
One solution would be to start all your shapes slightly spaced out from each other. Add invisible child nodes with physics bodies to all six sides and give each physics body an appropriate category based on their rating (1, 2 or 3).
When you start the game, move all the outer nodes into their proper position (sides touching) by using whatever movement method your prefer. This will give you contact messages as each hex side touches another. The contact messages will tell you what side number is touching its neighbor.
The exact coding of this idea depends on your current code, game play, etc...

UIView perpetual linear animation with collision detection

I'm trying to make a circular UIView animate in a particular direction until it collides with the borders of the view (which is full screen, so basically the borders of the device), at which point it will reflect off it and continue on its way infinitely. However, I'm not really sure how to pose my question, so I'm having trouble finding any information on it. I already have the view, the direction it will move in, and its velocity. I'm just not sure how to handle an animation like that.
Any advice is much appreciated! Thanks!
Use a CADisplayLink to continuously update the position of the view, synced with the refresh rate of the screen. The update method that that display link triggers will take into account the current x and y velocity of the view and update the frame based on it. If at ever point the x- or y-coordinate goes under or over a limit (0 or screen width/height), reverse the appropriate velocity value and recalculate the position.

How to avoid collision force in box2D?

i'm creating simple iOS game with Cocos2D and Box2D. In my game user has to create full word by shooting to squares with letters. If shooted letter is correct the square should explode, otherwise the square should fall down. I have created simple contact listener and I can detect collision between bullet and square but the problem is how to avoid collision force when letter is incorrect? I that situation (incorrect letter) i want square to simply fall down without applying collision force to the square.
Maybe I can delete the square and create new one at the position of deleted, but i think it's not the best idea :)
One suggestion I would make would be on collision, if the letter is incorrect, set the bodies x and z velocities to 0. This way when they collide with incorrect squares they will simply fall.
Another thing you could do would be to set the target square's body as fixed ( or rigid, I can't remember what it's called in Box2d off the top of my head). So even if an incorrect letter collides with it, it will not budge but the letter will bounce off it. And it the letter is correct, you can explode it as normal.
The solution I would advice is to set the velocity of the bullet to 0 in both directions in the PreSolve callback of the contact listener. Obviously you do a check in the PreSolve function. This way as the function is called PreSolve, the collision calculation have not happened yet. so setting velocity to 0 will make the bullet to have no effect on the square in terms of force.

Cocos 2D - Moving Sprites on a tiled map

I'm trying to move a sprite around a CCTMXMap in a smooth fashion. I've figured out how (using CCActions) to move from tile to tile, but I get gaps in my animation (it pauses for a frame while it reevaluates which direction to walk). I've tried moving the character in a scheduled update: method, but that gets messy when you try and restrict the sprite to only moving from tile to tile. Any suggestions on how to get the clean, consistent animation without messy manual animation using update?
Yes, don't use actions. You'll always have the 1-frame delay problem when using CCActions.
Moving the sprite in update is really pretty simple. Especially if you restrict movement to a speed (points per frame) that is clearly divisible by the tile size. For example if your tiles are 40x30, then horizontal speeds of 1,2,4,8,10 would work fine. Vertically 1,2,3,5,6,10 would work.
Update the position by this number, cast it down to int, compare it with the destination location:
if ((int)currentPos.x == (int)targetPos.x && (int)currentPos.y == (int)targetPos.y)
{
NSLog(#"I'm there!");
}
The reason for casting to int is to avoid rounding errors in floating point values.
Another solution would be - especially if your character can only move in one direction at a time - to figure out the number of frames it will take him to get there. If the character has to move 40 points to the right, and he moves at 4 points per frame, it'll take him 10 frames. Then just count the number of frames (how many times the update method ran) and if it reaches 10 (or 0 if you count down) then you know that the character has arrived without needing to check his position.

Resources