Spawning in multiple positions - swift

Is there a way to spawn randomly in multiple positions for example, I have four SKSpriteNodes which are the locations of where I would like another node to spawn inside of.
These are locations of where I would like the new node to spawn:
//bluesquare
self.bluesquare.position = CGPoint(x:90, y:400)
//greensquare
self.greensquare.position = CGPoint(x:290, y:400)
//yellowsquare
self.yellowsquare.position = CGPoint(x:90, y:150)
//redsquare
self.redsquare.position = CGPoint(x:290, y:150)
When they spawn within the location is there a why to make them spawn in random positions within that SKSpriteNode? (I am using swift with a spritekit game file)
Thanks

Use arc4random to create a random number between 0 and 3. Next use if statements to determine your spawn point based on the random number output.

Related

How to assign 1 animation to multiple objects

I am trying to initiate the movement of two cars in Unity. There will be a variable time gap between the time points which the two cars start moving. But Unity lets me assign only one animation at a time which means that I can not move the second car while the first animation is running.
How do I achieve this?

How to make objects spawn in a place at random times and move to a direction after spawning

How can i make a rootnode of a scene spawn duplicates of itself in a position (For example: x: 10,y: 10,z: 10)
then move in a direction until they are completely off screen and then dissappear.
I'm not asking for code, but i am asking for how to do this(For example what do i use for duplicating a rootnode?
How do i make that node disappear?
How do i generate a random number between 1 and 10 that will determine when to spawn the duplicate?)
And How do i make my post meet the quality standards?
You'll want to use a generalized class of nodes and spawn instances randomly.
Here's an answer on how to make random numbers in Swift. Use that to make three different random numbers between 1 and 10, and use the node class constructor with the three random numbers as input.
Finally, to move the node in a random direction, you need to have some movement function in the node class and feed it random numbers.
This is how you ask a good question.
Here is an answer:
You could:
First copy the rootnode using "node.clone" or "node.copy".
Clone duplicates the whole node tree (the node and children) whereas copy duplicates just that node.
Next assign the copy to a variable (so you can access it).
Then add any actions if you need to (refer below).
Then in your spawning loop or whatever you use to spawn objects, make a clone of the clone (the variable we made in step 2) and set it's position, with a random number.
To generate a random number that can be positive or negative use something like:
((((Float(arc4random()) / Float(UINT32_MAX))*2)-1)*5)//five is the important
//number here because it means you will get a number ranging from
// -5 to 5
Then add any actions you need to (refer below).
Finaly call scene.rootNode.addChildNode(theNodeYouJustPositioned)
To remove a node call node.removeFromParentNode.
Actions:
Alternitavely, to deleting the node yourself, and positioning the node yourself in a loop or such, and moving the node yourself, you could use SCNAction.
To use SCNAction simply call node.runAction(SCNAction).
There are many different ways of using SCNAction so please visit the docs for a whole list.
To make your node position itself (sort of) try running a runblock:^node:SCNNode{} and setting the node's position to a random number in there.
The node now positions it self so you can skip step 4.
Now if you want to move the node by a fixed direction (I mean "not random") you could just add a moveBy:xyz action in step 3.
But if it had to be a random direction you could do this action in step 5 and feed random numbers into it.
To delete you nodes using a time delay you could use a sequence: action and feed in an array containing, first, a waitForDuration:Seconds action, and last, a removeFromParentNode action.
Please visit the docs for SCNAction and any other class I mentioned that you may not have understood.
I don't know swift (at all) so I am sure there are a few syntax errors in the code. (sorry)
Post comments below if your confused.
Thanks!

Half scenes with static elements, other with spawn

I have 2D game, where half scene with spawn enemies and (for example) other half scene, where I want use static enemies and other elements.
I thought to create sript that after some time (for example 10 seconds), will stop spawn scripts , and run the movement of other elements.
So. Maybe there is a reasonable solution to this problem.
[UPDATE]
I need the most sensible solution of such a problem, I do not mean to do this, but how to make it better.
1) Can make static elements, which will be a certain time, just stand behind the camera, and then move ... or programmatically create static elements, over time, in advance of known locations...Or download the entire stack of elements over time.
2) Or can completely abandon this idea. A striking example is the Subway Surf, there static scenes (layout) are created in random order.
P.s. I hope I have explained my problem
Just learn to use "Invoke", it's extremely simple.
Invoke( "YourOtherRoutine", 10f );
So after ten seconds it will run the other routine. That routine could easily stop one script running, start another script running, or, whatever it is you want to do. There are tens of thousands of examples of Invoke() and InvokeRepeating() on the usual Unity forums, etc.
From your reference to Subway Surf, I assume that you want to generate static elements like the path and static trains in subway surf and non-Static elements like some moving trains. If so then I have a possible solution.
You can create pre-defined sets of elements (let say 20 or 30 set with different combination of elements) and then spawn them randomly one after another. e.g. have a look at the two reference images below.
Now, note that you might see these scenes exactly as in the images multiple times while playing the game, this is because they are pre-created, The developers behind Subway surf have created these paths and saved them as prefabs and then spawn them at different locations during game play.
You might have noticed that sometime the path is the same but the position of trains is different. This can be achieved by further creating spawn points on your path and then at runtime randomly select points on which you want to spawn your static elements.
In many cases when there are more than one gates you can pass through (I am referring to the gate in the second image). the moving trains spawns on the path of the gate you cross. Spawning the moving train can be achieved as mentioned in step two with a movement script attached to it. Regarding the question of how to know on what path to spawn there are two possible ways (that I can think of right know).
You can keep track of your players current lane and then spawn the train on that lane.
You can place separate triggers on each lane and then detect which lane trigger was triggered and then spawn the train on that lane.
For other moving trains just use the method in step 2 to spawn them but with a movement script attached.

Random Spawning in certain locations

How can you spawn SKSpritenodes randomly inside other SKSpritenodes? Maybe using constraints (in Swift)?
For example, I would like circles to spawn in random places inside different square SKSpriteNodes.
In order to do that, it's much better to use CGRects to contain the object that you are going to spawn.
You can use arc4random_uniform to get a random value for both the X and Y coordinates of the object that you are spawning and then use CGRectContainsPoint to check if the location is inside the CGRect.

Cocos2D : How to detect that I moved a sprite in a specific zone?

2 things I'd like to do with cocos2D :
1) set a random board based on 6 png files (like a 6 tiles boardgame)
2) be able to move a sprite/pawn with my finger and detect on which tile the sprite/pawn was moved and trigger a specific action depending upon which tile is under my sprite/pawn...
any code examples ???
cheers,
Tibi.
one way you could do it would be to create an array of 6 x/y coordinates, one for each of the locations of the tiles. you could use these to move your pawns to and to test if they are in these locations, i.e.
if (pawn.position.x == sotoredLoc.x && pawn.position.y == sotoredLoc.y)
[pawn runAction];
type thing...
for the random part you might want to search for how to create an array of unique random numbers.