I have a very simple problem that I am not understanding. In this mini game I just want to be able to touch touch 8 bees and they all play the same animations. The problem I am having is that bee is local and it needs to be global or needs to be crossed called.. I viewed the scopes for functions tutorial but I am not sure if I am going about it right.. Can someone point me in the right direction?
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 | display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR local i = 1 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 bee = nil local t1 = { {x=180,y=300}, {x=280,y=300}, {x=380,y=300}, {x=480,y=300}, {x=580,y=300}, {x=680,y=300}, {x=780,y=300}, {x=880,y=300} } local function spawnBees () for i = 1,8 do local bfactory = loqsprite.newFactory("bee") bee = bfactory:newSpriteGroup() bee.x = t1[i].x; bee.y = t1[i].y bee:prepare("aniBee Bee") end end listener = function( event ) local phase = event.phase if ( phase == "ended" ) then bee:play("aniBee Bee") end end bee:addEventListener( "touch", listener ) |
This should help - http://blog.anscamobile.com/2011/09/tutorial-modular-classes-in-corona/
Thanks @GrahamRanson
I got the bees to show up but the touch event doesn't work.. :(
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 | display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR local i = 1 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 t1 = { {x=180,y=300}, {x=280,y=300}, {x=380,y=300}, {x=480,y=300}, {x=580,y=300}, {x=680,y=300}, {x=780,y=300}, {x=880,y=300} } local function spawnBees () local bee = nil for i = 1,8 do local bfactory = loqsprite.newFactory("bee") bee = bfactory:newSpriteGroup() bee.x = t1[i].x; bee.y = t1[i].y bee:prepare("aniBee Bee") end end local function onTouch(event) if "ended" == event.phase then --bee:play("aniBee Bee") print("touched bee") end bee:addEventListener("touch",onTouch) return bee end local xTemp = spawnBees () |
You're adding the touch event listener within the touch event handler :-)
I don't know what you ment by that GrahamRanson ?:)
Is this what you mean.. If it is it, doesn't do any thing either...
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 | display.setStatusBar( display.HiddenStatusBar ) -- HIDE STATUS BAR local i = 1 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 t1 = { {x=180,y=300}, {x=280,y=300}, {x=380,y=300}, {x=480,y=300}, {x=580,y=300}, {x=680,y=300}, {x=780,y=300}, {x=880,y=300} } local function onTouch(event) if "ended" == event.phase then --bee:play("aniBee Bee") print("touched bee") end end local function spawnBees () local bee = nil for i = 1,8 do local bfactory = loqsprite.newFactory("bee") bee = bfactory:newSpriteGroup() bee.x = t1[i].x; bee.y = t1[i].y bee:prepare("aniBee Bee") end bee:addEventListener("touch",onTouch) return bee end local xTemp = spawnBees () |
In your spawnBees function you only add the event listener to the last bee created as it is added outside of the loop, give this a try:
1 2 3 4 5 6 7 8 9 10 11 12 | local function spawnBees () local bee = nil for i = 1,8 do local bfactory = loqsprite.newFactory("bee") bee = bfactory:newSpriteGroup() bee.x = t1[i].x; bee.y = t1[i].y bee:prepare("aniBee Bee") bee:addEventListener("touch",onTouch) end return bee end |
Cool thanks GrahamRanson!! That makes a lot more sense now!! Thanks a bunch!
I SEE THE LIGHT!
No problem, glad it helped.
it helped all right!
There are other scope problems....
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | local function spawnBees () local bee = nil for i = 1,8 do local bfactory = loqsprite.newFactory("bee") bee = bfactory:newSpriteGroup() bee.x = t1[i].x; bee.y = t1[i].y bee:prepare("aniBee Bee") end end local function onTouch(event) if "ended" == event.phase then --bee:play("aniBee Bee") print("touched bee") end bee:addEventListener("touch",onTouch) return bee end |
send message and call to a person
Forum: Report Spam + PM
[INSTALL_PARSE_FAILED_NO_CERTIFICATES]
[SOLVED] Director ERROR: Failed to execute new( params ) function on 'screen1'
Too many (200) local variables
Corona SVG Level Builder released. Drag and drop physics level editor with Inkscape and SVG.
Small bug in Collectible Items example
Pivot Joints and apparent elasticity
Can't make removeSelf() delayed
Updating Ring Type
Using sprites to create a tiled background
URGENT!: We lost our VFX Artist - still need one (1!) more VFX to ship our game!
iCade support (keyboard input)
Being able to deauthorize PC from web account
Dynamic triangle creation not possible?
Irreversible iTunes app data
Access Global Function
Spriteloq: How to use Spritesheet scales