Spel42: Animering

image

Vi programmerar animering genom sekvens av sprites nedladdade som sprite sheet som uppdelas i enskilda bilder genom online decomposition.

Titta på en animering här. Jämför med Running1 på App Store.

Template:

–Animation

— Use this function to perform your initial setup
function setup()

parameter.integer(“frameDelay”, 1,50, 10)
animation = Animation({“Dropbox:lion1″,”Dropbox:lion2″,”Dropbox:lion3”,
“Dropbox:lion4″,”Dropbox:lion5″,”Dropbox:lion6”,
“Dropbox:lion7″,”Dropbox:lion8”},10)

n=0
m=0

end

— This function gets called once every frame
function draw()
n=n+1
if n>700 then
n=1
end
background(225, 219, 224, 255)
sprite(“Dropbox:landscape1”,500-n,300,2000)
animation:setFrameDelay(frameDelay)
animation:draw(WIDTH / 2, HEIGHT / 4)
font(“AmericanTypewriter”)
fontSize(50)
fill(255, 0, 28, 255)
text(math.floor(m),300,600)
end

Animation = class()

function Animation:init(frames, delay)
self.frames = frames — table of sprite names
self.currentFrame = 1 — the current frame
self.frameDelay = delay — how many iterations of main draw loop before changing frame
self.count = 1 — keeps track of iterations passed
end

function Animation:draw(x, y)

print(m)
— Increment the counter
self.count = self.count + 1
— Check if frame should be updated
if self.count > self.frameDelay then
— Reset count and increment current frame
self.count = 1
self.currentFrame = self.currentFrame + 1
— Check if we’ve reached the last frame
if self.currentFrame > table.maxn(self.frames) then
— Back to first frame
self.currentFrame = 1
end
end
m=m+1/80
— Draw correct frame
sprite(self.frames[self.currentFrame], x, y,200)
end

function Animation:setFrameDelay(newDelay)
self.frameDelay = newDelay
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s