Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17078

Scale9Sprite setSize - solved

$
0
0

@serpri wrote:

My problem was: how to set size of (set Scale9) button or Sprite.
My version of solution:

float S_sz = 513; // set it or get it - Sprite real size, in pixels (in my case - 513x513 pixels);

float B_w  = S_sz / 3.0f; // -the width of "border" == size of Sprite block in pixels; since Sprite will be divided to 3x3 blocks, so we have to divide Sprite size by 3;

Size block_q = Size(3.5, 4); // size of the Spite on screen - in block units! (in my case - 3.5x4 blocks);
// should be more than 2x2 if you want border corners to be squared.

Scale9Sprite * p_sp = Scale9Sprite::create("sprite_file_name.png");
if (p_sp)
{
	p_sp->setScale9Enabled(1);
	this->addChild(p_sp, 100); // todo Z order!!

	p_sp->   // here we set Scale9 button or Sprite size in blocks;
		setPreferredSize
	//	setContentSize // both works ok;
			(Size( B_w * block_q.width , B_w * block_q.height ));

	auto object_size_on_screen_on_pixels = Size( 123, 456 ); // todo - by yourself;
	float scale = object_size_on_screen_on_pixels.width / ( B_w * block_q.width);

	p_sp->setScale( scale ); // here we set Scale9 button or Sprite size in pixels - on screen;
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17078

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>