我正在Windows上使用Corona SDK 2018.3319创建卡应用程序,并具有以下代码:
local widget = require("widget")
scrollView = widget.newScrollView(
{
top = display.contentHeight - display.contentHeight * 55 / 100,
left = 0,
width = display.contentWidth,
height = display.contentHeight * 55 / 100,
scrollWidth = 600,
horizontalScrollDisabled = true,
scrollHeight = 1300,
listener = scrollListener
}
)
local cards = {{name = "card1.png",x=8, y=8,w=70,h=90, sel=false},
{name = "card2.png",x=84, y=8,w=70,h=90, sel=false},
{name = "card3.png",x=160, y=8,w=70,h=90, sel=false},
{name = "card4.png",x=236, y=8,w=70,h=90, sel=false},
{name = "card5.png",x=8, y=106,w=70,h=90, sel=false},
{name = "card6.png",x=84, y=106,w=70,h=90, sel=false},
{name = "card7.png",x=160, y=106,w=70,h=90, sel=false},
{name = "card8.png",x=236, y=106,w=70,h=90, sel=false},
{name = "card9.png",x=8, y=204,w=70,h=90, sel=false},
{name = "card10.png",x=84, y=204,w=70,h=90, sel=false},
{name = "card11.png",x=160, y=204,w=70,h=90, sel=false},
{name = "card12.png",x=236, y=204,w=70,h=90, sel=false},
{name = "card13.png",x=8, y=302,w=70,h=90, sel=false},
{name = "card14.png",x=84, y=302,w=70,h=90, sel=false},
{name = "card15.png",x=160, y=302,w=70,h=90, sel=false},
{name = "card16.png",x=236, y=302,w=70,h=90, sel=false},
{name = "card17.png",x=8, y=400,w=70,h=90, sel=false},
{name = "card18.png",x=84, y=400,w=70,h=90, sel=false},
{name = "card19.png",x=160, y=400,w=70,h=90, sel=false},
{name = "card20.png",x=236, y=400,w=70,h=90, sel=false},
{name = "card21.png",x=8, y=498,w=70,h=90, sel=false},
{name = "card22.png",x=84, y=498,w=70,h=90, sel=false},
{name = "card23.png",x=160, y=498,w=70,h=90, sel=false},
{name = "card24.png",x=236, y=498,w=70,h=90, sel=false},
{name = "card25.png",x=8, y=596,w=70,h=90, sel=false},
{name = "card26.png",x=84, y=596,w=70,h=90, sel=false},
{name = "card27.png",x=160, y=596,w=70,h=90, sel=false},
{name = "card28.png",x=236, y=596,w=70,h=90, sel=false},
{name = "card29.png",x=8, y=694,w=70,h=90, sel=false},
{name = "card30.png",x=84, y=694,w=70,h=90, sel=false},
{name = "card31.png",x=160, y=694,w=70,h=90, sel=false},
{name = "card32.png",x=236, y=694,w=70,h=90, sel=false},
{name = "card33.png",x=8, y=796,w=70,h=90, sel=false},
{name = "card34.png",x=84, y=796,w=70,h=90, sel=false},
{name = "card35.png",x=160, y=796,w=70,h=90, sel=false},
{name = "card36.png",x=236, y=796,w=70,h=90, sel=false},
{name = "card37.png",x=8, y=894,w=70,h=90, sel=false},
{name = "card38.png",x=84, y=894,w=70,h=90, sel=false},
{name = "card39.png",x=160, y=894,w=70,h=90, sel=false},
{name = "card40.png",x=236, y=894,w=70,h=90, sel=false},
{name = "card41.png",x=8, y=992,w=70,h=90, sel=false},
{name = "card42.png",x=84, y=992,w=70,h=90, sel=false},
{name = "card43.png",x=160, y=992,w=70,h=90, sel=false},
{name = "card44.png",x=236, y=992,w=70,h=90, sel=false},
{name = "card45.png",x=8, y=1090,w=70,h=90, sel=false},
{name = "card46.png",x=84, y=1090,w=70,h=90, sel=false},
{name = "card47.png",x=160, y=1090,w=70,h=90, sel=false},
{name = "card48.png",x=236, y=1090,w=70,h=90, sel=false},
{name = "card49.png",x=8, y=1188,w=70,h=90, sel=false},
{name = "card50.png",x=84, y=1188,w=70,h=90, sel=false},
{name = "card51.png",x=160, y=1188,w=70,h=90, sel=false},
{name = "card52.png",x=236, y=1188,w=70,h=90, sel=false},
{name = "card53.png",x=8, y=1286,w=70,h=90, sel=false},
{name = "card54.png",x=84, y=1286,w=70,h=90, sel=false},
{name = "card55.png",x=160, y=1286,w=70,h=90, sel=false},
{name = "card56.png",x=236, y=1286,w=70,h=90, sel=false},
{name = "card57.png",x=8, y=1384,w=70,h=90, sel=false},
{name = "card58.png",x=84, y=1384,w=70,h=90, sel=false},
{name = "card59.png",x=160, y=1384,w=70,h=90, sel=false},
{name = "card60.png",x=236, y=1384,w=70,h=90, sel=false},
{name = "card61.png",x=8, y=1482,w=70,h=90, sel=false},
{name = "card62.png",x=84, y=1482,w=70,h=90, sel=false},
{name = "card63.png",x=160, y=1482,w=70,h=90, sel=false},
{name = "card64.png",x=236, y=1482,w=70,h=90, sel=false},
{name = "card65.png",x=8, y=1580,w=70,h=90, sel=false},
{name = "card66.png",x=84, y=1580,w=70,h=90, sel=false},
{name = "card67.png",x=160, y=1580,w=70,h=90, sel=false},
{name = "card68.png",x=236, y=1580,w=70,h=90, sel=false},
{name = "card69.png",x=8, y=1678,w=70,h=90, sel=false},
{name = "card70.png",x=84, y=1678,w=70,h=90, sel=false},
{name = "card71.png",x=160, y=1678,w=70,h=90, sel=false},
{name = "card72.png",x=236, y=1678,w=70,h=90, sel=false},
{name = "card73.png",x=8, y=1776,w=70,h=90, sel=false},
{name = "card74.png",x=84, y=1776,w=70,h=90, sel=false},
{name = "card75.png",x=160, y=1776,w=70,h=90, sel=false},
{name = "card76.png",x=236, y=1776,w=70,h=90, sel=false},
{name = "card77.png",x=8, y=1874,w=70,h=90, sel=false},
{name = "card78.png",x=84, y=1874,w=70,h=90, sel=false},
{name = "card79.png",x=160, y=1874,w=70,h=90, sel=false},
{name = "card80.png",x=236, y=1874,w=70,h=90, sel=false},
{name = "card81.png",x=8, y=1972,w=70,h=90, sel=false},
{name = "card82.png",x=84, y=1972,w=70,h=90, sel=false},
{name = "card83.png",x=160, y=1972,w=70,h=90, sel=false},
{name = "card84.png",x=236, y=1972,w=70,h=90, sel=false},
{name = "card85.png",x=8, y=2070,w=70,h=90, sel=false},
{name = "card86.png",x=84, y=2070,w=70,h=90, sel=false}}
local cardTouched = 0
local function touched(event)
for i = 1,#cards do
local card = event.target
if cards[i].sel == false and cardTouched == 1 then
card.alpha = .5
cardTouched = cardTouched - 1
cards[i].sel = true
elseif cards[i].sel == false and cardTouched == 0 then
card.alpha = 1
cardTouched = cardTouched + 1
cards[i].sel = false
end
end
end
for index = 1, 86 do
local card = display.newRect(cards[index].x,cards[index].y,cards[index].w,cards[index].h)
card.anchorX = 0
card.anchorY = 0
card:setFillColor(1,0,0)
scrollView:insert(card)
card:addEventListener("tap",touched)
end
我正在创建86个字母,我想触摸其中的一个字母时,它的不透明度为0.5,但一次只能一个,因此无法一次选择两个或多个。但我无法在循环中进行逻辑处理。
答案 0 :(得分:0)
您需要参考轻拍的卡片才能轻松更改不透明度。我将对所有卡的引用存储在myCardsRef
变量中。
试试
local mFloor = math.floor
local myCardsRef, cards = {}, {}
local lastTappedCard
scrollView = widget.newScrollView(
{
top = display.contentHeight - display.contentHeight * 55 / 100,
left = 0,
width = display.contentWidth,
height = display.contentHeight * 55 / 100,
scrollWidth = 600,
horizontalScrollDisabled = true,
scrollHeight = 1300,
listener = scrollListener
}
)
for i=1, 86 do
cards[#cards + 1] = {
name = 'card'..i..'.png',
x = 8 + ( i - 1 ) % 4 * 76,
y = 8 + ( mFloor( ( i - 0.5 ) / 4 ) * 98 ),
w = 70,
h = 90
}
end
local function tap( self, event )
if lastTappedCard then
myCardsRef[lastTappedCard].alpha = 1
end
myCardsRef[self].alpha = 0.5
lastTappedCard = self
end
for index = 1, #cards do
local card = display.newRect(cards[index].x,cards[index].y,cards[index].w,cards[index].h)
card.anchorX = 0
card.anchorY = 0
card:setFillColor( 1, 0, 0 )
card.tap = tap
card:addEventListener( "tap" )
scrollView:insert( card )
myCardsRef[card] = card
end
注意:我简化了cards
表以节省您的时间:)