@Brent or anyone who can help: I was debugging to see when [lua] local function recycleLaser( event )[/lua] is being called. I thought it was being called after [lua] thisLaser.trans = transition.to( thisLaser, { y=200, alpha=0, time=500, transition=easing.inOutQuad, onComplete=recycleLaser } ) [/lua] It never gets called. Any ideas? [import]uid: 53149 topic_id: 34124 reply_id: 136138[/import]
Guy’s thanks for the awesome help. In trying to figure this thing out I discovered a wonderful happy accident. The blendMode!!! OMG! Thank you for that. It made a world of a difference. The issue I am having now for anyone that can help me is memory leaks.
What I’m doing is this: Every time the joystick is moved I am spawning these tiny cubes for a period of 5 seconds. Then in 2 seconds I start to fade them out (alpha = 0) creating a nice trail that disappears starting from the end. The effect is quite spectacular. The issue is, it works perfect on my computer, but on the device it’s slower than honey. I thought maybe I could check for the cubes that are already at alpha = 0 and then delete those with object:removeSelf().
This seems to help, but only a tiny bit. Can you help me figure out the best way of doing this?
Here’s the function handling the drawing of the cubes when the player moves. p1laser() is called from a function that detects when the joystick has moved.
[lua]
local function p1laser ()
local isActive = true
if(lightcycleTimer1 >=1) then
–laser = display.newRect(av1X,av1Y, 2,2)
laser = display.newImageRect(cachelaser1,10,10)
laser.blendMode = “add” – screen works as well
laser.x,laser.y = av1X,av1Y
laser.rotation = av1Rot
laser:setFillColor(201,35,128,255)
physics.addBody(laser,{radius = 2})
end – end of if
local function resetLC1() – Resets player 1’s light cycle
lightcycleTimer1 = 1000
end
local function lightCycTmr()
lightcycleTimer1 = lightcycleTimer1 - 10
print(lightcycleTimer1)
if(lightcycleTimer1 <=0) then
print(“You are running low on fuel”)
local lc1T = timer.performWithDelay(2000, resetLC1)
end
end
lightCycTmr() – Everytime we move reduce the light cycle
local function cleanUp()
if(laser.alpha == 0) then
print(“clean up”)
laser:removeSelf()
end
end
if(isActive == true) then
transition.to(laser, {time = 2900, alpha = 0, onComplete = cleanUp} )
else
isActive = false
end
end[/lua] [import]uid: 53149 topic_id: 34124 reply_id: 135893[/import]
Any suggestions guys? I thought maybe if I can figure out a way to instance the same image instead of loading it all the time would help. I’m a little suckish on tables. Not even sure if that’s the right move.
Thanks [import]uid: 53149 topic_id: 34124 reply_id: 135923[/import]
Hello again,
Yes, you have the correct idea… you need to find a way to “recycle” your trail objects instead of constantly creating/fading/deleting. That latter approach is what’s slowing everything down… it’s fairly “expensive” to create tons of new objects on the fly, especially on mobile.
The idea here is, you’d want to just create a table with (say) 50 of these pre-created objects inserted. As the simulation runs, it pulls one off the top of the “stack”, places it, starts the fade, and then when the fade completes that object goes back into the stack (at the bottom).
Brent [import]uid: 200026 topic_id: 34124 reply_id: 135927[/import]
@Brent - giving my code example above, could you help me integrate the tables. Please and thank you. I am artist first and programmer second. I can understand what’s going on for the most part, but conceptualize the process is my weakness
Thanks [import]uid: 53149 topic_id: 34124 reply_id: 135929[/import]
Hi, happy to help… the basic setup would be like this:
--fill table "cachedLasers" with X lasers (determine how many by checking below, don't needlessly over-fill it)
local cachedLasers = {}
for i=1,20 do
local laser = display.newRect(0,0,10,10) ; laser.alpha = 0
cachedLasers[#cachedLasers+1] = laser
end
local table\_remove = table.remove --cache Lua function "table.remove" for better performance
--function to recycle laser on 'onComplete' of fade transition
local function recycleLaser( event ) --'event' refers to the actual laser object
event.alpha = 0
transition.cancel( event.trans )
cachedLasers[#cachedLasers+1] = event
end
--on joystick move
local function putLaser()
local thisLaser = cachedLasers[1]
table\_remove( cachedLasers, 1 )
thisLaser.alpha = 1
print(#cachedLasers) --remove this after you test how many need to be in table!
thisLaser.x, thisLaser.y = 100, 100
thisLaser.trans = transition.to( thisLaser, { y=200, alpha=0, time=500, transition=easing.inOutQuad, onComplete=recycleLaser } )
end
timer.performWithDelay(50,putLaser,0)
You’ll have to put in your own aspects here, like placing the laser bits at your Avatar X and Y, and taking out the “y=200” part of the transition (I just put that in here for testing). If any of this doesn’t make sense, just let me know!
Brent [import]uid: 200026 topic_id: 34124 reply_id: 135957[/import]
Thanks for the assistance. It works, but I get an error here.
[lua]thisLaser.alpha = 1 [/lua]
Error:
attempt to index local ‘thisLaser’ (a nil value)
stack traceback:
[import]uid: 53149 topic_id: 34124 reply_id: 135971[/import]
Actually in debugging it’s seems that once I run through the say 200 (i changed it from 20 to do 200) If I should call the function putLaser() I get the error. Maybe this is what it is supposed to do. I like what it’s doing. How do say cause to run through the for loop again to get more lasers?
Do I put the hole thing in once function and call that from joystick move? right now I’m only call putLaser() [import]uid: 53149 topic_id: 34124 reply_id: 135975[/import]
You should only run the loop once to stock the cachedLasers table. Put enough in the first time so that you never have to refill it (but don’t overfill it either, otherwise you have unused objects just sitting there).
I’d have to see your code at this point to determine why that error was popping up. As I gave it to you, it was working perfectly in my Simulator. [import]uid: 200026 topic_id: 34124 reply_id: 135978[/import]
Uhm - yes, tried putting it all into a function. FAILED! I like that it spawns x number of cubes and once those x cubes are spawned it does no more. I can play with that. How do I get rid of the error once x cubes has been spawned. I’m always going to call putLaser() when the joystick has moved. THis way if the player runs over more lasers I can adjust the count. How do I get the for loop to fire again?
Really the question is, how do I get the for loop to fire again. I’m going to try and put that in a function by itself. Not sure if it’ll work, but least i can do is try right
Thanks again for the amazing help [import]uid: 53149 topic_id: 34124 reply_id: 135979[/import]
The “putLaser” function should actually just be combined into your joystick move function, there’s no need to make it separate. As for stocking more lasers, I could write a quick function to “refill” the table, but shouldn’t there be a maximum limit at any time, say 100 at most? [import]uid: 200026 topic_id: 34124 reply_id: 135980[/import]
I retract that my Woot! Yes, I could use help in creating refill. Yes, the limit would be 100 until they collide with a refill object. Can you help me with the Refill part?
Thanks [import]uid: 53149 topic_id: 34124 reply_id: 135982[/import]
I can do the collision part Brent. I just need to know how to setup the refill. Thanks [import]uid: 53149 topic_id: 34124 reply_id: 135990[/import]
OK, in this case, the “refill” should be managed by a separate counter that controls whether the player can fire more lasers. It shouldn’t actually empty the table of laser objects, which is separate and created only once. You should create an “ammoCount” variable and count down to 0, and when it’s empty you just stop placing lasers… but those laser objects stay within the “cachedLasers” table. Then, when an ammo fill-up is received, you set the ammo count back up and start outputting the objects again.
The point is, you don’t want to “fill” that table of cached lasers more than once, especially if you need to (at some point) make them into physics bodies, which are a bit more expensive performance-wise.
And, the table doesn’t need to actually contain the maximum ammo level that a player can have, it only needs to contain enough that will be shown on-screen at any given time. It’s the new ammo count which controls whether you put them on the screen. Make sense?
Brent [import]uid: 200026 topic_id: 34124 reply_id: 136003[/import]
@Brent or anyone who can help: I was debugging to see when [lua] local function recycleLaser( event )[/lua] is being called. I thought it was being called after [lua] thisLaser.trans = transition.to( thisLaser, { y=200, alpha=0, time=500, transition=easing.inOutQuad, onComplete=recycleLaser } ) [/lua] It never gets called. Any ideas? [import]uid: 53149 topic_id: 34124 reply_id: 136138[/import]