samedi 10 juin 2017

randomiser freezes game after being used for awhile

I’m trying to create a randomiser that picks a random item from a set of items using a number ranges to determine the outcome of each item and how much it will come up when it picks an item e.g.

I have a number being picked from a range of 0 to 150 and the coin item is when a number is picked between 11…50 while a rarer item has a range of 0…10 this allows me to change the frequency of how much the items come up. as seen below

switch ItemRangeSelector(150, lowestNum: 0) {

        case 0...50:

            itemNode.texture = SKTexture(imageNamed: "coin1") ; giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(250, lowestNum: 125)) // negative coin amout here ( 150 - purchase price)

        case 51...101:

            itemNode.texture = SKTexture(imageNamed: "coin1"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(375, lowestNum: 250)) // postive coin amount here ( purchase price plus 50) or double the price

        case 102...117:

            itemNode.texture = SKTexture(imageNamed: "Sun"); giveItemAmount(itemName: "Item7", giveAmount: ItemAmountSelector(2, lowestNum: 1)) // special collector item amount if not completed

right now, the items are just hardcoded but during my game the items will be changing and may need to be taken out because the items will be full or not needed as the player advances further through the game. the game uses two functions to pick the items and the amount both using the same generic outline of picking a number within a range of the lowest number and the highest number as seen below.

func ItemRangeSelector(_ maximum: UInt32, lowestNum: UInt32) -> Int {

    var randomNumber: CGFloat
    var lowestNumber:CGFloat = 0

    lowestNumber = CGFloat(lowestNum)

    repeat {

        randomNumber = (CGFloat(arc4random_uniform(maximum)))

    } while currentNo == randomNumber || randomNumber < lowestNumber

    currentNo = CGFloat(randomNumber)

    return Int(randomNumber)
}


func ItemAmountSelector(_ maximum: UInt32, lowestNum: UInt32) -> Int {

    var randomNumber2: CGFloat
    var lowestNumber:CGFloat = 0

    lowestNumber = CGFloat(lowestNum)

    repeat {

        randomNumber2 = (CGFloat(arc4random_uniform(maximum)))

    } while currentNo2 == randomNumber2 || randomNumber2 < lowestNumber

    currentNo2 = CGFloat(randomNumber2)

    return Int(randomNumber2)
}

My problem: when used too much, the box freezes the game and it gives no error or anything and I can’t figure what would cause it to freeze while trying to pick a number. is there anyway i could do what i’m doing now better or easier without having multiple switches and functions and have an changing set of items that it picks from?

func playBoxTierOne() {

    chest.texture = SKTexture(imageNamed: "chest")

        switch ItemRangeSelector(150, lowestNum: 0) {

        case 0...50:

            itemNode.texture = SKTexture(imageNamed: "coin1") ; giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(250, lowestNum: 125)) // negative coin amout here ( 150 - purchase price)

        case 51...101:

            itemNode.texture = SKTexture(imageNamed: "coin1"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(375, lowestNum: 250)) // postive coin amount here ( purchase price plus 50) or double the price

        case 102...137:

            itemNode.texture = SKTexture(imageNamed: "Sun"); giveItemAmount(itemName: "Item7", giveAmount: ItemAmountSelector(2, lowestNum: 1)) // special collector item amount if not completed

        case 138...150:

            switch ItemRangeSelector(150, lowestNum: 0) {

            case 0...50:

                itemNode.texture = SKTexture(imageNamed: "coin1"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(450, lowestNum: 400))  //slightly larger coin amount here

            case 51...101:

                itemNode.texture = SKTexture(imageNamed: "coin1"); giveItemAmount(itemName: "Item3", giveAmount: ItemAmountSelector(3, lowestNum: 1)) //apia fruit amount here (1-3)

            case 102...140:

                itemNode.texture = SKTexture(imageNamed: "coin1"); giveItemAmount(itemName: "Item5", giveAmount: ItemAmountSelector(10, lowestNum: 5)) // bluestar amount here (5-8)

            case 141...150:

                switch ItemRangeSelector(150, lowestNum: 0) {

                case 0...59:

                    itemNode.texture = SKTexture(imageNamed: "coin4"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(750, lowestNum: 550)) // Large coin amount here

                case 60...94:

                    itemNode.texture = SKTexture(imageNamed: "Goblin"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(80, lowestNum: 30)) // Robber baron (gives a negaitive large amount of coins)

                case 95...149:

                    itemNode.texture = SKTexture(imageNamed: "Player1"); giveItemAmount(itemName: "Item3", giveAmount: ItemAmountSelector(5, lowestNum: 3)) // apia fruits (3-5)

                case 150:

                    itemNode.texture = SKTexture(imageNamed: "coin3"); giveItemAmount(itemName: "Item1", giveAmount: ItemAmountSelector(2500, lowestNum: 2500)) //JACKPOT COINS

                default:
                    return

                }

            default:
                return
            }

        default:
            return
        }

    exiteSkyBoxNode.isHidden = false

    //isBoxInUse = true
}

NOTE:

Also I use this function to store the amount given into core data where all the amount of coins are stored. I’m not sure if this is the problem so i just thought i’d include it.

func giveItemAmount(itemName:String, giveAmount:Int) {

    let getDataRequest:NSFetchRequest<Item> = Item.fetchRequest()
    getDataRequest.predicate = NSPredicate(format: "searchItemName == %@" , itemName)

    do {

        let searchResults = try CoreDatabaseContoller.getContext().fetch(getDataRequest)

        for result in searchResults as [Item] {

            retrieveAmountOfCoins(itemName: "Item1")
            coinAmountLabel.text = "x\(amountOfCoins)"

                if amountOfCoins >= tempPrice {

                    takeItemAmount(itemName: "Item1", takeAmount: tempPrice)

                    let amount = result.itemContainer!.intValue
                    result.itemContainer! = NSNumber(value: amount + giveAmount)

                    retrieveAmountOfCoins(itemName: "Item1")
                    coinAmountLabel.text = "x\(amountOfCoins)"

                    runLabelActionSequence(msg: "Received \(giveAmount) \(result.itemName!). Quantity now: \(result.itemContainer!)")

                } else {

                    runLabelActionSequence(msg: "NOT ENOUGH COINS")
                }

            do {

                try CoreDatabaseContoller.getContext().save()

            } catch {

                print("ERROR: \(error)")
            }
        }
    }

    catch {

        print("ERROR: \(error)")

    }
}




Aucun commentaire:

Enregistrer un commentaire