2 Replies
Latest reply on Jan 14, 2019 3:18 PM by jd.dp  Level 1 (0 points)

Wondering if anyone could share a bit of insight into something that’s been puzzling me.

After completing the lesson for “Placing in Patterns”, the completion prompt states:

“Challenge: The points returned by a pattern function are positioned around the centre of the scene. You can easily add an x, y offset to each point, placing the pattern anywhere you like. See if you can place several patterns in different places on the scene.”

I’ve been trying to figure this out, but have had little success. I’m sure I’m missing something obvious, or misunderstanding the challenge.

How might I go about setting an offet for each point in this array?

• Re: Learn to Code 3: Placing in Patterns Challenge  Level 1 (0 points)

Adding an offset for each point in the array:

1. The lesson "Placing an Array of Images" in the Coordinates section of Learn to Code 3 shows how to move (aka offset) an x and y coordinate.
2. You will want to move each point in the array after you have got the position from points at pointIndex.

Unfortunately, I am not sure yet how to place several patterns in different places on the scene.

• Re: Learn to Code 3: Placing in Patterns Challenge  Level 1 (0 points)

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)