-2

I am trying to create multiple SKSpriteNodes that each have their own independent variables that I can change/modify. I would like to be able to run a function when the app starts, for example "createSprites(5)" which would create 5 sprites with the image/texture "shape.png" at random x and y coordinates and add all 5 Sprites to an array that I can access and edit different Sprite's positioning based on the index value. I would then like to be able to have another function "addSprite()" which, each time it is called, create a new Sprite with the same "shape.png" texture, place it at another random X and Y coordinate and also add it to the array of all Sprites to, again, be able to access later and change coordinates etc.

I have been looking through so many other Stack Overflow pages and can not seem to find a solution. My ideal solution would simply be the two functions I stated earlier. One to create an "n" number of Sprites and another function to create and add one more sprite to the array each time it is called.

Hope that makes sense, I'm fairly new to Swift and all this Sprite stuff, so simple informative answers would be very much appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Terminal
  • 113
  • 4
  • 11
  • Well, break it down. You want to do one task, and repeat it several times. A simple for loop covers the latter. Now, how would you do this once? – Alexander Dec 31 '16 at 02:54
  • 4
    @Terminal Welcome to SO. Our job is not to write your code for you. I suggest you give it a try, and even if you don't get very far post what you have done. Then we can help you better. No one is just going to draft up this whole project for you. – Benjamin Lowry Dec 31 '16 at 03:04

1 Answers1

2

You're not going to find an ideal solution from the past because nobody has likely had exactly the same desire with both Swift and SpriteKit. Having said that, there's likely partial answers you can blend together, and get the result you want or, at least, an understanding of how to do it.

Sprite Positioning in SK is probably the first thing to read up on:

https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html

having gotten that figured out, you can move to random positions.

Random positioning of Sprites:

Duplicate Sprite in Random Positions with SpriteKit

Sprite Kit random positions

Both use earlier versions of randomisation that aren't as powerful as what's available now, in GameplayKit. So... Generating random numbers in Swift with GameplayKit: https://www.hackingwithswift.com/read/35/overview

It's hard to overstate the importance of understanding the various possibilities of game design implications of varying types of randomisation, so probably wise to read this, from Apple:

https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html

After that, it's a case of needing to determine what constitutes a time or event at which to create more sprites at more random positions, and how fussy you want to be about proximity to other sprites, and overlaps.

Community
  • 1
  • 1
Confused
  • 6,048
  • 6
  • 34
  • 75