...添加 表示背景圖片的類:函數
-- Background.lua
Background = {} function Background:new( imgFileName, maxWidth, maxHeight ) local o = {} setmetatable( o, self) self.__index = self if imgFileName then o:SetImage( imgFileName ) end return o end function Background:SetImage( imgFileName, maxWidth, maxHeight) self.drawable = love.graphics.newImage( imgFileName ) self.scaleX = maxWidth / self.drawable:getWidth(); self.scaleY = maxHeight / self.drawable:getHeight(); end function Background:draw() love.graphics.draw( self.drawable, 0, 0, 0, self.scaleX, self.scaleY, 0, 0 ) end
創景基類:ui
-- scene.lua require( "background") Scene = {} function Scene:new( o) o = o or { desc = nil, background = nil, } setmetatable( o, self) self.__index = self o:_Init() return o end function Scene:_Init() self.desc = "scene" self.background = Background:new() end function Scene:Initialize() end function Scene:SetBackgroundImg( imgFileName, maxWidth, maxHeight ) self.background:SetImage( imgFileName, maxWidth, maxHeight ) end function Scene:SetBackground( background) self.background = background end function Scene:draw() end
歡迎界面 的場景:lua
--welcone.lua WelcomeScene = Scene:new() function WelcomeScene:new() local o = {} setmetatable( o, self) self.__index = self o:Initialize() return o end function WelcomeScene:Initialize() self:SetBackgroundImg( "img/loading.jpg", g.width, g.height ) end function WelcomeScene:draw() self.background:draw() end
主函數變動爲:spa
require( "global") require( "function") function love.load() love.graphics.setMode( g.width, g.height) InitializeScenes() Log( "load...") g.scene = LoadScene( SCENE_WELCOME) end function love.draw() g.scene:draw() end function love.update(dt) end
運行效果將如:
code
源代碼: http://files.cnblogs.com/Wilson-Loo/XGame.0827.zipblog
添加按鈕操做, 使進入第二個場景:事件
function WelcomeScene:mousepressed(x, y, button) if self.btnStart:mousepressed( x, y, button) then g.scene = LoadScene( SCENE_1) end end
-- scene_1.lua require( "button") Scene_1 = Scene:new() function Scene_1:new() local o = {} setmetatable( o, self) self.__index = self o:Initialize() return o end function Scene_1:Initialize() self:SetBackgroundImg( "img/1_1.jpg", g.width, g.height ) end function Scene_1:draw() self.background:draw() end function Scene_1:update(dt) end function Scene:mousepressed(x, y, button) end
至此, 完成了簡單的 場景變換 和 鼠標按鈕事件.圖片