我创建了一个名为moonclass.lua的lua文件 它创造了一个月亮的显示对象 我创建了这个类的两个实例,也就是两个卫星, 这些是在level1.lua
中创建的然而,当我尝试旋转两个卫星时,只有创建的最后一个月亮会旋转,无论我将旋转指向哪一个。
下面的moonclasss.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
和level1.lua:
local storyboard = require ("storyboard")
local scene = storyboard.newScene()
storyboard.purgeScene ("menu")
display.setStatusBar( display.HiddenStatusBar )
local physics = require("physics")
physics.start()
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.x = display.contentWidth/2
backgroundimage.y = display.contentWidth/-1
require "starclass"
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
答案 0 :(得分:3)
local moon = require("moonclass")
local moon2 = require ("moonclass")
这些行返回相同的对象
require
未加载模块两次
第二个require
只返回第一个require
返回的值
您必须定义构造函数并调用它来创建新对象。
编辑:
local moon = {}
local moon_mt = { __index = moon } -- metatable
function moon.new(name) -- constructor
local newMoon = {
name = name or "unnamed"
}
return setmetatable( newMoon, moon_mt)
end
function moon:draw(x,y,group)
self.sprite = display.newImage("image/moon.png")
self.sprite.x = x
self.sprite.y = y
physics.addBody( self.sprite, "dynamic", { density=1, friction=0.8, bounce=0.05, radius=145 } )
group:insert (self.sprite)
end
function moon:talk() print( self.name .. " is called." )end
function moon:y(y)
self.sprite.y = self.sprite.y + y
return y
end
function moon:x(x)
self.sprite.x = self.sprite.x + x
return x
end
function moon:rotate(r)
self.sprite.rotation = self.sprite.rotation + r
return self.sprite
end
return moon