Having trouble making my SpriteKit game randomly generate Sprites

0

The intent is to make a banana drop from the sky, eventually have the player collect the banana before it touches the ground, then have another one drop. My issue at this moment is that it only drops one and doesn't drop another.

Following code creates the function

    func bananaDrop(timer: Timer) {
    print("- Debug: [bananaDrop] successfully initiated -")
    let spriteGenerator = GKShuffledDistribution(lowestValue: 1, highestValue: 7)
    let banana = SKSpriteNode(imageNamed:"banana")
    spriteGenerator.nextInt()
    banana.xScale = 0.2
    banana.yScale = 0.2

Adds the physics values

    let physicsBody = SKPhysicsBody(circleOfRadius: 15)
    physicsBody.contactTestBitMask = 0x00000001
    //physicsBody.pinned = true //Suspend in air
    physicsBody.allowsRotation = false
    physicsBody.affectedByGravity = true
    banana.physicsBody = physicsBody

Creates Center

    let center = size.width/2.0, difference = CGFloat(85.0)
    var x: CGFloat = 0

Creates a switch to random spawn (The 0s are just there for testing purposes)

    let bananaDrop = GKShuffledDistribution(lowestValue: 1, highestValue: 3)
    switch bananaDrop.nextInt() {
    case 1:
        x = 0
    case 2:
        x = 0
    case 3:
        x = 0
    default:
        fatalError("Number outside of [1, 3] generated")
    }

Finally adds the banana

    banana.position = CGPoint(x: x, y: (activePlayer.position.y + 200))
    addChild(banana)
}

If anyone can see an issue with my code please let me know!

Update (April 10, 2018: 10:18CST): Calling my function from the didMove function I get the following error

2018-04-10 10:10:42.324334-0500 Jungle Crasher[44612:953389] SKTexture: Error loading image resource: "Untitled"
2018-04-10 10:10:42.360542-0500 Jungle Crasher[44612:953389] SKTexture: Error loading image resource: "banana"
2018-04-10 10:10:42.723423-0500 Jungle Crasher[44612:953389] SKTexture: Error loading image resource: "001"
2018-04-10 10:10:42.796247-0500 Jungle Crasher[44612:953389] SKLabelNode: "Monaco" font not found.
2018-04-10 10:10:43.772976-0500 Jungle Crasher[44612:953389] -[Jungle_Crasher.GameScene updateCounting]: unrecognized selector sent to instance 0x7feefed0f970
2018-04-10 10:10:43.778395-0500 Jungle Crasher[44612:953389] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Jungle_Crasher.GameScene updateCounting]: unrecognized selector sent to instance 0x7feefed0f970'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010225012b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00000001018e4f41 objc_exception_throw + 48
    2   CoreFoundation                      0x00000001022d1024 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKit                               0x0000000102b32f51 -[UIResponder doesNotRecognizeSelector:] + 295
    4   CoreFoundation                      0x00000001021d2f78 ___forwarding___ + 1432
    5   CoreFoundation                      0x00000001021d2958 _CF_forwarding_prep_0 + 120
    6   Foundation                          0x000000010134cb1e __NSFireTimer + 83
    7   CoreFoundation                      0x00000001021e0174 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    8   CoreFoundation                      0x00000001021dfe32 __CFRunLoopDoTimer + 1026
    9   CoreFoundation                      0x00000001021df9ea __CFRunLoopDoTimers + 266
    10  CoreFoundation                      0x00000001021d7404 __CFRunLoopRun + 2308
    11  CoreFoundation                      0x00000001021d6889 CFRunLoopRunSpecific + 409
    12  GraphicsServices                    0x000000010ae419c6 GSEventRunModal + 62
    13  UIKit                               0x00000001028ff5d6 UIApplicationMain + 159
    14  Jungle Crasher                      0x0000000100fcb387 main + 55
    15  libdyld.dylib                       0x00000001069e9d81 start + 1
    16  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Here is the code for my newly added timer

var timer = Timer()

func bananaDropFinal(){
    // Scheduling timer to Call the function "banana" with the interval of 1 seconds
    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: Selector("banana"), userInfo: nil, repeats: true)
}

func banana(){
    bananaDrop()
}

viewDidLoad()
swift
sprite-kit
asked on Stack Overflow Apr 8, 2018 by Micah Shackelford • edited Apr 10, 2018 by Micah Shackelford

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0