UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Apple "Learn To Code 3" - need help!

Forums > Swift

Hi y'all!

Any Apple's "Swift Playgrounds" veterans here?

I'm slowly wrapping up with Apple's "Learn To Code 3" (known also as "Blu's Adventure"). I'm stuck on the first idea in one of the final tasks, "Music Universe". I tried to make an array of 16 letters and assign those to each note (since the amount of the available notes is 16), with no success so far. More info in the link attached: https://twitter.com/dorianzet/status/1241845192174579714?s=19If If need be, I can also share the code. Thanks!

2      

Hey, Dorian.

You can define the emoji list like this: let emojiList = ["πŸ‘»", "πŸ’€", "🀑", "πŸ‘Ύ", "πŸ€–", "πŸŽƒ", "πŸ–", "🦷", "🐣", "πŸ‰", "πŸ₯", "🌽", "πŸ–", "🏐", "🧲", "🦠"]

and replace the line: let graphic = Graphic(text: "❄️")

with: let graphic = Graphic(text: emojiList[Int.random(in: 0...15)])

This worked for me.

3      

Hi @guseulalio,

Thank you so much! Do you think it's possible to attach each emoji to each of the notes, so that when note 1 is played, πŸ‘» emoji would appear, note 2 = πŸ’€ etc.? So far the graphics seem to be all randomised, and I thought it would be nice to make it in a more orderly fashion.

It feels really nice to see such friendly approach to newbies like me, so I really appreciate your help even more!

3      

@twostraws  Site AdminHWS+

These forums are welcoming for everyone, and it's great to see @guseulalio really nailing that πŸ™Œ

3      

Thank you Paul.

Dorian, yes, you can.

First you need to replace the line graphic constant line above with:

let graphic = Graphic(text: emojiList[note])

You'll realize this causes an error. That's because the array index needs to be an Int, and at this point, note isn't an Int. We need to make sure it is. To do that, we need to redefine the note constant like this:

let note: Int = Int(normalizedXPosition * Double(numberOfNotes - 1))

This still won't fix the problem. Now we need to say that normalizedXPosition is a Double. Is needs to be a Double because, for the line above to work and give you a note between 0 and 15, normalizedXPosition needs to be between 0 and 1, otherwise it will be either 0 or 15.

let normalizedXPosition: Double = (touch.position.x + 500) / 1000

Things should work fine now, you should have one emoji for each note.

P.S.: Before trying all the above, I did the following:

let graphic = Graphic(text: emojiList[Int(note)])

which I thought would work, because that's the usual way to cast between number types. but it didn't, so I had to go around and change all the constants. If someone else has an explanation, I'd like to learn it.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.