Based on the SimpleView project, paste the example code in ViewController.mm as follow (other files not modified):
// // ViewController.mm // SimpleView // // Copyright (c) 2014 Corona Labs Inc. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. CoronaView \*view = (CoronaView \*)self.view; [view run]; // Send message to Lua NSDictionary \*event = [NSDictionary dictionaryWithObjectsAndKeys: @"pauseEvent", @"name", @"pause", @"phase", @"Message from Obj-C to Lua", @"message", nil]; [view sendEvent:event]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (id)coronaView:(CoronaView \*)view receiveEvent:(NSDictionary \*)event { NSLog( @"Logging properties of the event: %@", event ); // self.myLabel.text = [event objectForKey:@"message"]; return @"Nice to meet you CoronaCards!"; } @end
Similarly add the sample code in main.lua as follow:
--------------------------------------------------------------------------------------- -- A simple physics example in 8 lines! --------------------------------------------------------------------------------------- local physics = require( "physics" ) physics.start() local sky = display.newImage( "sky.png", 160, 195 ) local ground = display.newImage( "ground.png", 160, 445 ) local crate = display.newImage( "crate.png", 180, -50 ) crate.rotation = 5 physics.addBody( ground, "static", { friction=0.5, bounce=0.3 } ) physics.addBody( crate, { density=3.0, friction=0.5, bounce=0.3 } ) local myText = display.newText( "Hello World!", display.contentWidth / 2, display.contentHeight \* 0.3, native.systemFont, 16 ) -- Receive message from Obj-C local function handlePause( event ) if ( event.phase == "pause" ) then print( event.name ) --\> pauseEvent print( event.message ) --\> message --handle pause implementation here myText.text = event.message end return true end Runtime:addEventListener( "pauseEvent", handlePause ) -- Send message to Obj-C local function pressMe( event ) if ( event.phase == "ended" ) then local event = { name="coronaView", message="Hello from CoronaCards!" } print( "press crate" ) --dispatch the event to the global Runtime object local result = Runtime:dispatchEvent( event ) print( result ) --\> Response: Nice to meet you CoronaCards! end return true end crate:addEventListener( "touch", pressMe )
Run the program in iPhone simulator, the message “Message from Obj-C to Lua” shown on screen and log indicate sending from Obj-C to Lua works. Press the crate and in the log shown “press crate” and “false” (the return message). i think i have not register the delegate yet in Obj-C code but just have no idea how to do it.
Thanks for help.