Pharan đã viếtAnimations on different track numbers shouldn't affect each other in terms of what events they fire.
After further testing it seems like only the animation with the highest track index fires off the onEvent. Im probably doing something wrong, or maybe it's a bug?
Ive modified the demo program to show whats happening, heres the code. Would also be glad to share the whole project file if that would help.
local spine = require "spine-corona.spine"
local skeletons = {}
local activeSkeleton = 1
local lastTime = 0
function loadSkeleton(atlasFile, jsonFile, x, y, scale, animation, skin)
---
to load an atlas, we need to define a function that returns
---
a Corona paint object. This allows you to resolve images
---
however you see fit
local imageLoader = function (path)
local paint = { type = "image", filename = "data/" .. path }
return paint
end
---
load the atlas
local atlas = spine.TextureAtlas.new(spine.utils.readFile("data/" .. atlasFile), imageLoader)
---
load the JSON and create a Skeleton from it
local json = spine.SkeletonJson.new(spine.AtlasAttachmentLoader.new(atlas))
json.scale = scale
local skeletonData = json:readSkeletonDataFile("data/" .. jsonFile)
local skeleton = spine.Skeleton.new(skeletonData)
skeleton.flipY = true
---
Corona's coordinate system has its y-axis point downwards
skeleton.group.x = x
skeleton.group.y = y
---
Set the skin if we got one
if skin then skeleton:setSkin(skin) end
---
create an animation state object to apply animations to the skeleton
local animationStateData = spine.AnimationStateData.new(skeletonData)
animationStateData.defaultMix = 0.2
local animationState = spine.AnimationState.new(animationStateData)
---
set the skeleton invisible
skeleton.group.isVisible = false
---
set a name on the group of the skeleton so we can find it during debugging
skeleton.group.name = jsonFile
---
on event listener
animationState.onEvent = function (entry, event)
print(entry.trackIndex.." event: "..entry.animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'")
end
---
return the skeleton an animation state
return { skeleton = skeleton, state = animationState }
end
table.insert(skeletons, loadSkeleton("Male Character Side.atlas", "Male Character Side.json", 240, 300, 0.4, "walk"))
local offset = {}
local size = {}
skeletons[1].skeleton:getBounds(offset, size)
display.setDefault("background", 0.2, 0.2, 0.2, 1)
Runtime:addEventListener("enterFrame", function (event)
local currentTime = event.time / 1000
local delta = currentTime - lastTime
lastTime = currentTime
skeleton = skeletons[activeSkeleton].skeleton
skeleton.group.isVisible = true
state = skeletons[activeSkeleton].state
state:update(delta)
state:apply(skeleton)
skeleton:updateWorldTransform()
end)
attackButton = display.newRect( 50,50, 50, 50 )
function attackButton:touch(event)
if(event.phase=="began")then
print "attack button"
state:setAnimationByName(0, "Side Thrust", false)
end
end
attackButton:addEventListener( "touch" )
walkButton = display.newRect( 400,50, 50, 50 )
function walkButton:touch(event)
if(event.phase=="began")then
print "walk button"
state:setAnimationByName(1, "Side Walk", false)
end
end
walkButton:addEventListener( "touch" )
Can anyone at least confirm, that in Corona, they have two animations running in separate tracks with both firing off there onEvent events?
Any reason why the code I posted above wouldn't work? Any Corona users encounter this problem?