spawn Flies help

What I am trying to accomplish is simple… Basically I want the flies that the player has missed to come back somehow so that the player has an infinite amount of chances to catch all the flies.

I could do something like this

[lua]function transition_to_left()
transition.to(aFly, {time=2000, x = 0, y = aFly.y, onComplete = transition_to_right})
end

function transition_to_right()
transition.to(aFly, {time=2000, x = 2000, y = aFly.y, onComplete = transition_to_left})
end
transition_to_right()[/lua]

The only problem is I don’t know how to add it to the game I already have done…

CODE:

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

–Math Functions
local random = math.random

–requires Loq Sprite
local loqsprite = require(‘loq_sprite’)

–forward Declaration
local caughtFly, removeFly, spawnFly, catchFly
local onEnterFrame

–Variable we need for the game
local i
local fliesCaught = 0
local flies = {}
local fly
local maxFlies = 10
local fliesOnScreen = 0
local _fly={}
local counter = 0
local timeBetweenFlies = 1
local tries = 10

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

–Start creating the Elements on screen
–The background
local BG = display.newImageRect(“background.png”, 1024, 768)
BG.x = screenW/2; BG.y = screenH/2

–The Frog
local ffactory = loqsprite.newFactory(“frog”)
local frog = ffactory:newSpriteGroup()
frog.x = 640 ; frog.y = 470

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

function caughtFly(thisFly)
fliesCaught = fliesCaught + 1
–update the score
numberCount.text = string.format("%02d", fliesCaught)

–and let us remove this fly
removeFly(thisFly)
end

function removeFly(thisOne)
thisOne:removeSelf()
flies[thisOne.ID] = nil
thisOne = nil
–print(“Fly Removed”)
fliesOnScreen = fliesOnScreen - 1
end

function spawnFly()
if fliesOnScreen >= maxFlies then return end

local theFly = _fly.new()
if theFly == nil then return end

theFly:play(“aniFly fly”)
flies[theFly.ID] = theFly

fliesOnScreen = fliesOnScreen + 1

–print(“Flies on screen”, fliesOnScreen)
end

local function checkIfAnyFlyIsWithin(x1, x2)
local k,v
for k,v in pairs(flies) do
if v.x > x1 and v.x < x2 then
return v
end
end
end

local function catchFly(event)
–Play the frog aniamtion
frog:play(“aniFrog eat”)

–Determine if the fly is at the position where the frogs tongue is…
–1240 - 1280 is the window that we offer for the frog to catch the fly.

local theFly = checkIfAnyFlyIsWithin(1240,1300)
if theFly ~= nil then
caughtFly(theFly)
end
end

function onEnterFrame(event)
–the heart beat of this app
if counter >= tries and fliesOnScreen == 0 then
print(“Game Over”)

local text = display.newText(“Game Over”, 0,0, native.systemFontBold, 256)
text:scale(0.5,0.5)
text.x = screenW/2
text.y = screenH/2
text.alpha=0
transition.to(text,{time=1000, alpha=1})

Runtime:removeEventListener(“enterFrame”,onEnterFrame)
end

if fliesOnScreen < maxFlies then
timeBetweenFlies = timeBetweenFlies - 1
if timeBetweenFlies==0 then
spawnFly()
timeBetweenFlies = random(1,10)+20
end
end

local k,v
for k,v in pairs(flies)do
local theFly = v
theFly.x = theFly.x + theFly.speed
if theFly.x > 1550 then
theFly.canMove = false
flies[k]=nil
removeFly(v)
end
end

end

Runtime:addEventListener(“enterFrame”,onEnterFrame)
frog:addEventListener(“tap”, catchFly)

function _fly.new()
if tries < counter then return nil end

local flyfactory = loqsprite.newFactory(“fly”)
local aFly = flyfactory:newSpriteGroup()
aFly.x = 200; aFly.y = 350
aFly.ID = “Item_” … counter
aFly.speed = random(1,6)+3
aFly.canMove = true

counter= counter + 1
return aFly
end[/lua]
This link better explains the game itself http://howto.oz-apps.com/2011/10/bug-catcher-game-tutorial.html

Thanks for any help or suggestions!! [import]uid: 51459 topic_id: 16517 reply_id: 316517[/import]

I’m not sure what you mean… but you can just do fly.x… (or whatever your variable) is to the other end of the screen again…
Hope that helps! [import]uid: 95032 topic_id: 16517 reply_id: 61713[/import]

Hi Larochepower the issue that is really confusing to me is on about line 53 or so
[lua]function removeFly(thisOne)
thisOne:removeSelf()
flies[thisOne.ID] = nil
thisOne = nil
–print(“Fly Removed”)
fliesOnScreen = fliesOnScreen - 1
end[/lua]

Has the flies remove after the pass an x point… But to be honest I think I have come up with a solution that I am going to try out… :slight_smile: [import]uid: 51459 topic_id: 16517 reply_id: 61719[/import]

I found out what I needed to find out. Thanks for that! [import]uid: 51459 topic_id: 16517 reply_id: 61720[/import]

