Main.lua
Code: Select all
require "Sprite.lua"
function love.load()
love.graphics.setBackgroundColor(59,185,255) -- gorgeous sky blue color
grass = Sprite:new(24,12,1,1,"res/grass.png")
end
function love.update(dt)
end
function love.draw()
-- Draw frame (1,1) to position (50,50)
grass:drawFrame(1,1,50,50)
end
Sprite.lua
Code: Select all
require "MiddleClass.lua"
Sprite = class('Sprite')
function Sprite:initialize(frame_w, frame_h, cols, rows, file)
self.sheet = love.graphics.newImage(file)
self.frame_w = frame_w
self.frame_h = frame_h
self.cols = cols
self.rows = rows
end
-- Draw's a specific frame from location (row,col)
-- in our sheet to the screen at coordinates (x,y)
-- Note: First frame is always (1,1) in the sheet.
function Sprite:drawFrame(row,col,x,y)
-- Prevent the user from drawing a
-- frame that does not exist.
if row < 1 or col < 1 or
row > self.rows or col > self.cols then
print("Error (Sprite.lua): You attempted to draw a frame that"..
" does not exist within our sprite-sheet.")
return false
end
local blitQuad = love.graphics.newQuad( row-1 * self.frame_w,
col-1 * self.frame_h,
self.frame_w, self.frame_h,
self.sheet:getWidth(),
self.sheet:getHeight() )
love.graphics.drawq( self.sheet, blitQuad, x, y )
return true
end
Halp!
<3 Ninwa