Timed Event

The code above has hands moving across the screen and a print statement telling me when they have passed a hit spot. I also have a touch event that plays a animation when clicked. What I want to accomplish is a timing event. I only want the animation to happen when you have clicked on the screen when the hands are inside the hit spot… Not sure how to go about this, so any help will be much appreciated… Thanks

[lua]-- LOAD PARTICLE LIB
local loqsprite = require(‘loq_sprite’)
local physics = require (“physics”)
local director = require(“director”)
physics.start()
physics.setGravity(0,0)

local rand = math.random

local moveSpeed = 4000

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local girl = display.newImageRect(“girl.png”, 512, 785)
girl.x = 508; girl.y = 563

local hands = display.newImageRect(“hands.png”, 200, 127)
hands.x =0
hands.y = 700
physics.addBody (hands, {radius = sensorRect, isSensor = true})
hands.myName = “hands”

function moveLeft()
transition.to(hands, {time=moveSpeed, x = 10, y = hands.y, onComplete = moveRight})
end

function moveRight()
transition.to(hands, {time=moveSpeed, x = 1024, y = hands.y, onComplete = moveLeft})
end

local wfactory = loqsprite.newFactory(‘water’)
local water = wfactory:newSpriteGroup()
water.x = 508; water.y = 450
water.alpha = 0
local hitSpot = display.newImageRect(“hitSpot.png”, 240, 180)
hitSpot.x = 510; hitSpot.y = 675
hitSpot.alpha = .5
physics.addBody (hitSpot, {radius = sensorRect, isSensor = true})
hitSpot.myName = “hitSpot”

local screenTouch = function( _e )
if _e.phase == “ended” then
water:play(“water splash”)
water.alpha = 1
elseif _e.phase == “began” then
print(“begin touch”)

end
end

function hitSpot:collision(_e)

if _e.other.myName == “hands” then
print(“hands Passed”)

end
end
hitSpot:addEventListener(“collision”, hitSpot)
Runtime:addEventListener (“touch”, screenTouch)
moveRight()[/lua] [import]uid: 51459 topic_id: 14163 reply_id: 314163[/import]

I made a few changes to the code… Added a dispatch function which plays all the animations if clicked at the right time… Not sure how to track that the mouse has been clicked at the right time can somebody please help!!

[lua]display.setStatusBar( display.HiddenStatusBar ) – HIDE STATUS BAR

– LOAD PARTICLE LIB
local loqsprite = require(‘loq_sprite’)
local physics = require (“physics”)
local director = require(“director”)
physics.start()
physics.setGravity(0,0)

local rand = math.random

local moveSpeed = 4000

local screenW = display.contentWidth
local screenH = display.contentHeight

local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

local gfactory = loqsprite.newFactory(‘girl’)
local girl = gfactory:newSpriteGroup()
girl.x = 508; girl.y = 443

local wfactory = loqsprite.newFactory(‘water’)
local water = wfactory:newSpriteGroup()
water.x = 300; water.y = 310
water.alpha = 0

local hfactory = loqsprite.newFactory(‘hands’)
local hands = hfactory:newSpriteGroup()
hands.x =130; hands.y = 460
physics.addBody (hands, {radius = sensorRect, isSensor = true})
hands.myName = “hands”
atrace(xinspect(hands:getSpriteNames()))

function moveLeft()
transition.to(hands, {time=moveSpeed, x = 130, y = hands.y, onComplete = moveRight})
end

function moveRight()
transition.to(hands, {time=moveSpeed, x = 870, y = hands.y, onComplete = moveLeft})
end
local hitSpot = display.newImageRect(“hitSpot.png”, 240, 150)
hitSpot.x = 510; hitSpot.y = 555
hitSpot.alpha = 0
physics.addBody (hitSpot, {radius = sensorRect, isSensor = true})
hitSpot.myName = “hitSpot”
local screenTouch = function( _e )
if _e.phase == “ended” then

dispatcher()

end
end

function dispatcher()
water:play(“water splash”)
water.alpha = 1
hands:play(“handmove splash”)
girl:play(“washFace cleanFace”)
end

function hitSpot:collision(_e)

if _e.other.myName == “hands” then
print(“hands Passed”)

end
end
hitSpot:addEventListener(“collision”, hitSpot)
Runtime:addEventListener (“touch”, screenTouch)
moveRight()[/lua] [import]uid: 51459 topic_id: 14163 reply_id: 52160[/import]

Set a flag so that when the hand is in the hit spot, handActive = true (or whatever) and then when it’s out, handActive = false.

Then add a function where if handActive == true you do your animation.

Make sense? :slight_smile: [import]uid: 52491 topic_id: 14163 reply_id: 52277[/import]