Corona SDK:如何在屏幕的顶部和底部创建不可触摸的区域?
Corona SDK: How to create untouchable area at very the top and bottom of the screen?
- 如果我触及最顶部(比如,
y=5
),物体应该停止
到达 y=50
时移动
- 如果我触摸底部(比方说
y=300
),物体应该停止
到达 y=250
时移动
这是我到目前为止所做的:
local function movePlayer(event)
if(event.phase == "ended") then
transition.to(player, {y=event.y, time=3000})
end
end
我该怎么做?
这很容易通过简单的比较逻辑解决,添加此函数并将其与您喜欢的参数(例如玩家 x 和 y)一起使用,在您应用到玩家之前限制它们的值。
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.
这是函数
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
使用示例
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)
编辑:
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 )
- 如果我触及最顶部(比如,
y=5
),物体应该停止 到达y=50
时移动
- 如果我触摸底部(比方说
y=300
),物体应该停止 到达y=250
时移动
这是我到目前为止所做的:
local function movePlayer(event)
if(event.phase == "ended") then
transition.to(player, {y=event.y, time=3000})
end
end
我该怎么做?
这很容易通过简单的比较逻辑解决,添加此函数并将其与您喜欢的参数(例如玩家 x 和 y)一起使用,在您应用到玩家之前限制它们的值。
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.
这是函数
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
使用示例
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)
编辑:
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 )