Making spawn items disappear

I have a click event attached to a frog and flys flying in from the left of the screen. When the click event is “ended” the frog shoots out his tongue to catch a fly… Does anybody know how I can have a fly’s alpha transition down to 0 when the player clicks on a frog? I know how to do it if it wasn’t part of a spawn function.

Here is my code:

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

local rand = math.random

local i = 1

local flysCaught = 0

local loqsprite = require(‘loq_sprite’)

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 ffactory = loqsprite.newFactory(“frog”)
local frog = ffactory:newSpriteGroup()
frog.x = 640 ; frog.y = 470

local numberCount = display.newText(string.format("%02d", 0), 0, 0, native.systemFontBold, 40)
numberCount.x = frog.x; numberCount.y = frog.y

local hitArea = display.newRect( 0, 0,display.contentWidth, 250 )
hitArea:setFillColor( 255, 255, 255 )
hitArea.alpha = 0

function letsCount ()

flysCaught = flysCaught + 1
numberCount.text = string.format("%02d", flysCaught)
–transition.to(numberCount, { time=1500, y =100, alpha = 0} )

end

local function removeFly( _t)
_t:removeSelf()
_t = nil
print(“fly removed”)
end

local function spawnFlys()
local flyfactory = loqsprite.newFactory(“fly”)
local fly = flyfactory:newSpriteGroup()
fly.x = rand(300,380); fly.y = rand(300,380)
fly:play(“aniFly fly”)
local moveSpeed = rand(2500, 45000)
transition.to(fly, {time=moveSpeed, x=1550, onComplete=removeFly})
end

function hitTestObjects(obj1, obj2)
local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin
local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax
local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin
local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax
return (left or right) and (up or down)
end
catchFlys = function( event )

local phase = event.phase

local function onContact()

local eatFly = hitTestObjects( frog, hitArea)

if (eatFly == true) then
print(“fly cought”)
letsCount ()
end

end

if ( phase == “ended” ) then

frog:play(“aniFrog eat”)
timer.performWithDelay(15, onContact, 1 )

end
end

timer.performWithDelay(50, spawnFlys,20)
frog:addEventListener( “touch”, catchFlys ) [/lua]

Also I want to have a little mor control on how the flys come out… I want them to just show up 1 at a time instead of little bunches.

Thanks

[import]uid: 51459 topic_id: 16338 reply_id: 316338[/import]

Use an array, store the handle to the fly in the array and also the transition handle, so that when the fly is *caught* you can cancel the transition and remove the fly or set the alpha to zero.

You can also (as the levels progress) have *more* *flies* buzzing around the screen if you store the handle in an array.

another question out of curiosity, you are checking the Frog and the hitArea which is a fixed position rect on screen, how would you know where the fly is at a particular point of time?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 60876[/import]

drop a transition.to() in your onContact() function that transition’s the alpha to 0

transition.to(event.target, {time=500, alpha=0.0, onComplete=removeFly})

or something similar. [import]uid: 19626 topic_id: 16338 reply_id: 60878[/import]

@Rob, but when you have several flies on screen, how would you know which fly it is that needs the transition.to and what if by the time the transition is in progress, the main transition finishes and removes the object?

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 60880[/import]

@JayantV This is a kids app and I am just trying to give the kids the illusion that a fly is being *caught*… I like the array Idea! I’m going to give that a try… :slight_smile:

@Rob thanks that helps too! [import]uid: 51459 topic_id: 16338 reply_id: 60883[/import]

@JayantV Is there a way to set up an array of flys… Have one come out every 10 seconds… Then see how much time it takes for the fly to hit a specific area on the screen and have the function work when you click on the frog at the right time? [import]uid: 51459 topic_id: 16338 reply_id: 60884[/import]

you can create an object called fly, that can take a few parameters like x, y, speed, etc

you can then create an array local flies= {} and populate it with the fly object.

you can choose to keep the transition.to or use an enterFrame to move the fly by the speed.

you can then also check that if the fly is on the hitArea and the user tapped the frog, it should dart its tongue and eat the fly.

You can even have a math.random(1,6) + 9 to set the duration of the flies spawned to be 10 - 15 seconds and set them with random speeds too.

hope that gives you something to work on,

I might actually make a tutorial, with that idea (and your permission to use this example, otherwise I will have to come up with a similar example) that demonstrates what I just told you.
cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 60886[/import]

you will have to keep track of your flies in an array. Right now your hit logic isn’t checking which fly you hit anyway, so once you work it out, you will know which fly you hit and then you just transition that fly.

[import]uid: 19626 topic_id: 16338 reply_id: 60887[/import]

@JayantV Make a tutorial with my example!! You want the my files? It sounds very useful!! [import]uid: 51459 topic_id: 16338 reply_id: 60889[/import]

Sorry I forgot to say please… :slight_smile:
Where will the tutorial be?
If I construct it properly I will show you what I did… [import]uid: 51459 topic_id: 16338 reply_id: 60891[/import]

For *not* knowing where my tutorials are… Hmmmmmmmm

I have all my Tutorials on http://howto.oz-apps.com

You can send me the graphics for the Frog and the Flies, let me see what I can do over the weekend, as I will not make the whole game, but the bit that I just said.

spawn flies at random, the frog eats them when tapped on at the right time and the flies have random speeds.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 60892[/import]

I must be blind or something… I looked all over your website and can’t find your contact information anywhere… I see a place where I can leave a comment… Can I give you files on this forum? [import]uid: 51459 topic_id: 16338 reply_id: 60902[/import]

send it to me at dev [@] oz-apps [.] com

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 60903[/import]

File has been SENT!

Thanks for the help! [import]uid: 51459 topic_id: 16338 reply_id: 60907[/import]

tutorial posted, Jake, have a look

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16338 reply_id: 61038[/import]