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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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 ) 

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,

?:)

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.

@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,

?:)

@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.. :)

@Rob thanks that helps too!

@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?

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,

?:)

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.

@JayantV Make a tutorial with my example!! You want the my files? It sounds very useful!!

Sorry I forgot to say please... :)
Where will the tutorial be?
If I construct it properly I will show you what I did..

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,

?:)

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?

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

cheers,

?:)

File has been SENT!

Thanks for the help!

views:2287 update:2011/10/19 8:59:29
corona forums © 2003-2011