Multiply objects movment with enterFrame

I want to move text objects around the screen each on random direction. I have code witch can move one object at a time. How I can modify this code? Loop?

[lua]local screenLeft = display.screenOriginX
local screenWidth = display.contentWidth - screenLeft * 2
local screenRight = screenLeft + screenWidth
local screenTop = display.screenOriginY
local screenHeight = display.contentHeight - screenTop * 2
local screenBottom = screenTop + screenHeight
display.contentWidth = screenWidth
display.contentHeight = screenHeight

local xDirection
local yDirection

local xSpeed = 4
local ySpeed = 4

function randomDirection()
repeat
xDirection = math.random(-1, 1)
yDirection = math.random(-1, 1)
until xDirection ~= 0 and yDirection ~= 0
return xDirection, yDirection
end

randomDirection()

local text1 = display.newText(“Hello”, 100, 200, native.systemFont, 34 )
–local text2 = display.newText(“World”, 160, 250, native.systemFont, 34 )

local function animate(event)

local xPos = text1.x + ( xSpeed * xDirection );
local yPos = text1.y + ( ySpeed * yDirection );

if ( xPos > screenRight or xPos < screenLeft ) then
xDirection = xDirection * -1;

end
if ( yPos > screenBottom or yPos < screenTop) then
yDirection = yDirection * -1;
end

text1:translate( xPos - text1.x, yPos - text1.y)
end
Runtime:addEventListener( “enterFrame”, animate )[/lua]

Sorry, I have no idea why my code align in one line without spaces between text.

You could make a table for your objects, and then use a for loop to move them:

for i = 1, #textTable do local function animate() end

Try

local screenLeft = display.screenOriginX local screenWidth = display.contentWidth - screenLeft \* 2 local screenRight = screenLeft + screenWidth local screenTop = display.screenOriginY local screenHeight = display.contentHeight - screenTop \* 2 local screenBottom = screenTop + screenHeight --display.contentWidth = screenWidth --display.contentHeight = screenHeight local textObjects, numText = {}, 5 local mRandom = math.random math.randomseed( os.time() ) local function newText( options ) &nbsp; &nbsp; options = options or {} &nbsp; &nbsp; local xSpeed = options.xSpeed or 4 &nbsp; &nbsp; local ySpeed = options.ySpeed or 4 &nbsp; &nbsp; local text = options.text or 'Hello' &nbsp; &nbsp; local instance = display.newText( text, 100, 200, native.systemFont, 34 ) &nbsp; &nbsp; instance.xSpeed = xSpeed &nbsp; &nbsp; instance.ySpeed = ySpeed&nbsp; &nbsp; &nbsp; local function rand() return mRandom() \* 2 - 1 -- numbers from -1 to 1 end &nbsp; &nbsp; function instance:randomDirection() &nbsp; &nbsp; &nbsp; &nbsp; local xDirection &nbsp; &nbsp; &nbsp; &nbsp; local yDirection &nbsp; &nbsp; &nbsp; &nbsp; repeat &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xDirection = rand() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yDirection = rand() &nbsp; &nbsp; &nbsp; &nbsp; until xDirection ~= 0 and yDirection ~= 0 &nbsp; &nbsp; &nbsp; &nbsp; return xDirection, yDirection &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; function instance:animate( event ) &nbsp; &nbsp; &nbsp; &nbsp; local xPos = self.x + ( self.xSpeed \* self.xDirection ) &nbsp; &nbsp; &nbsp; &nbsp; local yPos = self.y + ( self.ySpeed \* self.yDirection ) &nbsp; &nbsp; &nbsp; &nbsp; if ( xPos \> screenRight or xPos \< screenLeft ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.xDirection = self.xDirection \* -1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ( yPos \> screenBottom or yPos \< screenTop ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.yDirection = self.yDirection \* -1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; self:translate( xPos - self.x, yPos - self.y ) &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; instance.xDirection, instance.yDirection = instance:randomDirection() &nbsp; &nbsp; return instance end &nbsp; &nbsp; local function loop( event ) &nbsp; &nbsp; for i=1, numText do textObjects[i]:animate( event ) end&nbsp; end &nbsp; &nbsp; for i=1, numText do textObjects[i] = newText() end &nbsp;&nbsp; Runtime:addEventListener( "enterFrame", loop )&nbsp;

I use loop function to update text objects from table textObjects.

Thanks guys.

And code above is just fantastic, exactly what I needed.

You could make a table for your objects, and then use a for loop to move them:

for i = 1, #textTable do local function animate() end

Try

local screenLeft = display.screenOriginX local screenWidth = display.contentWidth - screenLeft \* 2 local screenRight = screenLeft + screenWidth local screenTop = display.screenOriginY local screenHeight = display.contentHeight - screenTop \* 2 local screenBottom = screenTop + screenHeight --display.contentWidth = screenWidth --display.contentHeight = screenHeight local textObjects, numText = {}, 5 local mRandom = math.random math.randomseed( os.time() ) local function newText( options ) &nbsp; &nbsp; options = options or {} &nbsp; &nbsp; local xSpeed = options.xSpeed or 4 &nbsp; &nbsp; local ySpeed = options.ySpeed or 4 &nbsp; &nbsp; local text = options.text or 'Hello' &nbsp; &nbsp; local instance = display.newText( text, 100, 200, native.systemFont, 34 ) &nbsp; &nbsp; instance.xSpeed = xSpeed &nbsp; &nbsp; instance.ySpeed = ySpeed&nbsp; &nbsp; &nbsp; local function rand() return mRandom() \* 2 - 1 -- numbers from -1 to 1 end &nbsp; &nbsp; function instance:randomDirection() &nbsp; &nbsp; &nbsp; &nbsp; local xDirection &nbsp; &nbsp; &nbsp; &nbsp; local yDirection &nbsp; &nbsp; &nbsp; &nbsp; repeat &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xDirection = rand() &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yDirection = rand() &nbsp; &nbsp; &nbsp; &nbsp; until xDirection ~= 0 and yDirection ~= 0 &nbsp; &nbsp; &nbsp; &nbsp; return xDirection, yDirection &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; function instance:animate( event ) &nbsp; &nbsp; &nbsp; &nbsp; local xPos = self.x + ( self.xSpeed \* self.xDirection ) &nbsp; &nbsp; &nbsp; &nbsp; local yPos = self.y + ( self.ySpeed \* self.yDirection ) &nbsp; &nbsp; &nbsp; &nbsp; if ( xPos \> screenRight or xPos \< screenLeft ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.xDirection = self.xDirection \* -1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; if ( yPos \> screenBottom or yPos \< screenTop ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.yDirection = self.yDirection \* -1 &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; self:translate( xPos - self.x, yPos - self.y ) &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; instance.xDirection, instance.yDirection = instance:randomDirection() &nbsp; &nbsp; return instance end &nbsp; &nbsp; local function loop( event ) &nbsp; &nbsp; for i=1, numText do textObjects[i]:animate( event ) end&nbsp; end &nbsp; &nbsp; for i=1, numText do textObjects[i] = newText() end &nbsp;&nbsp; Runtime:addEventListener( "enterFrame", loop )&nbsp;

I use loop function to update text objects from table textObjects.

Thanks guys.

And code above is just fantastic, exactly what I needed.