无法使我的SpriteKit游戏随机生成精灵

时间:2018-04-08 21:30:55

标签: swift sprite-kit

意图是让香蕉从天空掉下来,最终让玩家在接触地面之前收集香蕉,然后又一滴香蕉。 此刻我的问题是它只丢掉一个而不会掉落另一个。

以下代码创建函数

    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

添加物理值

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

创建中心

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

创建一个随机生成的开关(0只是用于测试目的)

    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")
    }

最后添加香蕉

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

如果有人能看到我的代码有问题,请告诉我们!

更新(2018年4月10日:10:18CST): 从didMove函数调用我的函数我得到以下错误

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) 

这是我新添加的计时器的代码

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()

0 个答案:

没有答案