3.2 alpha - Flickering Sprites on Android (Images show and hide randomly)

On touch began, i am removing the sprite touched and adding another one in its place. this results in the other sprites,that are already visible on the screen, to become invisible. When I tap another sprite on the screen, then some of the previous invisible ones become visible and the ones that were visible become invisible. This happens in android emulator and on a device too (Samsung Galaxy 2). I am using 3.2 alpha 0 version of cocos2dx. Is there any fix for this from the development team of cocos2dx?

Can you post your code from onTouchBegan?

On touch began, I detect which sprite was touched and then this function is called:

_type = newType;
    std::string frameName;
    switch (_type) {
            break;
        case GREEN_TILE:
            frameName = "green.png";
            break;
        case RED_TILE:
            frameName = "red.png";
            break;
        default:
            break;
    }
    Vec2 oldPos = _sprite->getPosition();
    _sprite->removeFromParentAndCleanup(true);
    _sprite = Sprite::create(frameName);
    _sprite->setPosition(oldPos);
    _sprite->setRotation(_angle * 90.0f);
    GameLayer::getInstance()->addChild(_sprite,1);

I would recommend adding the new sprite to the layer before removing the old one. That way you ensure there is always one sprite showing at that location. Since you remove the sprite and then add the new one, there’s the potential for a pause between the two lines of code that could look like a flicker in the sprite.

I think i explained wrong. By a flicker i meant it completely disappears. Then on the next touch it appears again, even though it was suppose to be visible before I touched the screen. See - I have 10 sprites on the screen. 1 touch one - this one was suppose to change and get re-rendered onto the screen. Now what happens is out of those 10 on the screen 5 become invisible and the remaining 5 are still visible. What is suppose to happen is that all are visible on the screen. The second time I touch a sprite, 4 out of the 5 invisible ones become visible and the 3 out of the previously visible sprites become invisible. I guess it sounds confusing but I don’t know how else to explain it.

Ok, so maybe your code that detects which sprite is touched has some issues. Are you doing something like this in onTouchBegan:

  1. loop through this->getChildren()
  2. test if touch is within child
  3. if yes, call function that swaps red to green or vice versa.

?

If so, you could put a CCLOG statement in that function you call to see how many times it’s called with every touch. If it’s called multiple times, then the touch logic needs to be inspected.

The function is called only once. Its not a for - loop that I use. The sprites are positioned as a 2D array on the screen and the touch location is checked for which Row and column is intersected. This row & column position gives me the location of the sprite in the 2D array that i use for storing them. The the function to swap is called on this sprite. I take this approach as it is less expensive than iterating over a for loop and then exiting the loop when the function is found.

Wow… ok. Hmmm. Is there a pattern to which sprites become invisible when one is touched? i.e. is it deterministic?

I doubt it…i cant seem to see a pattern in it. Is this issue only happening to me? Has no one else faced this issue?

Ok. Well, it’s difficult to debug with out seeing some more code and the actual output… but I have an interesting experiment for you to try.

Currently, you have each sprite in a separate PNG. If you put them together in a sprite sheet, instead of removing and adding sprites all the time, you could just change the textureRect to the appropriate sprite.

It would be interesting to see what getting rid of the “removeFromParent” call would do.

Hi @toojuice,
sorry for the delay. Got side-tracked by some other work. I fixed it by adding z-ordering to each sprite that is there on the screen. It works fine after that. My concern is will i have to always do this?

Thanks,
Nidhi

Do you mean you no longer remove and add sprites? Are you adding all the sprites initially and then just changing the z-ordering to hide one behind another? If that’s the case, instead of changing z-ordering, you can use the setVisible(bool bVisible) method to hide and show the squares.