samedi 14 janvier 2017

Choose a random SpriteNode out of an array?

sorry if I make any mistakes in terms of language, english is just my third language or so.

I'm learning how to code and as a first project I thought it would be a good idea to create a little stone paper scissors game against the computer. The problem is, that obviously the computer may not show his choice, so I have to create a function, which makes a random picture x (I'm using Apple's smileys for it) appear on the position x on the screen.

So I have to:

  1. random number between 1 and 3 (var rN = GKRandomDistribution(lowestValue: 1, highestValue: 3)

  2. let array = [opStone, opPaper, opScissors] //already declared as files

  3. let choiceOp = array[rN]

  4. choiceOp.position = CGPoint(x. ?, y: ?)

  5. self.addChild(choiceOp)

... in theory. The problem is, that Swift does not accept a GKRandom distributed number rN at the third step.

Do you have any ideas how I could do it?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire