Hi,
I have just recently started to work with the SDK and I have been banging my head over a design detail I just can’t seem to get right. I would really appreciate if someone could point me into the right direction with this.
In short: How do you ensure screen updates if lots of events are flooding your app?
Details: :unsure:
Basically I am trying to properly orchestrate touch events and screen updates. In my test code I have created a grid of spites (8x8) on the screen and capture the touch event on each of them. As the user is moving his finger across the screen, I test for a few conditions and if they are fulfilled, change frames on the spites.
This works all fine in the simulator but fails on an iPhone 4s. I instrumented the code and suspect that the problem is actually quite simple. I simply spend too much time handling the event and directly calling my spite:setFrame. As it seems the screen won’t get updated as I receive so many events that I spend all time in my own code. I also noticed that my enterFrame events are only called twice per second if swiping heavily. If I stop swiping over the screen, the screen updates catch up after a fraction of a second.
Pseudo code:
... local function changeSprite(sprite) sprite:setFrame(...) end local function testValidMove() -- takes microsecond in simulator, several ms in the device ... end function onTouch(event) if event.phase == "began" or event.phase == "move" do if (testValidMove(event.target)) do changeSprite(event.target) end end ... end
As it seems even function calls or simple if statements take about 20x as long as in the simulator and my code uses too much time in own code and gets flooded by the touch events if moving the finger fast across the screen.
Are there any design patterns in Corona to deal with requirements like this? I couldn’t find any. I was looking at timer.performWithDelay or other means to detach from the UI thread but so far I had no luck.
BTW I was also trying to find ways to throttle the simulator to be closer to the device experience but didn’t come across anything using the search. Surely there is some way to do that?
Thanks!