Jake, I told you that you can experiment with the tries variable and line 100 that causes the game to be over.

comment out line 100 and change it to

if fliesOnScreen==0 and fliesCaught==XX then

where XX is the number of flies you need to catch

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16517 reply_id: 61743[/import]

Oh I get it now!

Thanks… I’m sorry but I had forgotten you told me that… This makes a lot more sense

Theres just one more thing that I am not sure how to put in code

[lua]if theFly.x > 1550 then[/lua]

checkIfAnyFlyIsWithin
1240 - 1280
And having the flies flip when needed with this
[lua]flies.xScale = -1 [/lua]

[import]uid: 51459 topic_id: 16517 reply_id: 61757[/import]

in english making the flies hover over the frog when not caught [import]uid: 51459 topic_id: 16517 reply_id: 61758[/import]

in english making the flies hover over the frog when not caught [import]uid: 51459 topic_id: 16517 reply_id: 61759[/import]

The entire code was written with the fact that the flies will fly from left to the right, so the flies that are reach the end of the screen are killed and new ones spawned.

Yes, you can use the scale with a -ve value to flip them, but a fly doing the moonwalk will look pretty lame. Since you will have to change the direction and so on.

The checkIfAnyFlyIsWithin is a generic routine to check for a fly’s x co-ordinate if it falls in a particular range. If it does, it means that the fly can be caught or has been caught

Line 126 removes the fly when it goes off the screen.

If you can manage, then try to add a new field to the fly called direction, so instead of removing the fly at 1550, you just flip the direction and the fly starts to move in the reverse direction. and when it reaches 0, then it starts to move in the reverse direction again.

That way it will ping pong between the screen till it is caught.

Now if you want them to hover and buzz over the frog, like in this video, http://iphone.oz-apps.com/the-first-attempt-at-swarming-behaviour-like that is a totally different thing and beyond the scope of what you are trying to do in that code.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16517 reply_id: 61761[/import]

O,o look what I found!

[lua]local xdirection,ydirection = 1,1
local xpos,ypos = display.stageWidth*0.5,display.stageHeight*0.5
local circle = display.newCircle( xpos, ypos, 20 );
circle:setFillColor(255,0,0,255);

local tPrevious = system.getTimer()
local function animate(event)
local tDelta = event.time - tPrevious
tPrevious = event.time
xpos = xpos + ( 0.084*xdirection*tDelta );
ypos = ypos + ( 0.066*ydirection*tDelta );

if (xpos > display.stageWidth - 20 or xpos < 20) then
xdirection = xdirection * -1;
end
if (ypos > display.stageHeight - 20 or ypos < 20) then
ydirection = ydirection * -1;
end

circle:translate( xpos - circle.x, ypos - circle.y)
end

Runtime:addEventListener( “enterFrame”, animate );[/lua]

In theory can’t I connect the spawnFlies to this function? [import]uid: 51459 topic_id: 16517 reply_id: 61763[/import]

or not spawnFlies but replacing circle with flies? [import]uid: 51459 topic_id: 16517 reply_id: 61765[/import]

That is another way to do what we’ve done. That is taking into account that there could be frame drops and the deltaTime is used to catch up on lost frames.

You can try what fits for you.

However, let me also give you an advice, you can go the the market and buy the furniture that you like, but there is no guarantee that they will all look good or fit in well together, so it is best to colour match and style match them while purchasing. What I mean is that you cannot have a Red Ancient wood crafter Sofa with metallic Blue side chairs and an acryllic Green coffee table and Yellow painted walls with a purple sliken rug , etc. They might look good on their own or with the appropriate furniture items. together they might not be such a good idea.

Similarly, when you are trying to add code that you are finding, I would suggest that you try to recreate the whole routine with that code, you cannot just plug in that code into something that works on an entire different principle.

Hope you get what I am saying,

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16517 reply_id: 61767[/import]

As I was lying in bed I was thinking of how foolish of me to slab some random code on here thinking it would solve my problem because it had something that said xdirection and ydirection on it…

But thats a funny way of putting it… I’ll keep that in mind.

I will also really study what you had suggested previously and try to come up with a solution and post it when I do :slight_smile:

Thanks for the help

Jake [import]uid: 51459 topic_id: 16517 reply_id: 61771[/import]

Jake,
I am not discouraging you from looking at alternatives, even I try to search for the most optimum way to do things, after all there could be someone that has a better way to do things that what we use currently.

However, you have to remember that you cannot have mixed furniture, so if you want to go retro, then go all retro if modern, all modern.

Do what ever you want all the way, not a half hearted patch.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16517 reply_id: 61772[/import]

Check it out JayantV!! I figured it out!..

[lua]local k,v
for k,v in pairs(flies)do
local theFly = v
theFly.x = theFly.x + theFly.speed
if theFly.x > 1550 then
–theFly.canMove = false
–flies[k]=nil
–removeFly(v)
theFly.speed = -theFly.speed
– end
elseif theFly.x < 0 then
theFly.speed = -theFly.speed

end
end

end[/lua]
[import]uid: 51459 topic_id: 16517 reply_id: 61907[/import]