Well, I tried (for now) to use just the plain images, not Particle Candy for the blade effect.
In my touch listener function I have the following piece of code:
[lua]function bgTouch(event)
if event.phase == “began” then
lastX, lastY = nil, nil
isSwipeEnded = false
end
if event.phase == “ended” or event.phase == “canceled” then
bladetip.x, bladetip.y = 0, 0
isSwipeEnded = true
end
if event.phase == “moved” then
if isSwipeEnded then
return
end
if swordSound == nil then
swordSound = audio.play(swordSwipe, {onComplete = function()
swordSound = nil
if not options.infiniteSlashes then
isSwipeEnded = true
end
end})
end
– check touches
bladetip.x, bladetip.y = event.x, event.y
if lastY ~= nil then
local dx, dy = math.abs(event.x - lastX), math.abs(event.y - lastY)
if (dx >= dy) then
local a = (1.0*(event.y - lastY))/(1.0*(event.x - lastX))
local b = lastY
local ang = math.atan2(event.y - lastY, event.x - lastX)
ang = ang/2/math.pi*360
for x=lastX,event.x,(event.x - lastX)/dx*5 do
local y = b + a * (x - lastX)
– print ("==> ", a, b, x - lastX, y)
local t = display.newImageRect(“bladeTrace.png”, 10, 30)
t.rotation = ang
t.x, t.y = x, y
bladetip.parent:insert(t)
transition.to(t, {time=500, alpha = 0, yScale = 0.1, transition = easing.inQuad, onComplete = function()
t:removeSelf()
end})
end
else
local a = (1.0*(event.x - lastX))/(1.0*(event.y - lastY))
local b = lastX
local ang = math.atan2(event.y - lastY, event.x - lastX)
ang = ang/2/math.pi*360
for y=lastY,event.y,(event.y - lastY)/dy*5 do
local x = b + a * (y - lastY)
– print ("==> ", a, b, x - lastX, y)
local t = display.newImageRect(“bladeTrace.png”, 10, 30)
t.rotation = ang
t.x, t.y = x, y
bladetip.parent:insert(t)
transition.to(t, {time=500, alpha = 0, yScale = 0.1, transition = easing.inQuad, onComplete = function()
t:removeSelf()
end})
end
end
end
lastX, lastY = event.x, event.y
end
end[/lua]
So I basically just create an image every 5 points of the trace and then scale it down and fade it out as time passes. This gives a nice result when I test it on a reasonably new device, like iPhone 4S.
However, when tested on the iPhone 3GS, the swipes lag the game a lot. So I might have to take some different approach or check if that lag is about the math above or about the display objects… [import]uid: 120659 topic_id: 33313 reply_id: 132627[/import]