Rotating both instances of same display object class

I have created a lua file called moonclass.lua which created a display object of a moon i have created two instances of this class, aka two moons, these were created in level1.lua

however when i try to rotate both moons, only the last moon that is created will rotate, regardless of which one i refer the rotation to.

I know I need to create a constructor for my class, however I am quite novice when it comes to LUA. If someone could show me how to create a constructor and how to call it for each instance, I would greatly appriecate it.

moonclasss.lua below:  
local moon = {}  
moon.\_\_index = moon   
local sprite  
local draw = function(x,y,group)  
sprite = display.newImage("image/moon.png")  
sprite.x = x  
sprite.y = y  
physics.addBody( sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )  
group:insert (sprite)  
end  
  
moon.draw = draw  
  
local y = function(y)  
sprite.y = sprite.y + y  
return y  
end  
moon.y = y  
  
local x = function(x)  
sprite.x = sprite.x + x  
return x  
end  
moon.x = x  
  
local rotate = function(rotate)  
sprite.rotation = sprite.rotation + rotate  
return rotate  
end  
moon.rotate = rotate  
  
return moon  

and level1.lua:

module(..., package.seeall)  
local storyboard = require ("storyboard")  
local scene = storyboard.newScene()  
storyboard.purgeScene ("menu")  
local chapter1level1 = require("scene objects").loadlevel()  
display.setStatusBar( display.HiddenStatusBar )  
  
local physics = require("physics")  
physics.start()  
--physics.setDrawMode( "hybrid" )  
physics.setGravity(0, 0)  
  
function scene:createScene(event)  
  
local group = self.view  
local background = display.newGroup()  
local foreground = display.newGroup()  
group:insert(background)  
group:insert(foreground)  
  
--BACKGROUND  
backgroundimage = display.newImage("image/galaxy background.png")  
foreground:insert(backgroundimage)  
--backgroundimage:setReferencePoint(display.BottomLeftReferencePoint)  
backgroundimage.x = display.contentWidth/2  
backgroundimage.y = display.contentWidth/-1  
require "controls"  
  
local moon = require("moonclass")  
moon.draw(0,120,foreground)  
local moon2 = require ("moonclass")  
moon2.draw(600,400,foreground)  
  
local platform = require "platformclass"  
platform.draw(512,200,foreground)  
local platform2 = require "platformclass"  
platform2.draw(50,0,foreground)  
  
local rocket = require "rocketclass"  
rocket.draw(200,600,foreground)  
function gameloop(event)  
  
--if (rockety \> -1920 and rockety \< 320) then  
 --foreground.y = rockety + 320  
--end  
moon.rotate(-0.2)  
moon2.rotate(-0.2)  
  
end  
  
Runtime:addEventListener("enterFrame", gameloop)  
  
end  
  
-- ENTER SCENE --   
function scene:enterScene (event)  
  
local chapter1level1 = require("scene objects").loadlevelenterscene()  
  
  
--]]  
  
end  
-- EXIT SCENE --  
function scene.exitScene(event)  
controlthrust:removeEventListener( "touch", thrust)  
controlleft:removeEventListener( "touch", rotateleft)  
controlright:removeEventListener( "touch", rotateright)  
Runtime:removeEventListener("enterFrame", gameloop)  
end  
  
scene:addEventListener("createScene", scene)  
scene:addEventListener("enterScene", scene)  
scene:addEventListener("exitScene", scene)  
  
return scene   

[import]uid: 166613 topic_id: 35750 reply_id: 335750[/import]

First, you don’t need the module(…, package.seeall) line with storyboard modules. They use the new modern way to create modules and that line is deprecated.

The problem that you’re observing is that modules are only loaded once. So all that:

moon = require("moonclass")  
moon2 = require("moonclass")  

is that you have two references to the same module. Normally your moonclass would have a function called “new” that returns a single instance of a moon and you code would look more like:

moonclass = require("moonclass")  
moon = moonclass.new()  
moon2 = moonclass.new()  

Of course for this too work, your moonclass module would need to implement a .new() function that returned the moon you’re creating. In fact, it will need quite a bit of restructuring.
[import]uid: 199310 topic_id: 35750 reply_id: 142199[/import]

thanks for your reply, in regards to module(…, package.seeall) line, i have already removed the need for this, its just still in there, my bad

i have since updated my code with a constructor shown below, however, i am still getting the same result, the last moon to be called is the only one that will rotate regardless of which one i call to rotate.

i dont know whether i have created my contructor correctly,forgive me im a bit of a novice.

moonclass.lua

local moon = {}  
  
local moon\_mt = { \_\_index = moon } -- metatable  
local sprite  
function moon.new(name) -- constructor  
  
local newMoon = {  
 name = name or "unnamed",  
  
 }  
 return setmetatable( newMoon, moon\_mt)  
end  
  
  
 function moon:draw(x,y,group)  
  
sprite = display.newImage("image/moon.png")  
sprite.x = x  
sprite.y = y  
physics.addBody( sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )  
group:insert (sprite)  
end  
  
function moon:talk() print( self.name .. " is called." )end  
  
function moon:y(y)  
sprite.y = sprite.y + y  
return y  
end  
function moon:x(x)  
sprite.x = sprite.x + x  
return x  
end  
  
function moon:rotate(r)  
sprite.rotation = sprite.rotation + r  
return sprite  
end  
  
return moon  

level 1.lua

[code]
–module(…, package.seeall)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
storyboard.purgeScene (“menu”)
local chapter1level1 = require(“scene objects”).loadlevel()
display.setStatusBar( display.HiddenStatusBar )

local physics = require(“physics”)
physics.start()
–physics.setDrawMode( “hybrid” )
physics.setGravity(0, 0)

function scene:createScene(event)

local group = self.view
local background = display.newGroup()
local foreground = display.newGroup()
group:insert(background)
group:insert(foreground)

–BACKGROUND
backgroundimage = display.newImage(“image/galaxy background.png”)
foreground:insert(backgroundimage)
–backgroundimage:setReferencePoint(display.BottomLeftReferencePoint)
backgroundimage.x = display.contentWidth/2
backgroundimage.y = display.contentWidth/-1
require “controls”
local moon = require (“moonclass”)
local moon1 = moon.new(“moon1”)
moon1:draw(0,400,foreground)
local moon2 = moon.new(“moon2”)
moon2:draw(300,400,foreground)

moon1:talk()
moon2:talk()
local platform = require “platformclass”
platform.draw(512,200,foreground)
local platform2 = require “platformclass”
platform2.draw(50,0,foreground)

local rocket = require “rocketclass”
rocket.draw(200,600,foreground)
function gameloop(event)

–if (rockety > -1920 and rockety < 320) then
–foreground.y = rockety + 320
–end
moon2:rotate(1)
moon1:rotate(-2)

end

Runtime:addEventListener(“enterFrame”, gameloop)

end

– ENTER SCENE –
function scene:enterScene (event)

local chapter1level1 = require(“scene objects”).loadlevelenterscene()
end
– EXIT SCENE –
function scene.exitScene(event)
controlthrust:removeEventListener( “touch”, thrust)
controlleft:removeEventListener( “touch”, rotateleft)
controlright:removeEventListener( “touch”, rotateright)
Runtime:removeEventListener(“enterFrame”, gameloop)
end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)

return scene
[/code] [import]uid: 166613 topic_id: 35750 reply_id: 142201[/import]

Let me get Someone to look at this. I’m not skilled enough with metatables. [import]uid: 199310 topic_id: 35750 reply_id: 142204[/import]

that would be great thanks! [import]uid: 166613 topic_id: 35750 reply_id: 142207[/import]

Oh, while I’m trying to find some help, if anyone in the community wants to take a stab at this, I would appreciate it.
[import]uid: 199310 topic_id: 35750 reply_id: 142221[/import]

what happens if you create 3 moons? [import]uid: 119483 topic_id: 35750 reply_id: 142238[/import]

First, you don’t need the module(…, package.seeall) line with storyboard modules. They use the new modern way to create modules and that line is deprecated.

The problem that you’re observing is that modules are only loaded once. So all that:

moon = require("moonclass")  
moon2 = require("moonclass")  

is that you have two references to the same module. Normally your moonclass would have a function called “new” that returns a single instance of a moon and you code would look more like:

moonclass = require("moonclass")  
moon = moonclass.new()  
moon2 = moonclass.new()  

Of course for this too work, your moonclass module would need to implement a .new() function that returned the moon you’re creating. In fact, it will need quite a bit of restructuring.
[import]uid: 199310 topic_id: 35750 reply_id: 142199[/import]

thanks for your reply, in regards to module(…, package.seeall) line, i have already removed the need for this, its just still in there, my bad

i have since updated my code with a constructor shown below, however, i am still getting the same result, the last moon to be called is the only one that will rotate regardless of which one i call to rotate.

i dont know whether i have created my contructor correctly,forgive me im a bit of a novice.

moonclass.lua

local moon = {}  
  
local moon\_mt = { \_\_index = moon } -- metatable  
local sprite  
function moon.new(name) -- constructor  
  
local newMoon = {  
 name = name or "unnamed",  
  
 }  
 return setmetatable( newMoon, moon\_mt)  
end  
  
  
 function moon:draw(x,y,group)  
  
sprite = display.newImage("image/moon.png")  
sprite.x = x  
sprite.y = y  
physics.addBody( sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )  
group:insert (sprite)  
end  
  
function moon:talk() print( self.name .. " is called." )end  
  
function moon:y(y)  
sprite.y = sprite.y + y  
return y  
end  
function moon:x(x)  
sprite.x = sprite.x + x  
return x  
end  
  
function moon:rotate(r)  
sprite.rotation = sprite.rotation + r  
return sprite  
end  
  
return moon  

level 1.lua

[code]
–module(…, package.seeall)
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
storyboard.purgeScene (“menu”)
local chapter1level1 = require(“scene objects”).loadlevel()
display.setStatusBar( display.HiddenStatusBar )

local physics = require(“physics”)
physics.start()
–physics.setDrawMode( “hybrid” )
physics.setGravity(0, 0)

function scene:createScene(event)

local group = self.view
local background = display.newGroup()
local foreground = display.newGroup()
group:insert(background)
group:insert(foreground)

–BACKGROUND
backgroundimage = display.newImage(“image/galaxy background.png”)
foreground:insert(backgroundimage)
–backgroundimage:setReferencePoint(display.BottomLeftReferencePoint)
backgroundimage.x = display.contentWidth/2
backgroundimage.y = display.contentWidth/-1
require “controls”
local moon = require (“moonclass”)
local moon1 = moon.new(“moon1”)
moon1:draw(0,400,foreground)
local moon2 = moon.new(“moon2”)
moon2:draw(300,400,foreground)

moon1:talk()
moon2:talk()
local platform = require “platformclass”
platform.draw(512,200,foreground)
local platform2 = require “platformclass”
platform2.draw(50,0,foreground)

local rocket = require “rocketclass”
rocket.draw(200,600,foreground)
function gameloop(event)

–if (rockety > -1920 and rockety < 320) then
–foreground.y = rockety + 320
–end
moon2:rotate(1)
moon1:rotate(-2)

end

Runtime:addEventListener(“enterFrame”, gameloop)

end

– ENTER SCENE –
function scene:enterScene (event)

local chapter1level1 = require(“scene objects”).loadlevelenterscene()
end
– EXIT SCENE –
function scene.exitScene(event)
controlthrust:removeEventListener( “touch”, thrust)
controlleft:removeEventListener( “touch”, rotateleft)
controlright:removeEventListener( “touch”, rotateright)
Runtime:removeEventListener(“enterFrame”, gameloop)
end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)

return scene
[/code] [import]uid: 166613 topic_id: 35750 reply_id: 142201[/import]

Let me get Someone to look at this. I’m not skilled enough with metatables. [import]uid: 199310 topic_id: 35750 reply_id: 142204[/import]

that would be great thanks! [import]uid: 166613 topic_id: 35750 reply_id: 142207[/import]

Oh, while I’m trying to find some help, if anyone in the community wants to take a stab at this, I would appreciate it.
[import]uid: 199310 topic_id: 35750 reply_id: 142221[/import]

what happens if you create 3 moons? [import]uid: 119483 topic_id: 35750 reply_id: 142238[/import]