require("Card")
function main()
local self = CCScene:create()
local layer
local allPoints
local allCards = {}
local countNum =1
local errCount = 0
local function getPoints()
allPoints = {}
for i=0,9 do
for j=0,5 do
table.insert(allPoints,1,ccp(i*80,j*80))
end
end
end
local function addCards()
math.randomseed(os.time())
local c
local randNum
local p
for var=1,10 do
c = Card(var)
layer:addChild(c)
randNum = math.random(table.maxn(allPoints))
p = table.remove(allPoints,randNum)
c:setPosition(p);
table.insert(allCards,1,c)
end
end
local function startGame()
--local c = Card(2)
--layer:addChild(c)
--c:showBg()
countNum = 1
getPoints()
addCards()
end
local function showAllCardsBg()
for key,var in ipairs(allCards) do
var:showBg()
end
end
local function onTouch(type,x,y)
local p = ccp(x,y)
for key,var in pairs(allCards) do
if var:boundingBox():containsPoint(p) then--检查是否点击在图片的范围内
if countNum == var.num then
table.remove(allCards,key)
layer:removeChild(var,true)
print(var.num)
if countNum == 1 then
showAllCardsBg()
end
if table.maxn(allCards)<= 0 then
print ("success")
end
countNum = countNum+1
else
errCount = errCount + 1
--if errCount >= 3 then
--startGame()
--end
end
break
end
end
end
local function init()
layer = CCLayer:create()
self:addChild(layer)
layer:setTouchEnabled(true)
layer:registerScriptTouchHandler(onTouch)
startGame()
--local s = CCSprite:create("ma.jpg")
--s:setPosition(ccp(300,300))
--layer:addChild(s)
--layer:setTouchEnabled(true)
--layer:registerScriptTouchHandler(function (type,x,y)
--print(type,x,y)
-- if s:boundingBox():containsPoint(ccp(x,y)) then
-- print ("mashibing click")
-- end
-- return true
--end)
end
init()
return self
end
local function __main()
CCDirector:sharedDirector():runWithScene(main())
local dir = CCDirector:sharedDirector()
dir:setDisplayStats(false)
CCEGLView:sharedOpenGLView():setDesignResolutionSize(800,480,kResolutionShowAll)
end
__main()
function Card(num)
local self = CCSprite:create()
local txt,bg
local function init()
self.num = num
self:setContentSize(CCSizeMake(80,80))
self:setAnchorPoint(ccp(0,0))
txt = CCLabelTTF:create(num,"Courier",50)
txt:setPosition(ccp(40,40))
self:addChild(txt)
bg = CCSprite:create("chen.jpg")
bg:setContentSize(CCSizeMake(80,80))--设置图片大小
bg:setPosition(ccp(40,40))
--bg:setTextureRect(CCRectMake(0,0,80,80))
--bg:setAnchorPoint(ccp(0,0))
self:addChild(bg)
self:showTxt()
end
self.showTxt = function ()
txt:setVisible(true)--设置对象的可见性
bg:setVisible(false)
end
self.showBg = function ()
txt:setVisible(false)
bg:setVisible(true)
end
init()
return self
end