Fixed spine-corona skins, slots.

This commit is contained in:
NathanSweet 2013-04-27 13:48:36 +02:00
parent 5ed819ba84
commit 2c3453f4dd
5 changed files with 31 additions and 19 deletions

View File

@ -1,21 +1,25 @@
local spine = require "spine-corona.spine"
--local name = "goblins"
local name = "spineboy"
local json = spine.SkeletonJson.new()
json.scale = 1
local skeletonData = json:readSkeletonDataFile("data/spineboy.json")
local skeletonData = json:readSkeletonDataFile("data/" .. name .. "/" .. name .. ".json")
local walkAnimation = skeletonData:findAnimation("walk")
local skeleton = spine.Skeleton.new(skeletonData)
function skeleton:createImage (attachment)
-- Customize where images are loaded.
return display.newImage("data/" .. attachment.name .. ".png")
return display.newImage("data/" .. name .. "/" .. attachment.name .. ".png")
end
skeleton.x = 150
skeleton.y = 325
skeleton.group.x = 150
skeleton.group.y = 325
skeleton.flipX = false
skeleton.flipY = false
skeleton.debug = true -- Omit or set to false to not draw debug lines on top of the images.
if name == "goblins" then skeleton:setSkin("goblingirl") end
skeleton:setToBindPose()
local lastTime = 0
@ -31,3 +35,4 @@ Runtime:addEventListener("enterFrame", function (event)
walkAnimation:apply(skeleton, animationTime, true)
skeleton:updateWorldTransform()
end)

View File

@ -59,8 +59,8 @@ spine.Skeleton.new_super = spine.Skeleton.new
function spine.Skeleton.new (skeletonData, group)
-- Skeleton extends a group.
local self = spine.Skeleton.new_super(skeletonData)
self = spine.utils.copy(self, group or display.newGroup())
self.group = group or display.newGroup()
-- createImage can customize where images are found.
function self:createImage (attachment)
return display.newImage(attachment.name .. ".png")
@ -76,12 +76,12 @@ function spine.Skeleton.new (skeletonData, group)
for i,slot in ipairs(self.drawOrder) do
local attachment = slot.attachment
local image = images[attachment]
local image = images[slot]
if not attachment then
-- Attachment is gone, remove the image.
if image then
image:removeSelf()
images[attachment] = nil
images[slot] = nil
end
else
-- Create new image.
@ -95,7 +95,7 @@ function spine.Skeleton.new (skeletonData, group)
print("Error creating image: " .. attachment.name)
image = spine.Skeleton.failed
end
images[attachment] = image
images[slot] = image
end
-- Position image based on attachment and bone.
if image ~= spine.Skeleton.failed then
@ -113,7 +113,7 @@ function spine.Skeleton.new (skeletonData, group)
image.rotation = -image.rotation
end
image:setFillColor(slot.r, slot.g, slot.b, slot.a)
self:insert(image)
self.group:insert(image)
end
end
end
@ -137,13 +137,13 @@ function spine.Skeleton.new (skeletonData, group)
bone.line.yScale = 1
end
bone.line:setColor(255, 0, 0)
self:insert(bone.line)
self.group:insert(bone.line)
if not bone.circle then bone.circle = display.newCircle(0, 0, 3) end
bone.circle.x = bone.worldX
bone.circle.y = -bone.worldY
bone.circle:setFillColor(0, 255, 0)
self:insert(bone.circle)
self.group:insert(bone.circle)
end
end
end

View File

@ -100,20 +100,25 @@ function Skeleton.new (skeletonData)
if not attachmentName then error("attachmentName cannot be nil.", 2) end
local slotIndex = self.data:findSlotIndex(slotName)
if slotIndex == -1 then error("Slot not found: " .. slotName, 2) end
if self.skin then return self.skin:getAttachment(slotIndex, attachmentName) end
if self.data.defaultSkin then
local attachment = self.data.defaultSkin:getAttachment(slotIndex, attachmentName)
if self.skin then
local attachment = self.skin:getAttachment(slotIndex, attachmentName)
if attachment then return attachment end
end
if self.data.defaultSkin then
return self.data.defaultSkin:getAttachment(slotIndex, attachmentName)
end
return nil
end
function self:setAttachment (slotName, attachmentName)
if not slotName then error("slotName cannot be nil.", 2) end
if not attachmentName then error("attachmentName cannot be nil.", 2) end
for i,slot in ipairs(self.slots) do
if slot.data.name == slotName then
slot:setAttachment(self:getAttachment(slotName, attachmentName))
if not attachmentName then
slot:setAttachment(nil)
else
slot:setAttachment(self:getAttachment(slotName, attachmentName))
end
return
end
end

View File

@ -241,7 +241,7 @@ function SkeletonJson.new (attachmentLoader)
for i,valueMap in ipairs(values) do
local time = valueMap["time"]
local attachmentName = valueMap["name"]
if attachmentName == json.null then attachmentName = nil end
if not attachmentName then attachmentName = nil end
timeline:setKeyframe(keyframeIndex, time, attachmentName)
keyframeIndex = keyframeIndex + 1
end

View File

@ -61,7 +61,9 @@ function Slot.new (slotData, skeleton, bone)
self:setColor(data.r, data.g, data.b, data.a)
local attachment
if data.attachmentName then attachment = self.skeleton:getAttachment(data.name, data.attachmentName) end
if data.attachmentName then
attachment = self.skeleton:getAttachment(data.name, data.attachmentName)
end
self:setAttachment(attachment)
end