y=5
), an object should stop
moving when it reaches y=50
y=300
), an object should stop
moving when it reaches y=250
Here is what i have done so far:
local function movePlayer(event)
if(event.phase == "ended") then
transition.to(player, {y=event.y, time=3000})
end
end
How do i do that?
答案 0 :(得分:1)
this is easy to solve with simple comparison logic, add this function and use it with the arguments you like for (player x and y for example), to limit their values before you apply to the player.
This function accepts three arguments - number value, low limit and high limit. And it returns a number, which is guranteed to be within these boundaries.
this is the function
function math.clamp(value, low, high)
if low and value <= low then
return low
elseif high and value >= high then
return high
end
return value
end
usage example
local actualSpeed = math.clamp(calculatedSpeed, 0, 200)
local temperature = math.clamp(temperature, -270)
player.x = math.clamp(player.x, 0, map.width)
cannon.rotation = math.clamp(cannon.rotation, -90, 90)
local age = math.clamp(age, 1, 120)
-- Six has higher probability than any other number
local luckyDice = math.clamp(math.random(1, 10), nil, 6)
EDIT :
local backGround = display.newRect(display.actualContentWidth/2,display.actualContentHeight/2,display.actualContentWidth,display.actualContentHeight)
backGround:setFillColor( 0.5, 0.5, 0.5, 1.0 )
local player = display.newCircle(100,100,50)
function math.clamp(value, low, high)
if low and value <= low then
return low
elseif high and value >= high then
return high
end
return value
end
function movePlayer(event)
if(event.phase == "moved") then
local xPosition = event.x
local yPosition = event.y
xPosition = math.clamp(xPosition, 0, 150)
yPosition = math.clamp(yPosition, 0, 150)
transition.to(player, {x=xPosition,y=yPosition,time = 30})
end
end
player.touch = movePlayer
player:addEventListener( "touch", movePlayer )
backGround:addEventListener( "touch", movePlayer )