c# to f# - C# event handler assignment (in CocosSharp), translation to F# -


i trying translate c# code please f#, learning cocossharp (http://developer.xamarin.com/guides/cross-platform/game_development/cocossharp/first_game/part3/). have mistakes either in defining handletouchesmoved or assigning touchlistener or both.

c# code:

touchlistener = new cceventlistenertouchallatonce (); touchlistener.ontouchesmoved = handletouchesmoved; addeventlistener (touchlistener, this); 

and handletouchesmoved:

void handletouchesmoved (system.collections.generic.list touches, ccevent touchevent) {     //... } 

my faulty f# code (just relevant piece within gamescene class):

type gamescene (mainwindow: ccwindow) x =      inherit ccscene (mainwindow)      let touchlistener = new cceventlistenertouchallatonce ()              // problem happen         touchlistener.ontouchesmoved <- x.handletouchesmoved         x.addeventlistener (touchlistener, x)      member x.handletouchesmoved (touches: collections.generic.list<cctouch>, touchevent: ccevent) =         () 

the assignment ontouchesmoved faulty apparently - doesn't compile: "this function takes many arguments, or used in context function not expected". elementary issue assignment missing. doing wrong please?

as far can see, ontouchesmoved mutable property of type action<_, _>. in general, f# functions not same thing .net delegates action. now, in context, f# lets write function delegate expected, not - think in case need more explicit.

i think (without testing) following should work

touchlistener.ontouchesmoved <- action<_,_>(fun t te ->     x.handletouchesmoved(t, te)) x.addeventlistener(touchlistener, x) 

you define handletouchesmoved local function (rather method) , write this:

let handletouchesmoved (touches:resizearray<cctouch>, touchevent: ccevent) =   ()  touchlistener.ontouchesmoved <- action<_,_>(handletouchesmoved) x.addeventlistener(touchlistener, x) 

the resizearray name f# alias .net generic mutable list type.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -