some surprises here… particularly the last point… maybe there’s a better way to do it but some things to be wary of and some conclusions
- set isVisible=false for offscreen items (initially)
- cache display object properties (x,y etc) in a “parent” class objevt
- set isVisible=true when items coords means they should be on screen
- *don’t* set isVisible=false again for items going back outside the screen bounds
can anyone comment on this?
(I’ve only tested it on the desktop, not the device)
j
[lua]require(“sprite”)
local balls = {}
local world = display.newGroup()
local spriteSheet = sprite.newSpriteSheet(“chuzzle.png”, 46,48)
local spriteSet = sprite.newSpriteSet(spriteSheet, 1,1)
local testball = display.newImage(“chuzzle.png”)
local w = testball.width
local h = testball.height
testball:removeSelf()
local moveSpeed = 0
function init()
local ball = {}
for b=1, 10000, 1 do
ball={}
– ******************************************
– both perform the same here
– ******************************************
– ball.sprite = sprite.newSprite(spriteSet)
ball.sprite = display.newImage(“chuzzle.png”)
– ******************************************
ball.xpos = math.random()*320
ball.ypos = (b*20) * (1+(math.random()*2))
balls[#balls+1] = ball
world:insert(ball.sprite)
– *******************************************
– taking this line out reduces fps to 7fps :S
– *******************************************
ball.sprite.isVisible=false
– *******************************************
ball.sprite.x=ball.xpos
ball.sprite.y=ball.ypos
end
end
function update(event)
local ball, ypos, bsprite
world.y = world.y - moveSpeed
world.ypos = world.y
for b=1, 10000, 1 do
ball=balls[b]
ballsprite = ball.sprite
– ******************************************
– reading a sprite.y value is a SLOW operation
– read it’s object value instead
– same for world.y / world.ypos
– ******************************************
– ypos = ballsprite.y + world.y
ypos = ball.ypos + world.ypos
– ******************************************
ytop = ypos - h
ybottom = ypos + h
if(ybottom > 0 and ytop < 480) then
ballsprite.isVisible=true
else
– **********************************
– adding this line slows things down
– from 30fps to 15fps!
– **********************************
– ballsprite.isVisible=false
– **********************************
end
end
end
local function onTouch(event)
local touchY = event.y
moveSpeed = 0-(240 - touchY)/4
end
init()
Runtime:addEventListener(“enterFrame”, update)
Runtime:addEventListener(“touch”, onTouch)
local fps = require(“fps”)
local performance = fps.PerformanceOutput.new();
performance.group.x, performance.group.y = display.contentWidth/2, 0;
performance.alpha = 0.6; [/lua] [import]uid: 6645 topic_id: 3618 reply_id: 303618[/import]