cocos2d-x 平时小例子(3)和lua的面向对象

1.csloader的加载

function MainScene:createStInput() 
  --self.input = ccs.GUIReader:getInstance():widgetFromBinaryFile("MainScene.csb") 
  --self:addChild(self.input) 
  local buttonLayer = cc.CSLoader:createNode("Layer.csb")
  local button = buttonLayer:getChildByName("Button_1") 
  button:retain() 
  --button:removeFromParentAndCleanup(true) 
  button:removeFromParent(true)
  button:setPosition(display.cx, display.cy) 
  --print(button:getReferenceCount()) 
  --print(buttonLayer:getReferenceCount()) 
  local input = buttonLayer:getChildByName("TextField_1")
  input:retain() input:removeFromParentAndCleanup(true)
  input:setPosition(display.cx - 100, display.cy - 100) 
  self:addChild(input) self:addChild(button)
end

2.ClippingNode的加载

local spritePlist = cc.SpriteFrameCache:getInstance(): addSpriteFrames("ui/mainUi12/uiMain12Plist.plist")  
local sp = cc.Sprite:createWithSpriteFrame(spritePlist:getSpriteFrameByName("ui/mainUi12/uiMain12RightBig1.png"))
--这个很重要不设置裁剪会出现问题 
sp:setScale(self.topPanel:getContentSize().width / sp:getContentSize().width)           
sp:setPosition(cc.p(0, 0))   
local clip = cc.ClippingNode:create(sp) 
clip:setInverted(false)
clip:setAlphaThreshold(0) 
clip:addChild(self.topPanel2)
clip:setPosition(cc.p(size.width / 2, topMainY))  
self:addChild(clip)

3.SpriteBatchNode的加载

self.plist = ******* 
local map = self.plist:getSpriteFrameByName ("plist/map/mapBg001.png")
local spriteBatchNode = cc.SpriteBatchNode: createWithTexture(map:getTexture(),100)    
spriteBatchNode:setAnchorPoint(cc.p(0, 0)) 
if nil ~= spriteBatchNode then 
  for i = 1, self.mapWidthNum do 
    for j = 1, self.mapHeightNum do 
      local mapspriteM = cc.Sprite:createWithSpriteFrame(map) 
      mapspriteM:setAnchorPoint(cc.p(0, 0)) 
      mapspriteM:setPosition(cc.p(**,**))  
      spriteBatchNode:addChild(mapspriteM, 1, i * j) 
     end 
   end
end

4.lua面向对象的写法

A = {x = 0, y =0}
function A:new(o)
  o = o or {} 
  if o.x ~= nil then 
    self.x = o.x
  end  

  if o.y ~= nil then 
    self.y = o.y 
  end
  setmetatable(o,self) 
  self.__index = self 
  return o
end

function A:setA(btx) 
  self.x = btxendfunction
  A:funName() print('A')
end

function A:printList1() 
   print(self.x ,self.y)
end

B = A:new({x =2, y = 2})
function B:printList1() 
  print(self.x + 1 ,self.y + 1)
end

A:setA(6)
A:printList1()
B:printList1()
s = B:new()
function B:funName() 
  print('B')
end
s:funName()
cocos2d-x 平时小例子(3)和lua的面向对象_第1张图片
运行结果

你可能感兴趣的:(cocos2d-x 平时小例子(3)和lua的面向对象)