触摸功能不起作用。

我正在尝试在我的iPad上的Codea中创建一个简单的应用程序,用于显示图像并让用户移动它。我能够正确地显示图片,但无法用手指移动它。

以下是我的代码。

function touched(touch)

 local currentTouchPosition = vec2(touch.x,touch.y)

if (touch.state == BEGAN) then

end

if (touch.state == MOVING) then

if   ((imagePosition.x - imageSize.x/2) < currentTouchPosition.x and
         (imagePosition.x + imageSize.x/2) > currentTouchPosition.x and
         (imagePosition.y - imageSize.y/2) < currentTouchPosition.y and
         (imagePosition.y + imageSize.y/2) > currentTouchPosition.y  ) then

        imagePosition = currentTouchPosition
    end
  end

 if (touch.state == ENDED) then

end

end

我应该如何使它工作?...提前感谢。

点赞
用户10732799
用户10732799

我猜你现在已经找到了答案,但如果你还没有,我希望这能帮到你。

不确定你在setup()或draw()中做了什么,但我所做的是定义了imageSize和imagePosition作为vec2,并给它们一个初始值。我还添加了一个简单的图像。

除了在touched()中稍微格式化代码之外,代码看起来很好。

我希望以下代码是有意义的。

function setup()

-- 屏幕中心
X = WIDTH/2
Y = HEIGHT/2

imageDims = 100 -- 定义图像大小

imagePosition = vec2(X,Y) -- 在setup()中将imagePosition和imageSize定义为vec2
imageSize = vec2(imageDims,imageDims)

end

function draw()

background(40, 40, 50)

sprite("Cargo Bot:Codea Icon",
    imagePosition.x,imagePosition.y,
    imageSize.x,imageSize.y)

end

function touched(touch)

local currentTouchPosition = vec2(touch.x,touch.y)

if (touch.state == BEGAN) then end

if (touch.state == MOVING) then
    if ((imagePosition.x - imageSize.x/2) < currentTouchPosition.x and
        (imagePosition.x + imageSize.x/2) > currentTouchPosition.x and
        (imagePosition.y - imageSize.y/2) < currentTouchPosition.y and
        (imagePosition.y + imageSize.y/2) > currentTouchPosition.y) then

        imagePosition = currentTouchPosition
    end
end

if (touch.state == ENDED) then end

end
2018-12-01 21:23:08