在Corona SDK中,如何响应触摸之类的事件来获取DisplayObject的颜色(填充)? [英] How can I get the colour (fill) of a DisplayObject in response to an event like a touch in Corona SDK?

查看:68
本文介绍了在Corona SDK中,如何响应触摸之类的事件来获取DisplayObject的颜色(填充)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我找出如何获得电晕中的矩形颜色吗?该矩形我已经充满了颜色,所以现在我想在触摸矩形时获得该颜色。

Can anyone help me find out how can I can get the color of rectangle in corona? That rectangle I already filled with color, so now I want to get that color when I touch on rectangle.

推荐答案

创建矩形:

local rectangle = display.newRect(0, 0, 100, 100)

将颜色以RGBA(您可以省略A)格式放在表中,并将其存储为矩形的自定义属性:

Put your color in RGBA (you can leave out the A) format in a table, and store it as a "custom property" for the rectangle:

rectangle.fillColor = {110, 30, 25}

通过unpack函数的神奇作用,该函数返回表的值,将该表传递给setFillColor:

Through the magic of the unpack function, which returns the values of a table, pass the table to setFillColor:

rectangle:setFillColor( unpack(rectangle.fillColor) )

现在,您总是可以像这样获得颜色:

Now you can always get the color like so:

print( unpack(rectangle.fillColor) ) --> 110    30    25

print( rectangle.fillColor ) -- simply returns the table

或将每种颜色放入变量中:

or to put each color in a variable:

local red, green, blue, alpha = unpack(rectangle.fillColor)

您还将看到这对于其他方面也很方便。

You'll see how this can come in handy for other things as well.

编辑

只要想起另一种很酷的方法,就可以劫持setFillColor函数:

Just thought of another cool way of doing it, by highjacking the setFillColor function:

local function decorateRectangle(rect)
    rect.cachedSetFillColor = rect.setFillColor -- store setFillColor function

    function rect:setFillColor(...) -- replace it with our own function
        self:cachedSetFillColor(...)
        self.storedColor = {...} -- store color
    end

    function rect:getFillColor()
        return unpack(self.storedColor)
    end
end

local rectangle = display.newRect(0, 0, 100, 100)

decorateRectangle(rectangle) -- "decorates" rectangle with more awesomeness

现在,您可以使用setFillColor设置颜色为正常值,并使用getFillColor返回颜色:)

Now you can use setFillColor to set color as normal, AND getFillColor to return it :)

rectangle:setFillColor(100, 30, 255, 255)

print(rectangle:getFillColor())

这篇关于在Corona SDK中,如何响应触摸之类的事件来获取DisplayObject的颜色(填充)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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