move object up down with math.sin

hi

can somebody convert the code below to corona code

it should basically move an object up and down using math.sin

var numberOfTicks = 0;

function gameLoop() {

    numberOfTicks++;

    ufo.y = (250 * sin(numberOfTicks * 0.5 * pi)) + 350;

    ufo.x += ufo.xSpeed;

}

thanks

this page is from

http://gamedevelopment.tutsplus.com/tutorials/quick-tip-create-smooth-enemy-movement-with-sinusoidal-motion–gamedev-6009

It’s pretty easy and you should definitly try to understand it yourself, as JavaScript and LUA are quite similar.

local numberOfTics = 0 local ySpeed = 0.5 local function gameLoop() ufo.y = (250\*math.sin(numberOfTicks\*ySpeed\*math.pi)) + 350 ufo.x = ufo.x + ufo.xSpeed numberOfTics = numberOfTics + 1 end

I added a ySpeed value, which you can alter to your needs.

HI

could not manage to get anything moving i am using code as below

can somebody try the code with moving an object

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 0.5

local function gameLoop()
ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
ufo.x = ufo.x + ufo.xSpeed
numberOfTics = numberOfTics + 1
end

thankyou

Well, of course you need an enterFrame listener to call the gameLoop.

Runtime:addEventListner("enterFrame", gameLoop)

Call this after you declared the gameLoop function.

HI

ok im trying this code , but seems object just moves right along the screen and not up and down

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 2.5

local function gameLoop( event )
   ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
   ufo.x = ufo.x + 20 --ufo.xSpeed
   numberOfTics = numberOfTics + 1
end

Runtime:addEventListener(“enterFrame”, gameLoop)
 

your help much appreciated

You need a much lower value for ySpeed.

One sinus wave goes from zero (0) to two pi (6.2831…), so if you multiply pi with a values greater than 1 there wont be much going on.

Take something in the range 0.01 to 0.1 or even smaller.

Still no joy, still seems to move horizontal only

can somebody actually try the code

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 0.1

local function gameLoop( event )
   ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
   ufo.x = ufo.x + 20 --ufo.xSpeed
   numberOfTics = numberOfTics + 0.1
end

Runtime:addEventListener(“enterFrame”, gameLoop)
 

I tried the code above and it does move downward on the screen. I modified it to slow the x axis speed of the object, so that you can see the behavior yourself:

local ufo = display.newRect( display.contentCenterX, display.actualContentHeight, 15, 15 ) local numberOfTics = 0 local ySpeed = 0.1 local function gameLoop( event ) ufo.y = (250\*math.sin(numberOfTics\*ySpeed\*math.pi)) ufo.x = ufo.x + 2 --ufo.xSpeed numberOfTics = numberOfTics + 0.1 end Runtime:addEventListener("enterFrame", gameLoop)

It’s pretty easy and you should definitly try to understand it yourself, as JavaScript and LUA are quite similar.

local numberOfTics = 0 local ySpeed = 0.5 local function gameLoop() ufo.y = (250\*math.sin(numberOfTicks\*ySpeed\*math.pi)) + 350 ufo.x = ufo.x + ufo.xSpeed numberOfTics = numberOfTics + 1 end

I added a ySpeed value, which you can alter to your needs.

HI

could not manage to get anything moving i am using code as below

can somebody try the code with moving an object

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 0.5

local function gameLoop()
ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
ufo.x = ufo.x + ufo.xSpeed
numberOfTics = numberOfTics + 1
end

thankyou

Well, of course you need an enterFrame listener to call the gameLoop.

Runtime:addEventListner("enterFrame", gameLoop)

Call this after you declared the gameLoop function.

HI

ok im trying this code , but seems object just moves right along the screen and not up and down

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 2.5

local function gameLoop( event )
   ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
   ufo.x = ufo.x + 20 --ufo.xSpeed
   numberOfTics = numberOfTics + 1
end

Runtime:addEventListener(“enterFrame”, gameLoop)
 

your help much appreciated

You need a much lower value for ySpeed.

One sinus wave goes from zero (0) to two pi (6.2831…), so if you multiply pi with a values greater than 1 there wont be much going on.

Take something in the range 0.01 to 0.1 or even smaller.

Still no joy, still seems to move horizontal only

can somebody actually try the code

local ufo = display.newRect( 500, 500, 15, 15 )

local numberOfTics = 0
local ySpeed = 0.1

local function gameLoop( event )
   ufo.y = (250*math.sin(numberOfTicks*ySpeed*math.pi)) + 350
   ufo.x = ufo.x + 20 --ufo.xSpeed
   numberOfTics = numberOfTics + 0.1
end

Runtime:addEventListener(“enterFrame”, gameLoop)
 

I tried the code above and it does move downward on the screen. I modified it to slow the x axis speed of the object, so that you can see the behavior yourself:

local ufo = display.newRect( display.contentCenterX, display.actualContentHeight, 15, 15 ) local numberOfTics = 0 local ySpeed = 0.1 local function gameLoop( event ) ufo.y = (250\*math.sin(numberOfTics\*ySpeed\*math.pi)) ufo.x = ufo.x + 2 --ufo.xSpeed numberOfTics = numberOfTics + 0.1 end Runtime:addEventListener("enterFrame", gameLoop)