-- create table
local stars = {}
-- initial vars
local stars_total = 600
local stars_field1= 200
local stars_field2= 400
local stars_field3= 800
local star_width = 1
local star_height = 1
-- create/draw objects
for i = 1, stars_total do
local star = {}
star.object = display.newRect(math.random(320),math.random(480),star_width,star_height)
stars[ i ] = star
end
-- update star locations and setcolor
local function udpdatestars(event)
for i = stars_total,1, -1 do
if (i < stars_field1) then
stars[i].object:setFillColor(100,100,100)
starspeed = 1
end
if (i < stars_field2 and i > stars_field1) then
stars[i].object:setFillColor(155,155,155)
starspeed = 2
end
if (i < stars_field3 and i > stars_field2) then
stars[i].object:setFillColor(255,255,255)
starspeed = 3
end
stars[i].object.y = stars[i].object.y + starspeed
if (stars[i].object.y > display.stageHeight) then
stars[i].object.y = stars[i].object.y-480
end
end
end
-- let updatestars run at enterFrame
Runtime:addEventListener("enterFrame",udpdatestars)
-- Enjoy it
-- Frehsworks
Its running on mine... Which iPhone are you using? I have a 3gs and a 1st gen. It runs pretty slow on the 1st gen, but it does run... I just copy/pasted the code you pasted in.
Here's a quick video of it: http://developer.anscamobile.com/starfield.MOV
Looks very nice to me. Would be great for a shmup! :D
Very nice !
carlos
both, 3G and 3GS won't work
When i hit the icon, the app starts and shows a black screen.
Probaly something to do with provisioning, i'am not using the adhoc distribute method, but a development profile ?
It worked perfect on my simulator on the computer, but jerky (like your video) on my iTouch. It looks like its updating once a second. Could it be a computing power issue - ie. not enough power on a handheld to process it the way the code is written? But I have other apps like "Zen of Snow" and "Gold Dust" which have hundreds of particles flying around very smoothly - I wonder how they did it? Could it be a Corona memory issue?
I was able to get it running on both my 1st gen and 3G S, though the difference in performance between the two was quite obvious. Keep in mind too, that just because you see lots of 'particles' flying around, doesn't mean there are actually particles flying around. You could create a big png file with hundreds of 'particles' in it and simply have it moving about the screen, or have 4-5 for that matter which would be a considerably smaller performance hit than hundreds of particles.
Just for fun I added print(#stars) at the end and it gave me 600 as the number of objects floating around the screen. The sample code for fishies will let you change the number of fish as well and it bogs down pretty good around that number. Generating that many objects on the fly (while nifty) isn't very efficient. This really isn't a corona limitation, its an iphone limitation, thus why it seems to work fine in the simulator.
If its an iPhone limitation, then how did they do it with "Gold Dust"? Its a free app, check it out - there are a thousand particles flying around very fluidly on my G2 iTouch ...
As far as "Gold Dust", the programmers are, most likely, using straight C/C++ to code the animation as opposed to an interpreted language. Also, they're probably using purpose-specific code, as opposed to higher-level, general purpose routines like provided by Corona.
Having said that, I've made some tweaks to freshworks original code so it now runs at a steady 14+ fps (that's 8500 stars updated per second) on my 2nd Gen Touch, as opposed to the ~6fps of the original 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 | --- code start --- display.setStatusBar( display.HiddenStatusBar ) local txt = display.newText( "", display.stageWidth/2, 8, nil, 10 ) txt:setTextColor( 255,0,255 ) local function init_fps() local fps_lst = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} local fps_ndx = 1 local fps_tally = 0 local prev_time = system.getTimer() return function( event ) local etime = event.time - prev_time prev_time = event.time local cur_fps = 1000 / etime fps_tally = ((fps_tally + cur_fps) - fps_lst[fps_ndx]) fps_lst[fps_ndx] = cur_fps fps_ndx = fps_ndx + 1 if fps_ndx > 30 then fps_ndx = 1 end txt.text = string.format( "%2.2f", fps_tally / 30 ) end end Runtime:addEventListener( "enterFrame", init_fps() ) local stars = {} for i = 1, 600 do local star = {} star.object = display.newRect( math.random(320), math.random(480), 1, 1 ) if i <= 200 then star.object:setFillColor( 100, 100, 100 ) star.object.speed = 1 elseif i <= 400 then star.object:setFillColor( 155, 155, 155 ) star.object.speed = 2 elseif i <= 600 then star.object:setFillColor( 255, 255, 255 ) star.object.speed = 3 end stars[i] = star end local function updateStars( event ) local s = stars for i = 1, 600 do local o = s[i].object o.y = o.y + o.speed if o.y > 480 then o.y = 0 end end end Runtime:addEventListener( "enterFrame", updateStars ) --- code end --- |
Also there's a bit of code that will compute the average FPS over the previous 30 frames.
Thanks DGuy. I tried the new code, but got the same result - looks good on my Mac, but has the same jerky look on my iTouch.
The numbers at the top of the screen show 30.30 on my Mac , but 12.63 on my iTouch - is that the average FPS? hmmmm
(Still at the airport -- 2nd attempt at posting from Droid browser)
I just grabbed Gold Dust on iPhone, and it looks to me like an OpenGL hardware particle effect, as opposed to actually compositing thousands of sprites. I agree that it would be awesome to get access to some of this stuff via Corona :)
freshworks -- are your builds still not working on the device? Could this be the dreaded "8-character bug" in 1.0?
My opinion:
I Think that the newRect function is a quiet heavy call for 1000 stars, that slows the app down. I will try with png's instead, can i reuse a png instead of using newImage for all stars ?
@evank:
Still not working no, but the 8-character isn't the issue is it, because i only use the main.lua ????
I actually tried it with images: an 8x8 & an 64x64 image, both with a single non-transparent pixel. Both where slower than just using newRect.
Hmmm just tested it on the iphone, but won't see anything but a black screen ??
In the simulator the above code works fine....
Can a staff member tell me why please