Game is not smooth

Game is not smooth
0

Im making a game using cocos2dx 3.17.2. The problem is that game doesnt run smoothly and i couldnt find a solution (tried many things). Screen is flickering on both android and win32.

Visual studio profiler tells schedule function is taking time. i have this code in scheduled function:

    void HelloWorld::scrollScene(float dt) {
        .......

        pos1 = sprSceneback1->getPosition();
     	pos2 = sprSceneback2->getPosition();

	    pos1.x -= scrollSpeed*dt;
	    pos2.x -= scrollSpeed*dt;

        sprSceneback1->setPosition(pos1);
        sprSceneback2->setPosition(pos2);
     }

I have 2 background sprites and they are constantly scrolling.

How can i fix this problem, sorry for my bad english

Have you tried using actions? In your case, MoveTo or MoveBy? You can using the RepeatForever as well. The cpp-tests project has plenty of examples.

1 Like

This is what I was going to say as well. Thanks @R101

1 Like

Getting/setting positions of two sprites shouldn’t be a problem. What is in here: “…”?

I tried but its the same :frowning:

Im doing same thing for 2 other sprites

Then your issue could be related to anything. Have you tried the VS profiling tools to narrow down what may be causing the issue? Are you using sprite sheets or separate image files for each sprite image? It’s really hard to even try to help without more information than what you have provided.

Please first try these 2 things and let me know results.

  1. Android : I hope you are checking with actual android device, not on emulator.
    If so try with diff Android device first.
  2. win32 : Try to run exe directly from proj.win32/Debug.win32 folder.

I tried and still i have flickering issue on all devices. :frowning:

Do you see flickering when you run the cpp-tests demo on your devices? If you do, then perhaps it’s your devices that are the issue. If you don’t see flickering, then it’s quite likely that something is wrong with your code, and you still haven’t provided more details (such as code) in order for anyone to truly help you.

I profiled my code on VS again and it shows scrollScene function takes much time:

I have too much code but im not doing anything unusual, i dont know why this happens. Just initializing sprites, setposition etc… Im going to run cpp tests now

I have flickering issue on cpptests too

Hi ! You can scale width ( or height ) 2 pixel or more ! Please check my codes below !

  • In init function:

m_background01 = Sprite::createWithSpriteFrameName(“background1.png”);
m_background01->setAnchorPoint(Vec2::ZERO);
m_background01->setPosition(m_origin);
this->addChild(m_background01);

m_background02 = Sprite::createWithSpriteFrameName(“background2.png”);
m_background02->setAnchorPoint(Vec2::ZERO);
m_background02->setPosition(m_background01->getPosition() + Vec2(m_background01->getContentSize().width,0.0f));
//m_background02->setScaleX(1.0f + (2.0f / m_background02->getContentSize().width)); if the solution does not works , you can scale m_background02 , 2 pixel or more !
this->addChild(m_background02);

m_background01->runAction(RepeatForever::create(MoveBy::create(0.5f, Vec2(-m_background01->getContentSize().width,0))));
m_background02->runAction(RepeatForever::create(MoveBy::create(0.5f, Vec2(-m_background02->getContentSize().width,0))));

In update function :

if (m_background01->getPosition().x <= -(m_background01->getContentSize().width))
{
m_background01->setPositionX(m_background02->getPositionX() + m_background01->getContentSize().width);
}

if (m_background02->getPosition().x <= -(m_background01->getContentSize().width))
{
m_background02->setPositionX(m_background01->getPositionX()+ m_background01->getContentSize().width);
}

1 Like

Its still lagging :frowning:

What can I do? When i run parallax node demo in cpp tests, it’s lagging too (on both 2 android devices).

What devices are you using? Specifically state the brand and model number, along with the version of Android installed on them.

What resolution are you running your tests at?

Which version of Android NDK?

The issue here is that you’re saying “I have a problem”, but you’re providing very little information, so how is anyone supposed to help? The more information you can provide, the more likely someone may have an idea of what may be going on with your setup.

Aside from that, what exactly do you mean by “it’s lagging”?

If you share your whole test codes or project, it would be helpful to solve the problem.:slightly_smiling_face:

NDK version is r19.
Game resolution: (480, 320)
Im using android studio (windows 10). tried release and debug build. I thought it was a problem with game code but same thing happens with cpptests.

I tested on

Xiaomi Redmi Note7 (Android 9)
and Samsung Note 3 SM-N9000 (Android 5.0)

both phones run my game with lag (i mean flickering, fps is dropping so the game doesnt run fluently).

I tried using cocos2d v4 rc0 but its the same. Whatever i did nothing worked.

Using NDK20, resolution set to 1920x1080 for both a HTC One (Android 6.0) and a Google Nexus 6P (Android 8.1), and the performance is around 60FPS for the majority of cpp-tests that I imagine you’d be running. There is something definitely wrong, most likely with your setup, or what you have installed on those devices, because the issue you’re experiencing makes no sense at all.

Can you try another resolution to test, perhaps even the native resolution of your device? 1920x1080 happens to be the native resolution of the devices I tested.

Didnt work. Can you take a look if i send code and resource files?