cocos2dx代码收集 (1)

前端之家收集整理的这篇文章主要介绍了cocos2dx代码收集 (1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1cocos2d-x 2.2.2 lua中更换CCSprite图片

--lua
--获取场景
localscene=CCDirector:sharedDirector():getRunningScene()

--创建精灵
localtestSp=CCSprite:create()
testSp:setPosition(ccp(250,250))
scene:addChild(testSp,200)

--更换图片
localtexture=CCTextureCache:sharedTextureCache():addImage("icon/100001.png")
texture:retain()
testSp:setTexture(texture)

--这里一定要设置好大小
locals=texture:getContentSize()
testSp:setTextureRect(CCRectMake(0,s.width,s.height))

2、实现一种页面指示器功能

functionGMKnapsackItems:createItemsPageIndicator(totalCount,curSelectIndex)

ifself.m_itemsIndicatorNode ~= nil then

self.m_itemsIndicatorNode:removeFromParent()

end

local indicatorNode = CCNode:create()

local horizontalDis = 5

local indicatorImage = nil

local indicatorImageSize = nil

for i=1,totalCount do

if curSelectIndex == (i-1) then

indicatorImage =CCSprite:create(GGxGetPng(picMag.knapsack_page_indicator_normal))

else

indicatorImage = CCSprite:create(GGxGetPng(picMag.knapsack_page_indicator_select))

end

if indicatorImageSize == nil then

indicatorImageSize = indicatorImage:getContentSize()

end

local posX = (i-1)*(indicatorImageSize.width+horizontalDis)

local posY = 0

indicatorImage:setAnchorPoint(CCPoint(0,0))

indicatorImage:setPosition(posX,posY)

indicatorNode:addChild(indicatorImage)

end

local indicatorNodeWidth =totalCount*indicatorImageSize.width + (totalCount-1)*horizontalDis

local indicatorNodeHeight =indicatorImageSize.height

indicatorNode:setContentSize(CCSizeMake(indicatorNodeWidth,indicatorNodeHeight))

indicatorNode:setAnchorPoint(CCPoint(0.5,0))

local itemsMainFrame = self.m_itemsMainFrame

local itemsMainFrameSize = itemsMainFrame:getContentSize()

indicatorNode:setPosition(itemsMainFrameSize.width/2,8)

itemsMainFrame:addChild(indicatorNode)

self.m_itemsIndicatorNode =indicatorNode

end

原文链接:https://www.f2er.com/cocos2dx/344760.html

猜你在找的Cocos2d-x相关文章