Love2d光标位置 [英] Love2d cursor positions

查看:159
本文介绍了Love2d光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于love2d(lua脚本)游标函数的问题。我不想让一个区域点击,使行动发生。



我开始在x和y参数循环的轨道上。唯一的问题是,如果它将经过一个数字/坐标的循环,并完成了love.mouse.get()将要结束的一个数字,并允许光标最终被点击最后一个坐标(一个像素)。

pre $ 对于r = 660,770 do - x坐标
mx = love.mouse.getX(r)
end

for f = 99.33,169.66 do - the y coordinate
my = love.mouse.getY(f)
end

如何将两个循环变量(r和f)结合在一起。



总结一切,我希望能够点击一个地区,并采取行动。我知道没有love.load,love.update和love.draw函数,因为这只是一个测试文件,以了解所有这些工作。

谢谢:


解决方案

。你真正想做的是在两个维度中定义一个最小值和一个最大值,监听鼠标事件,然后检查鼠标位置是否在你的边界内。考虑这个例子中的'游戏',我们绘制一个简单的红色框,当点击触摸顶部的文本显示时

  local box_dims = {
{660,770},
{99.33,169.66}










x> = box_dims [1] [1]和
x <= box_dims [1] [2]和
y> = box_dims [2] [1]和
y <= box_dims [2] [2] b
$ b show = not show
end
end

function love.draw()
love.graphics.setColor(255,0,0 ,255)

love.graphics.rectangle('fill',
box_dims [1] [1],box_dims [2] [1],
box_dims [1] [ 2] - box_dims [1] [1],
box_dims [2] [2] - box_dims [2] [1]


如果显示则
love.graphics.print('hello world',10,10)
end
end






查看文档以确定哪个鼠标活动是适合你的。




I have a question about the love2d(lua script) cursor functions. I wan't to make an area to click to make an action happen.

I started on a track of for loops in the x and y parameters. The only other problem ive thought of is if it will go through a for loop of number/coordinates and finish on 1 number which the love.mouse.get() will end up on and allow for the cursor to end up being clicked on that last coordinate(one pixel).

for r = 660, 770 do --the x coordinates
mx = love.mouse.getX(r)
end

for f = 99.33, 169.66 do  --the y coordinates
my = love.mouse.getY(f)
end

And how would I combine the two for loop variables(r and f).

To sum everything up I want to be able to click on an area and do an action. I know there are no love.load, love.update, and love.draw functions because this is just a test file to learn how all this works.

Thank you :)

解决方案

You're overthinking this problem. What you really want to do is define a minimum and a maximum in two dimensions, listen for mouse events, and then check if the mouse location is within your boundaries. There is no need to loop over your entire ranges.

Consider this example 'game' where we draw a simple red box which when clicked toggles the display of text in the top left corner.

local box_dims = {
    { 660, 770 },
    { 99.33, 169.66 }
}

local show = false

function love.mousepressed (x, y)
    if
        x >= box_dims[1][1] and
        x <= box_dims[1][2] and
        y >= box_dims[2][1] and
        y <= box_dims[2][2] then

        show = not show
    end
end

function love.draw ()
    love.graphics.setColor(255, 0, 0, 255)

    love.graphics.rectangle('fill',
        box_dims[1][1], box_dims[2][1],
        box_dims[1][2] - box_dims[1][1],
        box_dims[2][2] - box_dims[2][1]
    )

    if show then
        love.graphics.print('hello world', 10, 10)
    end
end


Take a look at the documentation to decide which mouse event is right for you.

这篇关于Love2d光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