mardi 24 mars 2015

How to extract value in "optional(value)"

I am trying to retrieve a value that is in an Optional(N) where "N" is the value. I am new to Swift, so I am not sure if I have the syntax right or if it is even recommended to write my code like this, but heres what I have:



func dmg_Calc() {
if player.SPD < opponent.SPD {
var XX = rndm(2, 3)
var A = opponent.Lvl
var B = opponent.ATT
var C = opponent.MOV[XX]?[1]
var D = player.ATT
var X = 1.0
var Y = 1.0
var Z = rndm_DBL(217.0, 255.0)


// Opponent Attacks

var DMG = ((2.0*A/5.0+2.0)*B)
DMG = (DMG/D/50.0)+2.0
DMG = DMG*(X*Y/10.0)*Z/255.0

player.HP = (player.HP - DMG)

println(C)
println(DMG)
println("HP Left \(player.HP)")
println(XX)
}
}


I am using this as a damage calculation as I can see. My problem is when you get to Var C. That line gives me an Optional(N). It is basically routing to a dict to an array with the needed values.



class Eevee {
var Lvl = 5.0
var HP = 20.0
var ATT = 55.0
var DEF = 15.0
var SPD = 16.0
var SPC = 15.0
var isPoisoned = false
var isParalyzed = false
var isAsleep = false
var MOV = [1 : ["Tackle", 40.0, 35], 2 : ["Tail Whip", 0.9, 35]]


The above is the class that the function is accessing. I know it may look messy or terrible, but its what I am working with at the moment.


Is what I want possible to be done or should I try a different method? Any help would be appreciated!!!




Aucun commentaire:

Enregistrer un commentaire