LoveExample-11 Animation

这一节引用了外部库,没看懂,先占个位,再研究。。。

-- Example: Create and use an Animation
require("animation")

function newImagePO2(filename)
	local source = love.image.newImageData(filename)
	local w, h = source:getWidth(), source:getHeight()

	--math.ceil 返回不小于参数的最小整数
	--math.pow(x,y)==x^y:x的y次方
	--math.log(x [, base]):返回以指定底的 x 的对数。 默认的 base 是 e (因此此函数返回 x 的自然对数)。
	-- Find closest power-of-two.
	--local wp = math.pow(2, math.ceil(math.log(w)/math.log(2)))
	--local hp = math.pow(2, math.ceil(math.log(h)/math.log(2)))
	local wp = 2^math.ceil(math.log(w)/math.log(2))
	local hp = 2^math.ceil(math.log(h)/math.log(2))

	-- Only pad if needed:
	if wp ~= w or hp ~= h then
	local padded = love.image.newImageData(wp, hp)
	padded:paste(source, 0, 0)
	return love.graphics.newImage(padded)
	end

	return love.graphics.newImage(source)
end

function love.load()
	-- Set a lovely pink background color.
	love.graphics.setBackgroundColor(246, 198, 222)

	-- Load the source of the animation.
	img = newImagePO2("assets/anim-boogie.png")

	-- Create an animation with a frame size of 32x32 and
	-- 0.1s delay betwen each frame.
	animation1 = newAnimation(img, 32, 32, 0.1, 6)
end

function love.update(dt)
	-- The animation must be updated so it 
	-- knows when to change frames.
	animation1:update(dt)
end

function love.draw()
	-- Draw the animation the center of the screen.
	animation1:draw(400, 300, 0, 1, 1)
end