math.random + something not working

Hello all, just started learning corona and can’t figure this out myself. Don’t even know if my code is making any sense but here we go : 

centerY = display.contentCenterY local gamescore = 0 function ChangeLeftButton() gamescore = gamescore + 1 local yp = {} yp[1] = centerY yp[2] = centerY + 20 yp[3] = centerY + 40 yp[4] = centerY + 60 if blankLeft.y == yp[1] then transition.to(blankLeft.y, {y = yp.."["..math.random(2,4).."]"}) elseif blankLeft.y == yp[2] then transition.to(blankLeft.y, {y = yp.."[1]" or yp.."["..math.random(3,4).."]" }) elseif blankLeft.y == yp[3] then transition.to(blankLeft.y, {y = yp.."[4]" or yp.."["..math.random(1,2).."]" }) elseif blankLeft.y == yp[4] then transition.to(blankLeft.y, {y = yp.."["..math.random(1,3).."]" }) end end function gameStarted() blankLeft = widget.newButton{ defaultFile = "blankball.png", x = centerX - 20, y = centerY, onPress = ChangeLeftButton } end

Are you trying to multiply math.random with yp?  the “…” is used for concatenation 

I am trying to change Y position to one of the four choices without the current

if it is one should the next be 2 or 3 or 4

if it is two should the next be 1 or 3 or 4

if it is tree should the next be 1 or 2 or 4

if it is four should the next be 1 or 2 or 3

The syntax you’re using is wrong, try this:

centerY = display.contentCenterY local gamescore = 0 function ChangeLeftButton() gamescore = gamescore + 1 local yp = {} yp[1] = centerY yp[2] = centerY + 20 yp[3] = centerY + 40 yp[4] = centerY + 60 local toY = y[1] if blankLeft.y == yp[1] then toY = yp[math.random(2,4)] elseif blankLeft.y == yp[2] then toY = yp[math.random(3,4)] elseif blankLeft.y == yp[3] then toY = yp[math.random(1,2)] elseif blankLeft.y == yp[4] then toY = yp[math.random(1,3)] end transition.to(blankLeft, { y = toY }) end function gameStarted() blankLeft = widget.newButton{ defaultFile = "blankball.png", x = centerX - 20, y = centerY, onPress = ChangeLeftButton } end

Note: The issue was you trying to concatenate a script like this:

transition.to(blankLeft.y, {y = yp.."[1]" or yp.."["..math.random(3,4).."]" }) 

That won’t work.  i.e. It isn’t valid Lua code, and you’re not allowed to assemble scripts as strings and execute them later.  That ability is removed from Lua for mobile and event if it were present, that’s not the right way to achieve it.  No big deal, but just something to keep in mind.

Oh, and this was wrong too:

transition.to(blankLeft.y, { y = toY }) 

You want to modify the ‘y’ field on the object ‘blankLeft’,

transition.to(blankLeft, { y = toY })

Are you trying to multiply math.random with yp?  the “…” is used for concatenation 

I am trying to change Y position to one of the four choices without the current

if it is one should the next be 2 or 3 or 4

if it is two should the next be 1 or 3 or 4

if it is tree should the next be 1 or 2 or 4

if it is four should the next be 1 or 2 or 3

The syntax you’re using is wrong, try this:

centerY = display.contentCenterY local gamescore = 0 function ChangeLeftButton() gamescore = gamescore + 1 local yp = {} yp[1] = centerY yp[2] = centerY + 20 yp[3] = centerY + 40 yp[4] = centerY + 60 local toY = y[1] if blankLeft.y == yp[1] then toY = yp[math.random(2,4)] elseif blankLeft.y == yp[2] then toY = yp[math.random(3,4)] elseif blankLeft.y == yp[3] then toY = yp[math.random(1,2)] elseif blankLeft.y == yp[4] then toY = yp[math.random(1,3)] end transition.to(blankLeft, { y = toY }) end function gameStarted() blankLeft = widget.newButton{ defaultFile = "blankball.png", x = centerX - 20, y = centerY, onPress = ChangeLeftButton } end

Note: The issue was you trying to concatenate a script like this:

transition.to(blankLeft.y, {y = yp.."[1]" or yp.."["..math.random(3,4).."]" }) 

That won’t work.  i.e. It isn’t valid Lua code, and you’re not allowed to assemble scripts as strings and execute them later.  That ability is removed from Lua for mobile and event if it were present, that’s not the right way to achieve it.  No big deal, but just something to keep in mind.

Oh, and this was wrong too:

transition.to(blankLeft.y, { y = toY }) 

You want to modify the ‘y’ field on the object ‘blankLeft’,

transition.to(blankLeft, { y = toY })