I try to modulize my code..... With no great succes:(

Hello everybody! This is the first time i try to modulize my code. I thought i would be easy but its a living nightmare… (just in the begining). Anyhow i can’t get my code to work. It’s splitted up in 3 lua files. Char.lua (My character), Helth.lua(My helth bar and its properties) and main.lua(my main file). Its just the collision thing and the -10 score thing which isnt working.

Hope you guys can help me :)!

Here is my lua files:

[lua]---------My Main.lua----------

display.setStatusBar (display.HiddenStatusBar)

require “physics”
physics.start()

require “Helth”

require “Char”
local W = display.contentWidth

local H = display.contentHeight

local BG = display.newImageRect( “mainBG.png”, 1024, 768 )
BG.x = 512
BG.y = 320

local Ground = display.newImageRect( “ground.png”, 1024, 64 )
Ground.x = 512
Ground.y = 678
Ground.name = “floor”

local Grass = display.newImageRect( “grass.png”, 1024, 128 )
Grass.x = 512
Grass.y = 755
physics.addBody(Grass, “static”)

Char.Char()

Helth.Head()

Helth.Detect()

------------My Char.lua---------------------

module(…, package.seeall)

function Char (event)

local Left = display

local Char = display.newImage (“1.png”)
Char.x = display.contentWidth /2
Char.y = display.contentHeight /2
physics.addBody(Char, “dynamic”)
Char.name = “Char”

end


---------My Helth.lua(all the issues are in this file)–

module(…, package.seeall)

function Char (event)

local Left = display

local Char = display.newImage (“1.png”)
Char.x = display.contentWidth /2
Char.y = display.contentHeight /2
physics.addBody(Char, “dynamic”)
Char.name = “Char”

end


[import]uid: 119384 topic_id: 24863 reply_id: 324863[/import]

levelhelper? But you dont there no statement for a collision… [import]uid: 86417 topic_id: 24863 reply_id: 100864[/import]

I would change it to something like this
[lua]display.setStatusBar (display.HiddenStatusBar)

require “physics”
physics.start()

local helth = require “Helth”

local char = require “Char”

local W = display.contentWidth

local H = display.contentHeight

local BG = display.newImageRect( “mainBG.png”, 1024, 768 )
BG.x = 512
BG.y = 320

local Ground = display.newImageRect( “ground.png”, 1024, 64 )
Ground.x = 512
Ground.y = 678
Ground.name = “floor”

local Grass = display.newImageRect( “grass.png”, 1024, 128 )
Grass.x = 512
Grass.y = 755
physics.addBody(Grass, “static”)

char.Char()

helth.Head()

helth.Detect()

------------My Char.lua---------------------

local class1 = {}

function class1.Char ()

local Left = display

local CharImage = display.newImage (“1.png”)
CharImage.x = display.contentWidth /2
CharImage.y = display.contentHeight /2
physics.addBody(CharImage, “dynamic”)
CharImage.name = “Char”

return class1
end


---------My Helth.lua(all the issues are in this file)–

local class2 = {}

function class2.Char ()

local Left = display

local CharImage = display.newImage (“1.png”)
CharImage.x = display.contentWidth /2
CharImage.y = display.contentHeight /2
physics.addBody(CharImage, “dynamic”)
CharImage.name = “Char”
return class2
end[/lua]

I havent tested this but its generally how I would code modules.
Notice I got rid of package.seeall. Using package.seeall will make everything a global in your game. [import]uid: 39088 topic_id: 24863 reply_id: 100865[/import]

I have to look into what Method Mobile says with getting rid of the package.seeall…

---- i dont see where the function Head, and Detect are in your helth.lua file  
Char.Char()  
   
Helth.Head()  
   
Helth.Detect()  
  

For the code you gave us i would have put

 Char.Char()  
 Helth.Char()  

[import]uid: 24708 topic_id: 24863 reply_id: 100902[/import]

Your absolutely right.

I should have put

helth.Char()

Typo

My bad [import]uid: 39088 topic_id: 24863 reply_id: 100916[/import]

Oh I’m so sorry guys, i copied the wrong lua fileXD

This is my real Helth.lua (i posted my char.lua last time XD)

Can you guys see the error in my code:/?

[lua]module(…, package.seeall)

function Head (event)

local Head = display.newImage (“HelthHead.png”)
Head.x = 750
Head.y = 50

local lives = 100

local InfoText = display.newText(lives…" %", 0,0,“Helvetica”,40)
InfoText.x = 850
InfoText.y = 50
end

function Detect (event)
function Char:collision (event)
if event.other.myName == “floor” then

lives = lives -10

end
end
Char:addEventListener(“collision”, Char)
end
[import]uid: 119384 topic_id: 24863 reply_id: 100970[/import]

At the top you say

Ground.name = "floor"  

but later you say

if event.other.myName == "floor"  

i think it should be

if event.other.name == "floor"  

I think everything else looks good… What does the terminal say? [import]uid: 24708 topic_id: 24863 reply_id: 100979[/import]

Hmm thanks, i must have missed that:)
I’m still getting a lot of errors. I will post the output that the terminal is giving me :stuck_out_tongue: I cant find any errors in my code but that beacuse this is the first time im trying to modulize my code (and im pretty new to corona). But mabye you guys can find the issue and help me out :)!

Copyright © 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.704
Runtime error
c:\users-----\desktop\rs modules\Helth.lua:26: attempt to call method ‘ad
dEventListener’ (a nil value)
stack traceback:
[C]: in function ‘addEventListener’
c:\users----\desktop\rs modules\Helth.lua:26: in function ‘Detect’
c:\users--------\deRuntime error: c:\users\phi\desktop\rs modules\Helth.lua:
26: attempt to call method ‘addEventListener’ (a nil value)
stack traceback:
[C]: in function ‘addEventListener’
c:\users-----\desktop\rs modules\Helth.lua:26: in function ‘Detect’
c:\users----\desktop\rs module [import]uid: 119384 topic_id: 24863 reply_id: 100984[/import]

Ops it’s became a doubble post, sorry for that :stuck_out_tongue:
[import]uid: 119384 topic_id: 24863 reply_id: 100986[/import]

As method mobile said at the top, when you use package.seeall you cant have local in front of something that you are calling in a different module…

Char = display.newImage ("1.png")  
Char.x = display.contentWidth /2  
Char.y = display.contentHeight /2  
physics.addBody(Char, "dynamic")  
Char.name = "Char"  

to my understanding this is what it should be… [import]uid: 24708 topic_id: 24863 reply_id: 100987[/import]