MovieClip.new: Difference between revisions
From GiderosMobile
m Text replacement - "</source>" to "</syntaxhighlight>" |
No edit summary |
||
| Line 13: | Line 13: | ||
=== Example === | === Example === | ||
'''Frame animations''' | '''Frame animations 1''' | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
--load frames | --load frames | ||
| Line 54: | Line 54: | ||
ballAnimation:setPosition(160, 240) | ballAnimation:setPosition(160, 240) | ||
stage:addChild(ballAnimation) | stage:addChild(ballAnimation) | ||
</syntaxhighlight> | |||
'''Frame animations V2''' | |||
<syntaxhighlight lang="lua"> | |||
local frames = {} | |||
local imgnumstartat, imgnumendat = 1, 20 -- image name sequence (eg.: image004.jpg, image005.jpg, ...) | |||
for i = 1, imgnumendat-imgnumstartat+1 do | |||
local iter = imgnumstartat+i-1 | |||
if iter < 10 then | |||
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_000"..(imgnumstartat+i-1)..".png", true)) | |||
elseif iter < 100 then | |||
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_00"..(imgnumstartat+i-1)..".png", true)) | |||
elseif iter < 1000 then | |||
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_0"..(imgnumstartat+i-1)..".png", true)) | |||
end | |||
frames[i]:setAnchorPoint(0.5, 0.5) | |||
end -- print(#frames) | |||
local anims = {} | |||
local timing = 16 | |||
for i = 1, #frames do | |||
anims[i] = {(i-1)*timing+1, i*timing, frames[i]} | |||
end -- print((#frames-1)*timing+1, #frames*timing) | |||
--print(#anims) --> 20 | |||
-- add anim frames | |||
local mc_jones = MovieClip.new{ | |||
anims[1], -- jones idle | |||
anims[2], | |||
anims[3], | |||
anims[4], | |||
anims[5], | |||
anims[6], | |||
anims[7], | |||
anims[8], | |||
anims[9], -- jones idle2 | |||
anims[10], | |||
anims[11], | |||
anims[12], | |||
anims[13], | |||
anims[14], | |||
anims[15], -- jones idle3 | |||
anims[16], | |||
anims[17], | |||
anims[18], | |||
anims[19], | |||
anims[20], | |||
} | |||
mc_jones:stop() -- don't play on start | |||
mc_jones:setPosition(160, 240) | |||
stage:addChild(mc_jones) | |||
-- key down event | |||
stage:addEventListener(Event.KEY_DOWN, function(e) | |||
if e.keyCode == KeyCode.F then -- jones idle | |||
mc_jones:setGotoAction(8*timing, 1) | |||
mc_jones:gotoAndPlay(1) | |||
elseif e.keyCode == KeyCode.G then -- jones idle2 | |||
mc_jones:setGotoAction(14*timing, 9*timing) | |||
mc_jones:gotoAndPlay(9*timing) | |||
elseif e.keyCode == KeyCode.H then -- jones idle3 | |||
mc_jones:setGotoAction(20*timing, 15*timing) | |||
mc_jones:gotoAndPlay(15*timing) | |||
end | |||
end) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
=== See also === | === See also === | ||
'''[[Animated_Sprite_Factory]]''' | '''[[Animated_Sprite_Factory]]''' for spritesheets | ||
{{MovieClip}} | {{MovieClip}} | ||
Revision as of 01:41, 1 October 2023
Available since: Gideros 2011.6
Class: MovieClip
Description
Creates a new MovieClip object. After constructing the MovieClip object, it starts playing. You don't need to call MovieClip:play.
MovieClip.new{timeline} -- *PLEASE NOTICE THE BRACKETS { } INSTEAD OF ( )*
Parameters
timeline: (table) array of timeline elements
Example
Frame animations 1
--load frames
local frames = {}
local bmp
for i = 1, 6 do
bmp = Bitmap.new(Texture.new("animation/ball"..i..".png", true))
bmp:setAnchorPoint(0.5, 0.5)
frames[#frames+1] = bmp
end
--arrange frames
local ballAnimation = MovieClip.new{
{1, 5, frames[1]},
{6, 10, frames[2]},
{11, 15, frames[3]},
{16, 20, frames[4]},
{21, 25, frames[5]},
{26, 30, frames[4]},
{31, 35, frames[6]},
{36, 40, frames[4]},
{41, 45, frames[5]},
{46, 50, frames[4]},
{51, 55, frames[6]},
{56, 60, frames[4]},
{61, 65, frames[5]},
{66, 70, frames[4]},
{71, 75, frames[6]},
{76, 80, frames[3]},
{81, 85, frames[2]},
{86, 150, frames[1]}
}
--loop animation
ballAnimation:setGotoAction(150, 1)
--start playing
ballAnimation:gotoAndPlay(1)
ballAnimation:setPosition(160, 240)
stage:addChild(ballAnimation)
Frame animations V2
local frames = {}
local imgnumstartat, imgnumendat = 1, 20 -- image name sequence (eg.: image004.jpg, image005.jpg, ...)
for i = 1, imgnumendat-imgnumstartat+1 do
local iter = imgnumstartat+i-1
if iter < 10 then
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_000"..(imgnumstartat+i-1)..".png", true))
elseif iter < 100 then
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_00"..(imgnumstartat+i-1)..".png", true))
elseif iter < 1000 then
frames[#frames+1] = Bitmap.new(Texture.new("gfx/hero/Jones_base_0"..(imgnumstartat+i-1)..".png", true))
end
frames[i]:setAnchorPoint(0.5, 0.5)
end -- print(#frames)
local anims = {}
local timing = 16
for i = 1, #frames do
anims[i] = {(i-1)*timing+1, i*timing, frames[i]}
end -- print((#frames-1)*timing+1, #frames*timing)
--print(#anims) --> 20
-- add anim frames
local mc_jones = MovieClip.new{
anims[1], -- jones idle
anims[2],
anims[3],
anims[4],
anims[5],
anims[6],
anims[7],
anims[8],
anims[9], -- jones idle2
anims[10],
anims[11],
anims[12],
anims[13],
anims[14],
anims[15], -- jones idle3
anims[16],
anims[17],
anims[18],
anims[19],
anims[20],
}
mc_jones:stop() -- don't play on start
mc_jones:setPosition(160, 240)
stage:addChild(mc_jones)
-- key down event
stage:addEventListener(Event.KEY_DOWN, function(e)
if e.keyCode == KeyCode.F then -- jones idle
mc_jones:setGotoAction(8*timing, 1)
mc_jones:gotoAndPlay(1)
elseif e.keyCode == KeyCode.G then -- jones idle2
mc_jones:setGotoAction(14*timing, 9*timing)
mc_jones:gotoAndPlay(9*timing)
elseif e.keyCode == KeyCode.H then -- jones idle3
mc_jones:setGotoAction(20*timing, 15*timing)
mc_jones:gotoAndPlay(15*timing)
end
end)
See also
Animated_Sprite_Factory for spritesheets