Mobile Corona SDK:如何在屏幕的顶部和底部创建不可触及的区域? 如果我触摸顶部(例如,y=5),一个对象应该停止 当达到y=50时移动 如果我触摸底部(假设y=300),对象应该停止 当达到y=250时移动
以下是我迄今为止所做的工作:Mobile Corona SDK:如何在屏幕的顶部和底部创建不可触及的区域? 如果我触摸顶部(例如,y=5),一个对象应该停止 当达到y=50时移动 如果我触摸底部(假设y=300),对象应该停止 当达到y=250时移动,mobile,lua,sdk,coronasdk,Mobile,Lua,Sdk,Coronasdk,以下是我迄今为止所做的工作: 如何做到这一点?这很容易用简单的比较逻辑解决,添加此函数并将其与您喜欢的参数(例如播放器x和y)一起使用,以便在应用于播放器之前限制其值 此函数接受三个参数-数值、下限和 上限。它返回一个数字,保证在 这些边界 这就是函数 function math.clamp(value, low, high) if low and value <= low then return low elseif high and value &g
如何做到这一点?这很容易用简单的比较逻辑解决,添加此函数并将其与您喜欢的参数(例如播放器x和y)一起使用,以便在应用于播放器之前限制其值 此函数接受三个参数-数值、下限和 上限。它返回一个数字,保证在 这些边界 这就是函数
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 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 )
local backGround=display.newRect(display.actualcontenttwidth/2,display.actualContentHeight/2,display.actualcontenttwidth,display.actualContentHeight)
背景:setFillColor(0.5,0.5,0.5,1.0)
本地玩家=显示。新圆圈(100100,50)
函数数学钳位(值、低、高)
如果低且值=高,则
回潮
结束
返回值
结束
功能移动播放器(事件)
如果(event.phase==“moved”),则
本地xPosition=event.x
本地yPosition=event.y
xPosition=数学钳位(xPosition,0,150)
yPosition=数学钳位(yPosition,0,150)
转换到(玩家,{x=xPosition,y=yPosition,time=30})
结束
结束
player.touch=movePlayer
播放器:addEventListener(“触摸”,移动播放器)
背景:addEventListener(“触摸”,移动播放器)
谢谢,这可能是我所需要的,但是现在,我不太明白如何在我的函数movePlayer中使用它(我刚刚开始学习编码)。我已经编辑了我的答案,并添加了一个易于理解和测试的代码,它采用了前面提到的解决方案,如果您仍然需要帮助,请告诉我们。
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 )