简体   繁体   中英

How do I pass the physics object between modules in the Corona Sdk

main.lua

local physics = require("physics")
local actor = require("actor")
physics.start()

//here physic is not nil
local a = Actor.new(200, 200, physics)

actor.lua

function Actor:new(x, y, physic)
   //here physic is nil
end

How come I cant pass the physics object into another module?

In Lua you have to be careful when you define a function in a table: do you use . or : . In actor.lua you define Actor:new(x,y,phys) which means there is an implicit self parameter that is available in the body of Actor:new ; this self will refer to the containing Actor table, and is the first parameter in the call to Actor:new . In main.lua you call Actor.new(200, 200, physics) : note the dot instead of colon, so the first parameter is 200, meaning self will be 200, x will be 200, and y will be physics , and phys will be nil. You should change the call to new or the definition of new , one or the other. For example in main.lua :

local a = Actor:new(200, 200, physics)

Note also that if your actor.lua doesn't return anything then local actor in main.lua will be nil . Looks like you probably defined Actor table as a global so you are able to reference it in main.lua .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM