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]