Sure thing, I’m always up for a bit of code hacking!
Let’s assume you have your image sheet configured, named “imageSheet”, and your 9-slice rectangle will go inside a display group called “rectGroup”. For this example, we’ll also assume that the image frames to build it begin at the top-left corner (frame 1) and proceed across to the top-right corner (frame 3), then wrap to the middle “row” (frames 4-6) and finally the bottom “row” (frames 7-9).
[lua]
local rectCenterX = display.contentCenterX
local rectCenterY = display.contentCenterY
local rectWidth = 200
local rectHeight = 300
– Create the left portion of the rectangle
local rectTopLeft = display.newImage( rectGroup, imageSheet, 1 )
local rectMiddleLeft = display.newImage( rectGroup, imageSheet, 4 )
local rectBottomLeft = display.newImage( rectGroup, imageSheet, 7 )
– Create the right portion of the rectangle
local rectTopRight = display.newImage( rectGroup, imageSheet, 3 )
local rectMiddleRight = display.newImage( rectGroup, imageSheet, 6 )
local rectBottomRight = display.newImage( rectGroup, imageSheet, 9 )
– Create the middle portion of the rectangle
local rectTopMiddle = display.newImage( rectGroup, imageSheet, 2 )
local rectMiddle = display.newImage( rectGroup, imageSheet, 5 )
local rectBottomMiddle = display.newImage( rectGroup, imageSheet, 8 )
– Top positioning/sizing
rectTopLeft.x = rectCenterX + ( rectTopLeft.contentWidth * 0.5 )
rectTopLeft.y = rectCenterY + ( rectTopLeft.contentHeight * 0.5 )
rectTopMiddle.width = rectWidth - ( rectTopLeft.contentWidth + rectTopRight.contentWidth )
rectTopMiddle.x = rectTopLeft.x + ( rectTopLeft.contentWidth * 0.5 ) + ( rectTopMiddle.contentWidth * 0.5 )
rectTopMiddle.y = rectTopLeft.y
rectTopRight.x = rectTopMiddle.x + ( rectTopMiddle.contentWidth * 0.5 ) + ( rectTopRight.contentWidth * 0.5 )
rectTopRight.y = rectTopLeft.y
– Middle positioning/sizing
rectMiddleLeft.height = rectHeight - ( rectTopLeft.contentHeight + rectTopRight.contentHeight )
rectMiddleLeft.x = rectTopLeft.x
rectMiddleLeft.y = rectTopLeft.contentBounds.yMax + ( rectMiddleLeft.height * 0.5 )
rectMiddle.width = rectTopMiddle.width
rectMiddle.height = rectHeight - ( rectTopLeft.contentHeight + ( rectTopRight.contentHeight ) )
rectMiddle.x = rectTopMiddle.x
rectMiddle.y = rectMiddleLeft.y
rectMiddleRight.height = rectHeight - ( rectTopLeft.contentHeight + rectTopRight.contentHeight )
rectMiddleRight.x = rectTopRight.x
rectMiddleRight.y = rectMiddleLeft.y
– Bottom positioning/sizing
rectBottomLeft.x = rectTopLeft.x
rectBottomLeft.y = rectMiddle.y + ( rectMiddle.contentHeight * 0.5 ) + ( rectBottomLeft.contentHeight * 0.5 )
rectBottomMiddle.width = rectTopMiddle.width
rectBottomMiddle.x = rectTopMiddle.x
rectBottomMiddle.y = rectBottomLeft.y
rectBottomRight.x = rectTopRight.x
rectBottomRight.y = rectBottomLeft.y
[/lua]
OK, so I lied about it being about 10 lines, but it’s still not bad. :) Toss this into a function and it could potentially be used over and over throughout your code. You could even make it more elegant by passing in an image sheet reference to the function and then you’d have the ability to create different styled rectangles using different image sheets.
Hope this helps,
Brent
