Sprite line thickness is strange. And then disappear (v3.17)

cocos2dx 3.17 (C++)
NDK r16b
Android 6.0 (huawei vns-l22)

I draw lines with Sprite.
After updating to v3.17, the thickness of lines become disjointed with some Android.
It is beautifully lined up in v3.14.1.

Is there a solution?

Sprite line code

for (int x = 0; x <= size.width*0.5f; x++) {
	Rect rect_x = Rect( 0.0f, 0, 1.0f, 200.0f);
	Sprite* line_x = Sprite::create();
	line_x->setTextureRect( rect_x );
	line_x->setPosition( x*2 , 400);
	line_x->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_x->setOpacity(255);
	this->addChild(line_x);

	Rect rect_x2 = Rect( 0.0f, 0, 1.0f, 200.0f);
	Sprite* line_x2 = Sprite::create();
	line_x2->setTextureRect( rect_x2 );
	line_x2->setPosition( (x*2+1) , 800);
	line_x2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_x2->setOpacity(255);
	this->addChild(line_x2);
}

for (int x = 0; x <= size.height*0.5f; x++) {
	Rect rect_y = Rect( 0.0f, 0, 200.0f, 1.0f);
	Sprite* line_y = Sprite::create();
	line_y->setTextureRect( rect_y );
	line_y->setPosition( 0  , x*2 );
	line_y->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_y->setColor(Color3B::WHITE);
	line_y->setOpacity(255);
	this->addChild(line_y);

	Rect rect_y2 = Rect( 0.0f, 0, 200.0f, 1.0f);
	Sprite* line_y2 = Sprite::create();
	line_y2->setTextureRect( rect_y2 );
	line_y2->setPosition( 400  , (x*2+1) );
	line_y2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_y2->setColor(Color3B::WHITE);
	line_y2->setOpacity(255);
	this->addChild(line_y2);
}

v3.17

v3.14.1

It’s very troubled :cold_sweat:
Is there a solution?

You can try enclosing all your coordinates with floor(). Your code uses integer values and doesn’t seem to require it but as something to try.

Also, you show the code to position the sprites but not the code to position the parent node. Try to enclose the parent node position with floor() and see if something changes. Is the parent node the Scene or a cocos2d::Node?

Try doing this to the parent node:

// node is the parent of the line sprites
cocos2d::Vec2 position = node->getPosition();
node->setPosition(floor(position.x), floor(position.y));

You can only do this if your design resolution is not set to something weird like 320x240 and you don’t need to place things at 1/2 or 1/4 of a pixel (in design units).

Thanks for your reply.

Parent is cocos2d::Layer;
Cocos2d resolution is same as android resolution.

I fixed the integer to float but it did not solve it.

iOS has no problem.
There was no problem with other Android.

I doubt the setting of OpenGL :thinking:

// 縦線 DrawLine
for (int x = 0; x <= size.width*0.5f; x++) {
	Rect rect_x = Rect( 0.0f, 0.0f, 1.0f , 200.0f);
	Sprite* line_x = Sprite::create();
	line_x->setTextureRect( rect_x );
	line_x->setPosition( float(x)*2.0f , 400.0f);
	line_x->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_x->setOpacity(255);
	this->addChild(line_x);

	Rect rect_x2 = Rect( 0.0f, 0.0f, 1.0f, 200.0f);
	Sprite* line_x2 = Sprite::create();
	line_x2->setTextureRect( rect_x2 );
	line_x2->setPosition( float(x)*2.0f+1.0f , 800.0f);
	line_x2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_x2->setOpacity(255);
	this->addChild(line_x2);
}

for (int x = 0; x <= size.height*0.5f; x++) {
	Rect rect_y = Rect( 0.0f, 0.0f, 200.0f, 1.0f);
	Sprite* line_y = Sprite::create();
	line_y->setTextureRect( rect_y );
	line_y->setPosition( 0.0f  , float(x)*2.0f);
	line_y->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_y->setColor(Color3B::WHITE);
	line_y->setOpacity(255);
	this->addChild(line_y);

	Rect rect_y2 = Rect( 0.0f, 0.0f, 200.0f, 1.0f);
	Sprite* line_y2 = Sprite::create();
	line_y2->setTextureRect( rect_y2 );
	line_y2->setPosition( 400.0f  , float(x)*2.0f+1.0f);
	line_y2->setAnchorPoint(Vec2::ANCHOR_TOP_LEFT);
	line_y2->setColor(Color3B::WHITE);
	line_y2->setOpacity(255);
	this->addChild(line_y2);
}

cocos2d::Vec2 position = this->getPosition();
this->setPosition(floor(position.x), floor(position.y));

I only can think in an interpolation artifact. The resolution of the game may be the same as your test phone but maybe the presence of the action bar at the top and the system buttons makes cocos to have to interpolate.

If you take a screenshot, with the correct key combination for that particular phone, and analyze it in an image editor you can find out the metrics of the action bar and system buttons bar. I don’t know if there are functions to get the metrics from C++ code. This is useful only to identify the problem, it’s not a solution. Use that specific screen resolution is not an option (it will break for some other phone sooner or later).

Sorry, I’m out of ideas for now.

1 Like

cocos2dx 3.16 is no problem.
It is probably a bug of 3.17.

3.16