This is my solution. I don’t know if it’s the right one or the most elegant one but it works. I post only the two blocks of code you can modify in the lesson.
For each of the two blocks I wrote both the code you should have after completing the regular task and the one I tweaked to complete the challenge.
And I leave a little bit of thinking not explaining my solution.^^
BLOCK 1
REGULAR SOLUTION
// Get array of points in a pattern.
let points = scene.circlePoints(radius: 200, count: 100)
// Index to points.
var pointIndex = 0
CHALLENGE SOLUTION
// Get array of points in a pattern.
let points = scene.circlePoints(radius: 200, count: 100)
// Index to points.
var pointIndex = 0
var randomX = 0.0
var randomY = 0.0
BLOCK 2
REGULAR SOLUTION
// Get a random image and place it.
let index = randomInt(from: 0, to: animals.count - 1)
var graphic = Graphic(image: animals[index])
graphic.scale = 0.5
scene.place(graphic, at: touch.position)
// Get position from points at pointIndex.
let position = points[pointIndex]
// Increment pointIndex.
pointIndex += 1
if pointIndex == points.count {
pointIndex = 0
}
// Move graphic to position.
graphic.move(to: position, duration: 1.0)
CHALLENGE SOLUTION
// Get a random image and place it.
let index = randomInt(from: 0, to: animals.count - 1)
var graphic = Graphic(image: animals[index])
graphic.scale = 0.5
scene.place(graphic, at: touch.position)
// Get position from points at pointIndex.
var position = points[pointIndex]
position.x = position.x + randomX
position.y = position.y + randomY
// Increment pointIndex.
pointIndex += 1
if pointIndex == points.count {
pointIndex = 0
randomX = randomDouble(from: -300, to: 300)
randomY = randomDouble(from: -300, to: 300)
}
// Move graphic to position.
graphic.move(to: position, duration: 1.0)