Background image tiling with cocos2d-x binding Lua

Time:2020-11-10

Usage context:
First, create a panel in cocos studio as the background layer, in which you need to tile the pictures.

The code is as follows:
The notes indicate the wrong method I used, which should be noted.

local panelPage1     = ccui.Helper:seekWidgetByName(uiNode, "Panel_page1")
local panelRepeatBg1 = ccui.Helper:seekWidgetByName(panelPage1, "Panel_repeat_bg")

--Note 1: don't use plist method to specify the image for Sprite, otherwise gettexture() will get the whole picture of plist file instead of the small picture you specified
-- cc.SpriteFrameCache:getInstance():addSpriteFrames("publish/More/More_png.plist")
-- local bgSprite = cc.Sprite:createWithSpriteFrameName("More_repeat_bg.png")

local bgSprite = cc.Sprite:create("More/More_repeat_bg.png")

bgSprite:setAnchorPoint(0, 0)
bgSprite:setPosition(0, 0)

--Note 2: you can't just set size to sprite, you should set rect instead
-- local bgSize = panelRepeatBg1:getContentSize()
-- bgSprite:setContentSize(bgSize)

local bgRect = panelRepeatBg1:getBoundingBox()
bgSprite:setTextureRect(bgRect)

bgSprite:getTexture():setTexParameters(gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT)

panelRepeatBg1:addChild(bgSprite)

--The meaning of the four parameters in todo settexparameters() remains to be found out