Swift 4 SprikeKit - Thread 1: Fatal error: Index out of range - arrays

I am just learning Swift and I am following along with another project that I had worked on. However, I am getting this error:
Error I am getting:
Thread 1: Fatal error: Index out of range
All my code:
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var scoreLabel : SKLabelNode?
var player : SKSpriteNode?
var track : Int = 0
var trackArray : [SKSpriteNode]? = [SKSpriteNode]()
var ballDirection = Int(arc4random_uniform(2))
var velocityArray = [Int]()
var playerVelocity : Int = GKRandomSource.sharedRandom().nextInt(upperBound: 100)
var currentScore : Int = 0 {
didSet {
self.scoreLabel?.text = "SCORE: \(self.currentScore)"
}
}
func createHUD() {
scoreLabel = self.childNode(withName: "score") as? SKLabelNode
currentScore = 0
}
//I think the problem is coming from this function, however I more-or-less copied it from a tutorial project that works so I don't know what the problem is.
func setupTracks() {
for i in {
if let track = self.childNode(withName: "\(i)") as? SKSpriteNode {
trackArray?.append(track)
print(track)
}
}
}
func createBall() {
player = SKSpriteNode(imageNamed: "ball1")
player?.physicsBody = SKPhysicsBody(circleOfRadius: player!.size.width / 2)
player?.physicsBody?.linearDamping = 0
//This line throws the error
let ballPosition = trackArray?[track].position
print(track)
player?.position = CGPoint(x: (ballPosition?.x)!, y: (ballPosition?.y)!)
player?.position.x = (ballPosition!.x)
player?.position.y = (ballPosition!.y)
self.addChild(player!)
}
override func didMove(to view: SKView) {
createHUD()
createBall()
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
I have 8 Color Sprite objects that I put in through the GameScene.sks each named 0 - 9,
I think it is looking for a value in the array that has not been yet assigned? but I have a part that (I think) is assigning that value. It also works in the other project so I am very lost.

Some of this doesn't make 100% sense to me without context, but based on what I can infer it seems a better solution for you would be to use a single dictionary to access the SKSpriteNode as well as the direction and velocity. To achieve this you could create a custom object, or just use a simple tuple.
After you initialize the dictionary and fill it with the objects you can access a single value in the dictionary to get all of the data you need for your createEnemy function. It might look something like this:
var tracks = [Int : (node: SKSpriteNode, direction: CGFloat, velocity: CGFloat)]()
func setupTrack() {
for i in 0 ... 9 {
if let track = self.childNode(withName: "\(i)") as? SKSpriteNode {
//Get direction and velocity here, if initial value is known
//Setting to 0 for now
tracks[i] = (node: track, direction: 0, velocity: 0)
}
}
}
func createEnemy(forTrack track: Int) -> SKShapeNode? {
guard let thisTrack = tracks[track] else { return nil }
let enemySprite = SKShapeNode()
enemySprite.name = "ENEMY"
enemySprite.path = CGPath(roundedRect: CGRect(x: 0, y: -10, width: 20, height: 20), cornerWidth: 10, cornerHeight: 10, transform: nil)
let enemyPosition = thisTrack.node.position
let left = thisTrack.direction
enemySprite.position.x = left ? -50 : self.size.height + 50
enemySprite.position.y = enemyPosition.y
enemySprite.physicsBody?.velocity = left ? CGVector(dx: thisTrack.velocity, dy: 0) : CGVector(dx: 0, dy: -thisTrack.velocity)
return enemySprite
}
This will prevent you from going out of bounds on your array. Note on this: Since you are looping through a hardcoded 0...9 range, but you are optionally appending items to that Array (in the case where your if let track fails) you have no guarantee that the "track" item with the name "7" corresponds to the 7th index in your array. This is another reason a Dictionary is safer. Additionally, it seems you are maintaining 3 separate arrays (tracks, directionArray, and velocityArray), which seems prone to error for the same reason I just described.

Related

Can't access a specific Struct in an array on swift. Keep getting this error "Cannot call value of non-function type '""

I am trying to read and/or console print on of the elements of my array which consist of 28 structs. I can not access any of the struct on my array is says that "Cannot call value of non-function type '[Ficha]'" and I can't find why.. Sorry kind of a newbie on swift.. The section commented is where I discovered the problem but I can't even print one of the elements.
Please help
import UIKit
import Foundation
struct Ficha {
var numero: Int
var ladoA = 0
var ladoB = 0
}
extension Ficha: CustomStringConvertible {
var description: String {
return "f\(numero) \(ladoA)/\(ladoB)"
}
}
var dSet = [Ficha] ()
var rSet = [Int: Ficha] ()
func setDset () {
dSet = []
rSet = [:]
var fj = 0
var x1: Double = 0
var ficha1 : Ficha
var fichanum = 0
for x in 0...6 {
for y in x...6 {
fichanum = fichanum + 1
dSet.append(Ficha.init(numero: fichanum, ladoA: x, ladoB: y))
}
}
dSet.shuffle()
}
setDset()
print (dSet(2))
Using dSet with parenthesis is incorrect, that's the syntax for a function. So the line:
print(dSet(2))
is assuming there's a function that returns something:
func dSet(_ x: Int) -> Something {
return Something
}
To access an item at an index you use the square brackets, so it should be:
print(dSet[2])
Which will print the item at index 2 in the array dSet.
As others have pointed out, you access members of a collection using subscripts, which are invoked via [], not () (which is for regular function calls).
You can simplify this code quite a bit, btw:
import UIKit
struct Ficha {
var numero: Int
var ladoA = 0
var ladoB = 0
}
extension Ficha: CustomStringConvertible {
var description: String {
return "f\(numero) \(ladoA)/\(ladoB)"
}
}
func calculateDset() -> [Ficha] {
let xyPairs = (0...6).flatMap { x in
(x...6).map { y in (x: x, y: y) }
}
return zip(1..., xyPairs)
.map { (fichanum, pair) in
return Ficha(numero: fichanum, ladoA: pair.x, ladoB: pair.y)
}
.shuffled()
}
let dSet = calculateDset()
print(dSet[2])
UIKit already imports Foundation
Make functions return values, don't set them directly.
Don't set initial values to variables, only to immediate overwrite them with something else.
rSet, fj, x1, and ficha1 are unused, delete them.

Swift: Index Out of Range Error when Populating Two-Dimensional Array

I just started to learn Swift and I would like to code the game BubbleBreaker which I have already created in both Java and C# some years ago.
For this, I wanted to create a two-dimensional array of Bubble (which is derived from SKSpriteNode), however, when I am trying to populate the array, I always get an "index out of range error" at index [0][0]. Can somebody please help me?
class GameScene: SKScene {
//game settings
private var columns = 10
private var rows = 16
private var bubbleWidth = 0
private var bubbleHeight = 0
//bubble array
private var bubbles = [[Bubble]]()
override func didMove(to view: SKView) {
initializeGame()
}
private func initializeGame() {
self.anchorPoint = CGPoint(x: 0, y: 0)
//Optimize bubble size
bubbleWidth = Int(self.frame.width) / columns
bubbleHeight = Int(self.frame.height) / rows
if bubbleWidth < bubbleHeight {
bubbleHeight = bubbleWidth
}
else {
bubbleWidth = bubbleHeight
}
//Initialize bubble array
for i in 0 ... columns-1 {
for j in 0 ... rows-1 {
let size = CGSize(width: bubbleWidth, height: bubbleHeight)
let newBubble = Bubble(size: size)
newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)
bubbles[i][j] = newBubble // THIS IS WERE THE CODE BREAKS AT INDEX [0][0]
self.addChild(newBubble)
}
}
}
}
bubbles starts off empty. There's nothing at any index.
Update your loop to something like this:
//Initialize bubble array
for i in 0 ..< columns {
var innerArray = [Bubble]()
for j in 0 ..< rows {
let size = CGSize(width: bubbleWidth, height: bubbleHeight)
let newBubble = Bubble(size: size)
newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)
innertArray.append(newBubble)
self.addChild(newBubble)
}
bubbles.append(innerArray)
}
This builds up an array of arrays.
Instead of assigning new value as unexisting value, append new empty array of Bubble for every column and then append to this array newBubble for every row
for i in 0 ... columns-1 {
bubbles.append([Bubble]())
for j in 0 ... rows-1 {
let size = CGSize(width: bubbleWidth, height: bubbleHeight)
let newBubble = Bubble(size: size)
newBubble.position = CGPoint(x: i*bubbleWidth, y: j*bubbleHeight)
bubbles[i].append(newBubble)
self.addChild(newBubble)
}
}

