1、cocos2d-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))
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