Best coding method to move 100 coins across the screen over and over again?

Ok, I posted a previous topic about collision detection between separate groups and I could not get it to work so now my new post is how to efficiently move 100 coins across the screen without losing performance and fps?

Since I cannot put 100 coins in a display group and move just the display group because I would lose the physics collision detection, I have to leave them out of a group and move them individually across the screen in the "enterFrame" event listener. However, when I test it on my ITouch 3G, the performance sucks. It slows down and my game is not playable.

Again, what would be the best method to move 100 coins across the screen? I tried to use transitions and translate but they all give me performance problems like the "enterFrame" event listener. My BEST method was to place the 100 coins in a display group and move the group across the screen and there was no performance lag in my ITouch 3G, however, I lost my physics collision detection and that is key in my game because I want to detect when the character hits a coin and make the coin disappear.

Another idea I was thinking, but I haven't done it yet. Would it be better to keep my coins static and just move my character and camera instead towards the coins like those Doodle Jump games? I'm thinking that would be more efficient, however, how would I do that? If I place my coins static on the screen, how would I move my character towards the coins and still simulate a Parallax Scrolling effect?

Note: I'm using the Director class and placing my 100 coins in the localGroup object. My character is also placed in the local group object.

Here is my code. I have an outer loop and inner loop running because I have different shapes for my coins and each shape is stored in a table. The outer table stores the inner table shapes. As you can see, running for loops in the enterFrame event listener is not efficient and it's taking a toll on my performance:

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
function MoveCoinsTable(coinsTable, xOffset, yOffset, turboOffset)      
        if (PlayState.InGame == true) then
                if (PlayState.MoveCoinsStarted == false) then
                        PlayState.MoveCoinsStarted = true
                        
                        local index1 = 1
                        for index1 = 1, #coinsTable + 1, 1 do
                                if (coinsTable[index1] ~= nil) then
                                        local innerCoinsTable = coinsTable[index1]
                                        local index2 = 1
                                        
                                        for index2 = 1, #innerCoinsTable + 1, 1 do
                                                if (innerCoinsTable[index2] ~= nil) then
                                                        local coin = innerCoinsTable[index2]
                                                        
                                                        coin.x = coin.x - (xOffset * (.50 + (turboOffset / 4)))
                
                                                        if ((coin.x + (coin.width / 2)) <= 0) then
                                                                coin.x = _W + (coin.width / 2)
                                                        end
 
                                                end     
                                        end
                                        
                                        if (index1 == #coinsTable and index2 == #innerCoinsTable) then
                                                PlayState.MoveCoinsStarted = false
                                        end
                                end
                        end
                end
        end
end
 
-- Code to get 100 coins
local _coinsTable = Coins.GetCoins()
 
local tPrevious = system.getTimer()
 
function MoveEnvironment(event)
        if (PlayState.InGame == true) then
                local tDelta = event.time - tPrevious
                tPrevious = event.time
                local turboOffset = 50
                
                local xOffset = ( 0.20 * tDelta )
                local yOffset = ( 0.15 * tDelta )
 
                MoveCoinsTable(_coinsTable, xOffset, yOffset, turboOffset)
 
        end
end
 
Runtime:addEventListener("enterFrame", MoveEnvironment)

Simply put:

Don't use all 100 coins at once.

Unless they all need to be onscreen at the same time, you don't need to have all 100 coins doing stuff that drags the performance down.

I cannot understand the *logic* of what you are trying to achieve, however one of the reasons for lagging performance is that enterFrame is called about 30 times a second and you are inside of that handler looping through

index1 = 1, #coinsTable+1
index2 = 1, innerCoinsTable + 1

lets say the first loop was 1, 100 and the second loop was 1, 50

you have 100*50 = 5000 items being processed in 1/30 th of a second.

So you need to optimise your routine or break it down into slices using coroutines and call them in 30 slices in a second. There are many suggestions for optimisation. You can have a large number of objects, it is about how you manage then that will give you speed and performance.

cheers,

?:)

views:1277 update:2011/10/30 22:40:54
corona forums © 2003-2011