1

First of all here's my previous question so you see what I'm trying to make.

We are trying to make a Small game. We made a sort of monster fighting game. We made items to the game but I want them to drop by the monster. made different types and would like to know how to code to get a Drop Chance on the Items... like

So now I know how that works I'm stuck to get a random item from my list.

So what I actually want is to get a Random item of my "NormalType" list when i print it..

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}


class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }
}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }
}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber = (arc4random_uniform(2))

print(Items[randomnumber])
Cœur
  • 37,241
  • 25
  • 195
  • 267
Kristof.V
  • 143
  • 1
  • 7
  • I suggest you have a look on my quite detailed answer to you previous question: it showed how (after presumably slaying a monster) you could call the `.dropItem()` method of class `Monster`, and it would return, randomly, one of the cases of a an `enum` holding your different rarities. You should be able to generalize this method to drop not only a random rarity, but also a random item. See http://stackoverflow.com/questions/34645338/drop-chance-in-swift/34646036#34646036 – dfrib Jan 07 '16 at 23:45
  • Might try it, but i'm kinda starting with all this so... your answer whas kinda hard to understand for me so i whas hoping on a easyer answer :P just started with this all 10 days ago.. :) – Kristof.V Jan 07 '16 at 23:51
  • I see :) Anyway, I added also item types to the example in the previous thread (for say, inspiration), but I saw you got what you needed for this question. Good luck with your game! – dfrib Jan 08 '16 at 00:08

1 Answers1

1

So you made a little mistake here's the code:

You have forgot to convert to Int the : (arc4random_uniform(2))

import UIKit

protocol NormalType {
    var name: String { get }
}

class knife: NormalType {
    let name = "Knife"
    let Str = 10
}
class sword: NormalType {
    let name = "Sword"
    let Str = 20
}
class katana: NormalType {
    let name = "Katana"
    let Str = 30
}



class RareType {
    class Knife: RareType {
        var Str = 10
        var Hp = 10
    }
    class sword: RareType {
        var Str = 20
        var HP = 15
    }
    class Katana: RareType {
        var Str = 30
        var Hp = 20
    }

}

class LegendaryType {
    class Knife: LegendaryType {
        var Str = 10
    }
    class sword: LegendaryType {
        var Str = 20
    }
    class Katana: LegendaryType {
        var Str = 30
    }

}
var Knife = knife()
var Sword = sword()
var Katana = katana()

var Items: [NormalType] = [Knife, Sword, Katana]
var randomnumber =   Int(arc4random_uniform(2))



print(Items[randomnumber])