How to call 2 functions on onclick in coffee script - coffeescript

I am trying to call 2 functions on click one button in coffeescript.but i am getting error dialtone() is not a function
dialtone1 = ->
audio = document.getElementById('dialtone1')
if audio.paused
audio.play()
else
audio.currentTime = 0
return
alex.on Events.Click, (event, layer) -> timer() dialtone()
How can i solve this

You have two things wrong:
There is no dialtone function, the function is called dialtone1.
timer() dialtone() doesn't do what you think it does.
When CoffeeScript sees timer() dialtone(), it thinks you mean
timer()(dialtone())
i.e. call what timer returns as a function with what dialtone returns as its argument.
Fix the typo in the function name (either where you define it or where you call it) and use two statements to call the functions:
alex.on Events.Click, (event, layer) -> timer(); dialtone()
# ---------------------------------------------^
or
alex.on Events.Click, (event, layer) ->
timer()
dialtone()
or even:
timer_and_dialtone = ->
timer()
dialtone()
alex.on Events.Click, timer_and_dialtone

Related

How to call a parameter label of type closure (Swift)

Closures As Parameters
let driving = {
print("I'm driving in my car.")
}
func travel(action: () -> Void) {
print("I'm getting ready to go.")
action()
print("I arrived!")
}
travel(action: driving)
action is a parameter label. How come we consider it as a function call as in action()?
Lets look at a brief example:
func myFunction() {...} Swift sees this as () -> Void
the same way that Swift sees this "John" as String
So when you write a function like myFunction you could really say
func myFunction() -> Void {...} for the same result.
Now action is defined as a parameter as a function but the type that it accepts for that parameter is () -> Void aka a closure which for now you can think of just another function.
So the line action() is just the call of that function.
But be sure to read up on functions accepted as parameters

Closures in Swift

I've been trying to understand how closures work. I understand that they capture their environments, but I don't understand how this happens, though.
In the code,
func make() {
print("Done")
}
func count( f: #escaping ()->()) -> () -> (){
var c = 0
return{
c = c + 1
f()
}
}
let countmake = count(f: make)
countmake()
countmake()
When stepping through the code, why does the declaration of c get skipped, and instead go straight to the anonymous function?
Thanks in advance!
You declare the c variable only once, when call the count function.
When you call the countmake function, you call only this block (closure):
{
c = c + 1
f()
}
Because you return this block from the count function.
But this closure keeps a context. It means that the system saves in the memory all variables which are referred from your closure. Thus the c variable is defined only once and available from the countmake function.

What's the difference between the two closure

I tried comment and uncomment the activity() in the following code. I found when I commented the activity() the result in playground would just show "play tennis" once. However it would show twice if I uncommented activity(). What's the difference between the two statements?
class Baby {
var name = "peter"
var favoriteActivity: (() -> ())!
func outsideActivity(activity: #escaping () -> ()) {
//activity()
favoriteActivity = activity
}
}
var cuteBaby = Baby()
cuteBaby.outsideActivity {
print("play tennis")
}
cuteBaby.favoriteActivity()
This is what’s going on:
Consider this method:
func outsideActivity(activity: #escaping () -> ()) {
//activity()
favoriteActivity = activity
}
All that does is save the closure in the favoriteActivity property
Thus, when you do:
// create `Baby` instance
var cuteBaby = Baby()
// this method saves closure in `favoriteActivity`, but doesn’t call it
cuteBaby.outsideActivity {
print("play tennis")
}
// this now calls the closure
cuteBaby.favoriteActivity()
All the outsideActivity method does is save the closure in a property called favoriteActivity.
Thus you see one print statement.
However, now consider this method:
func outsideActivity(activity: #escaping () -> ()) {
activity()
favoriteActivity = activity
}
This actually calls the closure before saving it in the property.
So, when you do:
// create `Baby` instance
var cuteBaby = Baby()
// this method both calls the closure and then also saves it in `favoriteActivity`
cuteBaby.outsideActivity {
print("play tennis")
}
// this now calls the saved closure a second time
cuteBaby.favoriteActivity()
In this case, you’ll see your print statement being called twice.
That’s why the first rendition calls the closure only once, whereas the second calls the closure twice.
Usually when you pass a closure to a method, you either (a) call the closure from within the method (perhaps in some completion handler or the like); or (b) save the closure in some property so you can call it later.
So, this second example is very unusual, where outsideActivity both calls the closure itself and saves that closure in some property so you can call it again later. You usually do one or the other, but not both.

UIView animateWithDuration Cannot invoke '*' with an argument list of type [duplicate]

I am creating a doubly-linked-list of scripts (MSScripts) that are supposed to have their own run() implementation, and they call the next script (rscript) when they're ready . One of the scripts I'd like to create is just a delay. It looks like this:
class DelayScript : MSScript
{
var delay = 0.0
override func run() {
let delay = self.delay * Double(NSEC_PER_SEC)
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
let weakSelf = self
dispatch_after(time, dispatch_get_main_queue()) {
weakSelf.rscript?.run()
Void.self
}
}
init(delay: Double) {
super.init()
self.delay = delay
}
}
Where rscript is the next script to run. The problem is that if I remove the last line of the dispatch_after, it doesn't compile, and that's because of the changed return type of run() from optional chaining. I randomly decided to insert Void.self and it fixed the problem, but I have no idea why.
What is this Void.self, and is it the right solution?
Optional chaining wraps whatever the result of the right side is inside an optional. So if run() returned T, then x?.run() returns T?. Since run() returns Void (a.k.a. ()), that means the whole optional chaining expression has type Void? (or ()?).
When a closure has only one line, the contents of that line is implicitly returned. So if you only have that one line, it is as if you wrote return weakSelf.rscript?.run(). So you are returning type Void?, but dispatch_async needs a function that returns Void. So they don't match.
One solution is to add another line that explicitly returns nothing:
dispatch_after(time, dispatch_get_main_queue()) {
weakSelf.rscript?.run()
return
}

If SKAction Repeat Action Count Ends

I have a ground object being repeated 23 times:
var moveGroundSpritesForever = SKAction.repeatAction(SKAction.sequence([moveGroundSprite]), count: 23)
How can I create an if statement in swift for when this count finishes to perform another action?
Thanks
Use func runAction(action: SKAction!, completion block: (() -> Void)!). completion closure will trigger when all your repeated actions are executed