Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you use Mutliple SpriteSheets?

Asked by 5 years ago
Edited 5 years ago

How would I edit this code so I can play a different spritemap after the one thats currently playing?

Thanks!

Code:

    gif = {}

gif.DecalURL1 = "http://www.roblox.com/asset/?id=2991554403"

gif.ImageSize = Vector2.new(800,450) -- The expected crop size for the gif.

gif.MapSize = Vector2.new(4000,2700) -- The expected sprite map size. May get changed if roblox's decal uploader changes the scale.

gif.Frames = 30 -- Number of frames in the gif.

gif.Interval = 0.1 -- How long to wait between each frame.



----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- Figure out the actual assetId



local assetId = tonumber(string.match(gif.DecalURL1,"?id=(%d+)"))

if not assetId then

error("Could not get assetId from supplied url: " .. gif.DecalURL)

end



function getType(a)

local info = game:GetService("MarketplaceService"):GetProductInfo(a)

return info.AssetTypeId

end



while true do

if getType(assetId) == 1 then

gif.SpriteMap = "rbxassetid://" .. assetId

break

else

assetId = assetId - 1

end

end



----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





currentFrame = -1

root = math.floor(math.sqrt(gif.Frames))



function gif:GetImageSize()

if gif.MapSize.X > 1024 or gif.MapSize.Y > 1024 then

local ratio = gif.ImageSize.Y/gif.ImageSize.X

local expected = Vector2.new(1024,1024*ratio)

local alpha = (expected/gif.MapSize).X

return gif.ImageSize * alpha

else

return gif.ImageSize

end

end



function gif:GetRectOffset(frame)

-- Returns an ImageRectOffset to be used by an ImageLabel based on the current frame, and the data supplied above.

local size = gif:GetImageSize()

local x = (currentFrame % root) * size.X

local y = math.floor( (currentFrame / root) ) * size.Y

return Vector2.new(x,y)

end



function gif:NextFrame()

-- Properly increments the frames and returns a RectOffset for that frame.

-- Used for playback.

if currentFrame == gif.Frames-1 then

currentFrame = 0

else

currentFrame = currentFrame + 1

end

return gif:GetRectOffset(currentFrame)

end



function gif:SetFrame(frame)

if type(frame) == "number" then

currentFrame = math.floor(frame)

end

end



function gif:Init(image)

if image:IsA("ImageLabel") or image:IsA("ImageButton") then

local size = gif:GetImageSize()

image.ImageRectSize = size

image.Image = gif.SpriteMap

end

end



function gif:Yield()

-- Waits until "gif.Interval" seconds pass.

local t = tick()

local rs = game:GetService("RunService")

while tick()-t < gif.Interval do

rs.RenderStepped:wait()

end

end



return gif



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Answer this question