MovieClip.new

From GiderosMobile
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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},holdWhilePlaying)

Parameters

timeline: (table) array of timeline elements
holdWhilePlaying: (bool) keep a reference to itself while it is playing optional, default=false

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, animating with 2 movieclips

-- first we create the anims for the player
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 = 8
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, true)
mc_jones:setGotoAction(14*timing, 9*timing)
mc_jones:gotoAndPlay(9*timing)

-- 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)

-- then we move the player using another movieclip
local timing2 = 500
local mc2 = MovieClip.new{
	{1, timing2/2, mc_jones, {x={160, 240, "inOutExponential"}}},
	{timing2/2+1, timing2, mc_jones, {x={240, 160, "inOutExponential"}}},
}

mc2:setGotoAction(timing2, 1)
mc2:setPosition(160, 240)
stage:addChild(mc2)

See also

Animated_Sprite_Factory for spritesheets