【cocos2dx开发技巧5】脚本lua的使用--代码集锦

仿照“我叫MT”的控件

【cocos2dx开发技巧5】脚本lua的使用--代码集锦

代码如下:

	local function createBanner()
		local layerFarm = CCNode:create()

		local cache = CCSpriteFrameCache:sharedSpriteFrameCache()
		cache:addSpriteFramesWithFile("pic.plist")
		local spritebatch = CCSpriteBatchNode:create("pic.png")
		layerFarm:addChild(spritebatch)

		local bg = CCSprite:createWithSpriteFrameName("bg.png")
		spritebatch:addChild(bg)

		cclog("The size of bg is: %0.2f, %0.2f", bg:getContentSize().width, bg:getContentSize().height)

		local px = bg:getContentSize().width / 2;
		local py = bg:getContentSize().height /2;

		local head01 = CCSprite:createWithSpriteFrameName("head01.png")
		spritebatch:addChild(head01)
		head01:setAnchorPoint(ccp(0, 0.5));
		head01:setPosition(-px, 0);

		local lv = CCSprite:createWithSpriteFrameName("lv.png")
		spritebatch:addChild(lv);
		lv:setPosition(-px + 10, -py + 10);
		lv:setAnchorPoint(ccp(0, 0));

		local name = CCLabelTTF:create("yyyy", "Arial", 24)
		layerFarm:addChild(name);
		name:setAnchorPoint(ccp(0, 0));
		name:setPosition(-135, 5);

		local hp = CCLabelTTF:create("7569", "Arial", 24)
		layerFarm:addChild( hp);
		hp:setAnchorPoint(ccp(0, 0));
		hp:setPosition(-140, -35);

		local zhandou = CCLabelTTF:create("2334", "Arial", 24)
		layerFarm:addChild( zhandou);
		zhandou:setAnchorPoint(ccp(0, 0));
		zhandou:setPosition(-25, -35);

		return layerFarm;
	end


	local function createLayerFarm()
		local layerFarm = createBanner()

		layerFarm:setScale(0.8);
		layerFarm:setPosition(origin.x + visibleSize.width / 2 , origin.y + visibleSize.height / 2)

		return layerFarm;
	end


按钮控件

【cocos2dx开发技巧5】脚本lua的使用--代码集锦

	local function createBtn(name, callback)
		local pMainBtn = CCMenuItemSprite:create(
										CCSprite:createWithSpriteFrameName("d_"..name..".png"),
                                        CCSprite:createWithSpriteFrameName("d_"..name.."_s.png"), 
                                        CCSprite:createWithSpriteFrameName("d_"..name.."_s.png"));
        pMainBtn:registerScriptTapHandler(callback)
        pMainBtn:setAnchorPoint(ccp(0, 0))
        return pMainBtn;
	end

	local function createButtoms()
		local node = CCNode:create();
		local cache = CCSpriteFrameCache:sharedSpriteFrameCache()
		cache:addSpriteFramesWithFile("images/gTopper.plist", "images/gTopper.png");
		
		local function cbHome()
			cclog("home")
		end
		local function cbFuben()
			cclog("cbFuben")
		end
		local function cbZhandou()
			cclog("cbZhandou")
		end
		local function cbStore()
			cclog("cbStore")
		end
		local function cbFriends()
			cclog("cbFriends")
		end
		local function cbGonglve()
			cclog("cbGonglve")
		end
		
		local bHome = createBtn("home", cbHome);
		local bFuben = createBtn("fuben", cbFuben);
		local bZhandou = createBtn("zhandou", cbZhandou);
		local bStore = createBtn("store", cbStore);
		local bFriends = createBtn("friends", cbFriends);
		local bGonglve = createBtn("gonglve", cbGonglve);
                 
		local  menu = CCMenu:create()

        menu:addChild(bHome)
        menu:addChild(bFuben)
        menu:addChild(bZhandou)
        menu:addChild(bStore)
        menu:addChild(bFriends)
        menu:addChild(bGonglve)
        
       	local size = bHome:getContentSize().width;
       	local gap = 8
       	bHome:setPosition(-3*(size + gap), 0)
       	bFuben:setPosition(-2*(size + gap), 0)
       	bZhandou:setPosition(-1*(size + gap), 0)
       	bStore:setPosition(0*(size + gap), 0)
       	bFriends:setPosition(1*(size + gap), 0)
       	bGonglve:setPosition(2*(size + gap), 0)
        
		menu:setPosition( 0, origin.y + visibleSize.height / 2)
		node:addChild(menu)
		
		return node
	end


toluafix, CCNode::registerScriptHandler, toluafix_isfunction

你可能感兴趣的:(cocos2dx)