A simple demo of using pinch gestures to create fireworks
-- pinch
-- Use this function to perform your initial setup
function setup()
displayMode(FULLSCREEN)
touches={}
sparkles={}
pinchstart=0
pinchgap=0
pg=0
--initialise sounds
sound(SOUND_EXPLODE, 8351,0)
sound(SOUND_SHOOT, 7032,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(0)
--put some text on screen
font("AcademyEngravedLetPlain")
fill(239, 141, 20, 255)
fontSize(64)
text("Fireworks",WIDTH/2,HEIGHT/2)
fontSize(16)
text("Use two-fingered pinch or unpinch/zoom(?) to create fireworks. Size of pinch matters! Single finger touch for sparkler.",WIDTH/2,7*HEIGHT/16)
--some variables
touchcount=0
prevx=0
prevy=0
--loop through current touches
for k,touch in pairs(touches) do
touchcount = touchcount + 1
pg=math.sqrt( (touch.x-prevx)^2+(touch.y-prevy)^2 ) --calculate the distance between the most recent two touches
prevx=touch.x
prevy=touch.y
end
--check if the condition sare right for start of a pinch event and if so then log details
if touchcount==2 and pinchstart==0 then
pinchstart=1
pinchgap=pg
elseif touchcount==1 then
--sparkler
sound(DATA, "ZgNAf0cfDx8TFhhAAAAAANYCnD09GiM+CgBAWEBAQEAYE2sa")
table.insert(sparkles,{
img=readImage("Cargo Bot:Star"),
x=prevx,
y=prevy,
dir=math.random(360),
fade=255,
size=math.random(5),
speed=math.random(3),
r=255,
g=255,
b=255
})
end
--draw the sparkly bits
for s,d in pairs(sparkles) do
--add transparency to the sparkles so it fades away
tint(d.r,d.g,d.b,d.fade)
pushMatrix()
translate(d.x,d.y)
sprite(d.img,0,0,d.size,d.size)
popMatrix()
d.x = d.x + d.speed*math.sin(math.rad(-d.dir))
d.y = d.y + d.speed*math.cos(math.rad(-d.dir))
d.fade = d.fade -5
if d.fade<0 then
table.remove(sparkles,s)
end
end
tint(255)
end
function touched(touch)
if touch.state == ENDED then
if pinchstart==1 then
pinchstart=0 --reset the pinch event
prevx=0
prevy=0
ptouchx=0
ptouchy=0
--calculate final pinch distances
for k,t in pairs(touches) do
pg=math.sqrt( (t.x-prevx)^2+(t.y-prevy)^2 )
prevx=t.x
prevy=t.y
ptouchx = ptouchx + t.x
ptouchy = ptouchy + t.y
end
ptouchx = ptouchx/2
ptouchy = ptouchy/2
--check for a pinch event
if pg<pinchgap and pg<100 then
--pinch
sound(SOUND_SHOOT, 7032,pinchgap/250)
for ang=0,359,10 do
table.insert(sparkles,{
img=readImage("Cargo Bot:Star Filled"),
x=ptouchx,
y=ptouchy,
dir=ang,
fade=175+math.random(50),
size=pinchgap/70+math.random(5),
speed=pinchgap/70+math.random(5),
r=math.random(255),
g=math.random(255),
b=math.random(255)
})
end
--check for an unpinch event
elseif pg>pinchgap then
--expand
sound(SOUND_EXPLODE, 8351,pg/250)
for ang=0,359,5 do
table.insert(sparkles,{
img=readImage("Cargo Bot:Smoke Particle"),
x=ptouchx,
y=ptouchy,
dir=ang,
fade=255,
size=pg/70,
speed=pg/70,
r=255,
g=255,
b=255
})
end
end
end
touches[touch.id] = nil
else
touches[touch.id] = touch
end
end