I am a recent starter to Corona and Lua, coming from a web development background.
I’ve been reading about making classes and having a bit of trouble understand how best to access methods and properties. I’ll use my cose as it is for this example. Basically, I would like to create a new ‘player’, display it on the screen and add it as a physics object.
In main.lua:
[lua]require(“physics”)
physics.start()
physics.setDrawMode(“hybrid”)
physics.setScale(60)
physics.setGravity(0,0)
Player = require(“player”)
local player = Player:init()[/lua]
In player.lua
[lua]local Player = {}
local Player_mt = {__index = Player}
— Create a new instance of a Player object.
function Player:init()
local self = {}
setmetatable( self, Camera_mt )
self._view = display.newGroup()
local playerCollisionFilter = {categoryBits = 2, maskBits = 5}
local playerBodyElement = {filter = playerCollisionFilter}
local imageSheet = graphics.newImageSheet(“spriteCharacter.png”, {
width = 80,
height = 56,
numFrames = 6,
sheetContentWidth = 240,
sheetContentHeight = 112
})
local character = display.newSprite(imageSheet, {
name=“walking”,
start=1,
count=4,
time=420,
loopCount = 0,
loopDirection = “forward”
})
self._view:setReferencePoint(display.CenterReferencePoint)
self.isBullet = true
self.objectType = “player”
self.isVisible = true
self.x = 100
self.y = 100
physics.addBody(self._view,“dynamic”,playerBodyElement)
self._view:insert(character)
character:play()
return self
end
return Player[/lua]
Currently, I can’t see anything on the screen, and I don’t think this is correct. I want my player.lua file to handle player methods and be able to alter properties of the player object (health, visibility, scale etc) but main.lua needs to be able to get properties such as x and y position.
Can someone see where I am going wrong with this? I would like to keep it as object oriented as I can and get into good coding habits from the start.
Thanks [import]uid: 140429 topic_id: 24526 reply_id: 324526[/import]
[import]uid: 52491 topic_id: 24526 reply_id: 99312[/import]