How do I make this extension of Array? [duplicate]

Suppose I have an array and I want to pick one element at random.
What would be the simplest way to do this?
The obvious way would be array[random index]. But perhaps there is something like ruby's array.sample? Or if not could such a method be created by using an extension?
Swift 4.2 and above
The new recommended approach is a built-in method on the Collection protocol: randomElement(). It returns an optional to avoid the empty case I assumed against previously.
let array = ["Frodo", "Samwise", "Merry", "Pippin"]
print(array.randomElement()!) // Using ! knowing I have array.count > 0
If you don't create the array and aren't guaranteed count > 0, you should do something like:
if let randomElement = array.randomElement() {
print(randomElement)
}
Swift 4.1 and below
Just to answer your question, you can do this to achieve random array selection:
let array = ["Frodo", "Samwise", "Merry", "Pippin"]
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
print(array[randomIndex])
The castings are ugly, but I believe they're required unless someone else has another way.
Riffing on what Lucas said, you could create an extension to the Array class like this:
extension Array {
func randomItem() -> Element? {
if isEmpty { return nil }
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
For example:
let myArray = [1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16]
let myItem = myArray.randomItem() // Note: myItem is an Optional<Int>
Swift 4 version:
extension Collection where Index == Int {
/**
Picks a random element of the collection.
- returns: A random element of the collection.
*/
func randomElement() -> Iterator.Element? {
return isEmpty ? nil : self[Int(arc4random_uniform(UInt32(endIndex)))]
}
}
In Swift 2.2 this can be generalised so that we have:
UInt.random
UInt8.random
UInt16.random
UInt32.random
UInt64.random
UIntMax.random
// closed intervals:
(-3...3).random
(Int.min...Int.max).random
// and collections, which return optionals since they can be empty:
(1..<4).sample
[1,2,3].sample
"abc".characters.sample
["a": 1, "b": 2, "c": 3].sample
First, implementing static random property for UnsignedIntegerTypes:
import Darwin
func sizeof <T> (_: () -> T) -> Int { // sizeof return type without calling
return sizeof(T.self)
}
let ARC4Foot: Int = sizeof(arc4random)
extension UnsignedIntegerType {
static var max: Self { // sadly `max` is not required by the protocol
return ~0
}
static var random: Self {
let foot = sizeof(Self)
guard foot > ARC4Foot else {
return numericCast(arc4random() & numericCast(max))
}
var r = UIntMax(arc4random())
for i in 1..<(foot / ARC4Foot) {
r |= UIntMax(arc4random()) << UIntMax(8 * ARC4Foot * i)
}
return numericCast(r)
}
}
Then, for ClosedIntervals with UnsignedIntegerType bounds:
extension ClosedInterval where Bound : UnsignedIntegerType {
var random: Bound {
guard start > 0 || end < Bound.max else { return Bound.random }
return start + (Bound.random % (end - start + 1))
}
}
Then (a little more involved), for ClosedIntervals with SignedIntegerType bounds (using helper methods described further below):
extension ClosedInterval where Bound : SignedIntegerType {
var random: Bound {
let foot = sizeof(Bound)
let distance = start.unsignedDistanceTo(end)
guard foot > 4 else { // optimisation: use UInt32.random if sufficient
let off: UInt32
if distance < numericCast(UInt32.max) {
off = UInt32.random % numericCast(distance + 1)
} else {
off = UInt32.random
}
return numericCast(start.toIntMax() + numericCast(off))
}
guard distance < UIntMax.max else {
return numericCast(IntMax(bitPattern: UIntMax.random))
}
let off = UIntMax.random % (distance + 1)
let x = (off + start.unsignedDistanceFromMin).plusMinIntMax
return numericCast(x)
}
}
... where unsignedDistanceTo, unsignedDistanceFromMin and plusMinIntMax helper methods can be implemented as follows:
extension SignedIntegerType {
func unsignedDistanceTo(other: Self) -> UIntMax {
let _self = self.toIntMax()
let other = other.toIntMax()
let (start, end) = _self < other ? (_self, other) : (other, _self)
if start == IntMax.min && end == IntMax.max {
return UIntMax.max
}
if start < 0 && end >= 0 {
let s = start == IntMax.min ? UIntMax(Int.max) + 1 : UIntMax(-start)
return s + UIntMax(end)
}
return UIntMax(end - start)
}
var unsignedDistanceFromMin: UIntMax {
return IntMax.min.unsignedDistanceTo(self.toIntMax())
}
}
extension UIntMax {
var plusMinIntMax: IntMax {
if self > UIntMax(IntMax.max) { return IntMax(self - UIntMax(IntMax.max) - 1) }
else { return IntMax.min + IntMax(self) }
}
}
Finally, for all collections where Index.Distance == Int:
extension CollectionType where Index.Distance == Int {
var sample: Generator.Element? {
if isEmpty { return nil }
let end = UInt(count) - 1
let add = (0...end).random
let idx = startIndex.advancedBy(Int(add))
return self[idx]
}
}
... which can be optimised a little for integer Ranges:
extension Range where Element : SignedIntegerType {
var sample: Element? {
guard startIndex < endIndex else { return nil }
let i: ClosedInterval = startIndex...endIndex.predecessor()
return i.random
}
}
extension Range where Element : UnsignedIntegerType {
var sample: Element? {
guard startIndex < endIndex else { return nil }
let i: ClosedInterval = startIndex...endIndex.predecessor()
return i.random
}
}
You can use Swift's built-in random() function as well for the extension:
extension Array {
func sample() -> Element {
let randomIndex = Int(rand()) % count
return self[randomIndex]
}
}
let array = [1, 2, 3, 4]
array.sample() // 2
array.sample() // 2
array.sample() // 3
array.sample() // 3
array.sample() // 1
array.sample() // 1
array.sample() // 3
array.sample() // 1
Another Swift 3 suggestion
private extension Array {
var randomElement: Element {
let index = Int(arc4random_uniform(UInt32(count)))
return self[index]
}
}
Following others answer but with Swift 2 support.
Swift 1.x
extension Array {
func sample() -> T {
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
Swift 2.x
extension Array {
func sample() -> Element {
let index = Int(arc4random_uniform(UInt32(self.count)))
return self[index]
}
}
E.g.:
let arr = [2, 3, 5, 7, 9, 11, 13, 17, 19, 23, 29, 31]
let randomSample = arr.sample()
An alternative functional implementation with check for empty array.
func randomArrayItem<T>(array: [T]) -> T? {
if array.isEmpty { return nil }
let randomIndex = Int(arc4random_uniform(UInt32(array.count)))
return array[randomIndex]
}
randomArrayItem([1,2,3])
Here's an extension on Arrays with an empty array check for more safety:
extension Array {
func sample() -> Element? {
if self.isEmpty { return nil }
let randomInt = Int(arc4random_uniform(UInt32(self.count)))
return self[randomInt]
}
}
You can use it as simple as this:
let digits = Array(0...9)
digits.sample() // => 6
If you prefer a Framework that also has some more handy features then checkout HandySwift. You can add it to your project via Carthage then use it exactly like in the example above:
import HandySwift
let digits = Array(0...9)
digits.sample() // => 8
Additionally it also includes an option to get multiple random elements at once:
digits.sample(size: 3) // => [8, 0, 7]
Swift 3
import GameKit
func getRandomMessage() -> String {
let messages = ["one", "two", "three"]
let randomNumber = GKRandomSource.sharedRandom().nextInt(upperBound: messages.count)
return messages[randomNumber].description
}
Swift 3 - simple easy to use.
Create Array
var arrayOfColors = [UIColor.red, UIColor.yellow, UIColor.orange, UIColor.green]
Create Random Color
let randomColor = arc4random() % UInt32(arrayOfColors.count)
Set that color to your object
your item = arrayOfColors[Int(randomColor)]
Here is an example from a SpriteKit project updating a SKLabelNode with a random String:
let array = ["one","two","three","four","five"]
let randomNumber = arc4random() % UInt32(array.count)
let labelNode = SKLabelNode(text: array[Int(randomNumber)])
If you want to be able to get more than one random element out of your array with no duplicates, GameplayKit has you covered:
import GameplayKit
let array = ["one", "two", "three", "four"]
let shuffled = GKMersenneTwisterRandomSource.sharedRandom().arrayByShufflingObjects(in: array)
let firstRandom = shuffled[0]
let secondRandom = shuffled[1]
You have a couple choices for randomness, see GKRandomSource:
The GKARC4RandomSource class uses an algorithm similar to that employed in arc4random family of C functions. (However, instances of this class are independent from calls to the arc4random functions.)
The GKLinearCongruentialRandomSource class uses an algorithm that is faster, but less random, than the GKARC4RandomSource class. (Specifically, the low bits of generated numbers repeat more often than the high bits.) Use this source when performance is more important than robust unpredictability.
The GKMersenneTwisterRandomSource class uses an algorithm that is slower, but more random, than the GKARC4RandomSource class. Use this source when itโ€™s important that your use of random numbers not show repeating patterns and performance is of less concern.
I find using GameKit's GKRandomSource.sharedRandom() works best for me.
import GameKit
let array = ["random1", "random2", "random3"]
func getRandomIndex() -> Int {
let randomNumber = GKRandomSource.sharedRandom().nextIntWithUpperBound(array.count)
return randomNumber
or you could return the object at the random index selected. Make sure the function returns a String first, and then return the index of the array.
return array[randomNumber]
Short and to the point.
There is a built-in method on Collection now:
let foods = ["๐Ÿ•", "๐Ÿ”", "๐Ÿฃ", "๐Ÿ"]
let myDinner = foods.randomElement()
If you want to extract up to n random elements from a collection you can add an extension like this one:
extension Collection {
func randomElements(_ count: Int) -> [Element] {
var shuffledIterator = shuffled().makeIterator()
return (0..<count).compactMap { _ in shuffledIterator.next() }
}
}
And if you want them to be unique you can use a Set, but the elements of the collection must conform to the Hashable protocol:
extension Collection where Element: Hashable {
func randomUniqueElements(_ count: Int) -> [Element] {
var shuffledIterator = Set(shuffled()).makeIterator()
return (0..<count).compactMap { _ in shuffledIterator.next() }
}
}
Latest swift3 code try it its working fine
let imagesArray = ["image1.png","image2.png","image3.png","image4.png"]
var randomNum: UInt32 = 0
randomNum = arc4random_uniform(UInt32(imagesArray.count))
wheelBackgroundImageView.image = UIImage(named: imagesArray[Int(randomNum)])
I figured out a very different way to do so using the new features introduced in Swift 4.2.
// ๐Ÿ‘‡๐Ÿผ - 1
public func shufflePrintArray(ArrayOfStrings: [String]) -> String {
// - 2
let strings = ArrayOfStrings
//- 3
var stringans = strings.shuffled()
// - 4
var countS = Int.random(in: 0..<strings.count)
// - 5
return stringans[countS]
}
we declared a function with parameters taking an array of Strings and returning a String.
Then we take the ArrayOfStrings in a variable.
Then we call the shuffled function and store that in a variable. (Only supported in 4.2)
Then we declare a variable which saves a shuffled value of total count of the String.
Lastly we return the shuffled string at the index value of countS.
It is basically shuffling the array of strings and then also have a random pick of number of the total number of count and then returning the random index of the shuffled array.

Can't change properties of referenced object in array in Swift

I am trying to create a grid-based map made out of 20*20 tiles, but I'm having trouble making the tiles communicate and access properties of adjacent ones.
This is the code used trying to accomplish this:
var tileArray:Array = []
class Tile:SKSpriteNode
{
var upperAdjacentTile:Tile?
override init()
{
let texture:SKTexture = SKTexture(imageNamed: "TempTile")
let size:CGSize = CGSizeMake(CGFloat(20), CGFloat(20))
super.init(texture: texture, color: nil, size: size)
}
required init(coder aDecoder: NSCoder)
{
fatalError("init(coder:) has not been implemented")
}
func setAdjacentTile(tile:Tile)
{
upperAdjacentTile = tile
}
}
for i in 0..<10
{
for j in 0..<10
{
let tile:Tile = Tile()
tile.position = CGPointMake(CGFloat(i) * 20, CGFloat(j) * 20 )
tile.name = String(i) + String(j)
//checks if there is a Tile above it (aka previous item in array)
if(i+j != 0)
{
//The position of this tile in array minus one
tile.setAdjacentTile(tileArray[(j + (i * 10)) - 1] as Tile)
}
tileArray.append(tile)
}
}
println(tileArray[10].upperAdjacentTile) //Returns the previous item in tileArray
println(tileArray[9].position) //Returns the position values of same item as above
println(tileArray[10].upperAdjacentTile.position) //However, this doesn't work
Why can't i access/change the properties of the tile referenced in "upperAdjacentTile"?
upperAdjacentTile is an Optional you have to unwrap first (since it can be nil).
// will crash if upperAdjacentTile is nil
println(tileArray[10].upperAdjacentTile!.position)
or
if let tile = tileArray[10].upperAdjacentTile {
// will only run if upperAdjacentTile is not nil
println(tile.position)
}
Edit
As rintaro suggested you also have to specify the type of the objects the array contains:
var tileArray = [Tile]()

Arrays and Swift

I'm trying to deal with an app in Swift and I'm facing the following error using arrays:
fatal error: Array index out of range
This appears when apps assign a value to the array at index 0:
class DrawScene: SKScene {
init(size: CGSize) {
super.init(size: size)
}
var line = SKShapeNode()
var path = CGPathCreateMutable()
var touch: UITouch!
var pts = [CGPoint]()
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
touch = touches.anyObject() as UITouch!
self.pts[0] = touch.locationInNode(self) <-- Error appears here
drawLine()
}
Some ideas? (I'm using xcode 6 Beta 4)
Your array is empty at first.
if self.pts.isEmpty {
self.pts.append(touch.locationInNode(self))
}
else {
self.pts[0] = touch.locationInNode(self)
}
As it says in the docs:
โ€œYou canโ€™t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an arrayโ€™s existing bounds, you will trigger a runtime error.โ€
You'll have to use append or += to add to an empty array. If you always want to set this point to the first object in the array, replacing anything already there, you'll have to check the count of the array first.
I don't recommend this to anyone, but you could implement your own subscript that allows you to do this:
extension Array {
subscript(safe index: Int) -> Element? {
get {
return self.indices.contains(index) ? self[index] : nil
}
set(newValue) {
guard let newValue = newValue { return }
if self.count == index {
self.append(newValue)
} else {
self[index] = newValue
}
}
}
}
And you can use this as expected:
var arr = [String]()
arr[safe: 0] = "Test"
arr[safe: 1] = "Test2"
print(arr) // ["Test", "Test2"]
arr[safe: 0] = "Test0"
print(arr) // ["Test0", "Test2"]
arr[safe: 3] = "Test2" // fatal error: Index out of range

Resources