Incrementing a variable

Sorry for such a noob question, but I am trying to increment a variable from 1 to 8 in a set amount of time, but I cant seem to get it to work correctly. Here is my code as of right now:

local gameLoop = function ( event )  
  
 if gameIsActive then  
  
 scrollSpeed = 1  
  
 local moveBuildings = function()  
  
 leftBuilding1.y = leftBuilding1.y + scrollSpeed  
 if leftBuilding1.y \>= 720 then  
 leftBuilding1.y = -240  
 end  
  
 leftBuilding2.y = leftBuilding2.y + scrollSpeed  
 if leftBuilding2.y \>= 720 then  
 leftBuilding2.y = -240  
 end  
  
 rightBuilding1.y = rightBuilding1.y + scrollSpeed  
 if rightBuilding1.y \>= 720 then  
 rightBuilding1.y = -240  
 end   
  
 rightBuilding2.y = rightBuilding2.y + scrollSpeed  
 if rightBuilding2.y \>= 720 then  
 rightBuilding2.y = -240  
 end  
  
 end  
  
 timer.performWithDelay(2000, moveBuildings)  
 --transition.to(scrollSpeed, {delay=2000, time=2000, scrollSpeed=8})  
 end  
  
 end  
  

Basically I want the buildings to start off slowly scrolling (scrollSpeed = 1) and then increase in speed over a 2 second time frame to a higher value (scrollSpeed = 8).

Im sure it is easy im just having some issues. Does anyone have any idea’s on how to make this work properly? Thanks a lot in advance!

-Kyle [import]uid: 9968 topic_id: 4520 reply_id: 304520[/import]

http://developer.anscamobile.com/content/timer-library

timer.performWithDelay(2000, moveBuildings,8); << it will call moveBuildings every 2 seconds 8 times.

c. [import]uid: 24 topic_id: 4520 reply_id: 14260[/import]

The moveBuildings function is not want I want to increment. To give you an idea of what im looking for, I want a ship that when the game screen loads, it is sitting on the ground and then after 2 seconds (timer.performWithDelay(2000, moveBuildings) the buildings start to scroll down based on the scroll speed variable which is set to 8. This is to simulate a lift off. The problem is you go from stand still to instantly going the speed of lift off. Their is an “enterFrame” listener that is being usedmgn

What I want is after the 2 second pause, I want the buildings to start moving down, only I want them to start slow and increase their speed until they get to the maximum speed. This increase should only take about 2 seconds to occur. In order to do this, I need a function or method that will wait 2 seconds, and then every .25 seconds increase scrollSpeed by 1 in order to simulate speed of the ship increasing.

Anyone have any ideas? I have attempted many things and cannot get it to work correctly but I cannot get it to work. If anyone is having trouble understanding what I mean, let me know and I will try to do better. Thanks in advance!

P.S. Thank you for the reply Carlos! I tried what you said and a couple variations, but no luck yet! Keep up the good work!

-Kyle [import]uid: 9968 topic_id: 4520 reply_id: 14314[/import]

To answer your question directly, use a timer to call not moveBuildings but rather some other function that’s something like

function increment()
speed = speed + 2
end

Now that’s pseudocode, not what you actually want to write, but hopefully you get the idea.


In this case however I’m wondering why you aren’t using the physics engine. Just use applyForce continuously in the enterFrame event and it’ll smoothly takeoff. [import]uid: 12108 topic_id: 4520 reply_id: 14315[/import]

Well its a parallax scroll. The idea is navigate the ship left and right using the accelerometer as it travels “up”. To simulate the ship constantly moving up, I have the background moving down and repeating forever, and the ship stays in the same y position, giving the illusion of traveling up.

When the game starts, the ship is on the ground and the background is not scrolling, and after 2 seconds it “takes off”. The code I posted above works perfect, but it looks weird because the ship is sitting on the ground, and then suddenly its traveling at top speed. I want to simulate the ship taking off at a slow speed and have it quickly increase to the top speed, and from there on just scroll at the same speed

What you suggested about the separate function worked out quite well, but do you think there is a better way to do a parallax scroll with what I want to do?

Thanks a lot for the info jhocking!!

-Kyle [import]uid: 9968 topic_id: 4520 reply_id: 14322[/import]

So this method works great on the simulator, but is very choppy and slow on a device (iPhone 4) [import]uid: 9968 topic_id: 4520 reply_id: 14327[/import]

Could you be more specific than “choppy”? [import]uid: 12108 topic_id: 4520 reply_id: 14371[/import]

I asked the same/similar question:
http://developer.anscamobile.com/forum/2010/11/30/how-kill-time

It kinda works.

Any additional feedback would be great.
Thanks,
RD [import]uid: 7856 topic_id: 4520 reply_id: 14422[/import]

The building moves about 2 pixels and freezes for a millisecond and moves another 2 pixels or so. I got rid of the timer.performWithDelay, and got rid of the moveBuildings function, so now the code to move the buildings is just in the gameLoop function.

This runs perfectly with no issues. I think the timer was causing an issue.

@rdcube, thanks for the post, that will def help out. I will be reading the timer docs again now that I have a much better understanding of Lua as a whole compared to when I first started.

Thanks a lot guys, and any other suggestions are welcome! [import]uid: 9968 topic_id: 4520 reply_id: 14438[/import]

Yeah I probably wouldn’t have used a timer myself, I was just explaining what you would do with the timer since you had started to go down that route. I would do something more like incrementing a counter every frame and then have an if statement in the enterFrame listener that increases speed at certain frames. [import]uid: 12108 topic_id: 4520 reply_id: 14458[/import]