Random transition\translation problem. Angle.

Hello,
I’m stuck in my object random transition after it reaches an edge of screen. After object reaches the screen edge it makes the same moves. Here is a picture with little square waypoint(it changes slitely over time but always makes the same moves):

Uploaded with ImageShack.us

How to make it more randomly?

[lua]local physics = require(“physics”)
physics.start()
physics.setGravity( 0, 0)

display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage( “bg.png” )
background.y = 240

local radius = 25

local xdirection = 1
local ydirection = 1

local xspeed = 0
local yspeed = 0

local xpos = 160
local ypos = 240

– Get current edges of visible screen (accounting for the areas cropped by “zoomEven” scaling mode in config.lua)
local screenTop = display.screenOriginY
local screenBottom = display.viewableContentHeight + display.screenOriginY
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX

local groundBot = display.newImage(“ground1.png”)
physics.addBody( groundBot, “static”, { bounce=0.7 } )
groundBot.x = 160
groundBot.y = 405

local groundUp = display.newImage(“ground1.png”)
physics.addBody( groundUp, “static”, { bounce=0.7 } )
groundUp.x = 160
groundUp.y = 75

local groundL = display.newImage(“ground2.png”)
physics.addBody( groundL, “static”, { bounce=0.7 } )
groundL.x = 0
groundL.y = 240

local groundR = display.newImage(“ground2.png”)
physics.addBody( groundR, “static”, { bounce=0.7 } )
groundR.x = 320
groundR.y = 240

local h1 = 22
local w1 = 78
local xdirection1 = 1
local ydirection1 = 1

local xspeed1 = 5
local yspeed1 = 5

local xpos1 = 20 + w1/2
local ypos1 = 70 + h1
local sq = display.newImage( “en1.png”, xpos1, ypos1 )
physics.addBody( sq, “static”, { friction=0 } )

– Get current edges of visible screen (accounting for the areas cropped by “zoomEven” scaling mode in config.lua)
local screenTop = display.screenOriginY + 90
local screenBottom = display.viewableContentHeight + display.screenOriginY - 90
local screenLeft = display.screenOriginX
local screenRight = display.viewableContentWidth + display.screenOriginX

local function animate1(event)
xpos1 = xpos1 + ( xspeed1 * xdirection1 );
ypos1 = ypos1 + ( yspeed1 * ydirection1 );

if ( xpos1 > screenRight - w1*0.25 or xpos1 < screenLeft + w1*0.25 ) then
xdirection1 = xdirection1 * -1;
end
if ( ypos1 > screenBottom - h1*0.25 or ypos1 < screenTop + h1*0.25 ) then
ydirection1 = ydirection1 * -1;
end

sq:translate( xpos1 - sq.x, ypos1 - sq.y)
end

Runtime:addEventListener( “enterFrame”, animate1 );

local h2 = 119
local w2 = 27
local xdirection2 = 1
local ydirection2 = 1

local xspeed2 = 5
local yspeed2 = 5

local xpos2 = 300
local ypos2 = 320
local sq2 = display.newImage( “en2.png”, xpos2, ypos2 )
physics.addBody( sq2, “static”, { friction=0 } )

– Get current edges of visible screen (accounting for the areas cropped by “zoomEven” scaling mode in config.lua)
local function animate2(event)
xpos2 = xpos2 - ( xspeed2 * xdirection2 );
ypos2 = ypos2 - ( yspeed2 * ydirection2 );

if ( xpos2 > screenRight - w2*0.25 or xpos2 < screenLeft + w2*0.25 ) then
xdirection2 = xdirection2 * -1;
end
if ( ypos2 > screenBottom - h2*0.25 or ypos2 < screenTop + h2*0.25 ) then
ydirection2 = ydirection2 * -1;
end

sq2:translate( xpos2 - sq2.x , ypos2 - sq2.y)
end

Runtime:addEventListener( “enterFrame”, animate2 ); [/lua]

Thank you!
[import]uid: 49851 topic_id: 29889 reply_id: 329889[/import]