Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation reset color and opacity of Sprite #15696

Closed
noprops opened this issue May 22, 2016 · 2 comments
Closed

Animation reset color and opacity of Sprite #15696

noprops opened this issue May 22, 2016 · 2 comments
Milestone

Comments

@noprops
Copy link
Contributor

noprops commented May 22, 2016

When I change color or opacity of Sprite and run Animate, color and opacity will be back to default in every setSpriteFrame.
This happens only if TexturePacker algorithm is polygon.

http://discuss.cocos2d-x.org/t/animation-reset-color-and-opacity-of-sprite/29452

@felippeduran
Copy link
Contributor

felippeduran commented Jul 29, 2016

Any progress in this issue? I have the same problem here (cocos2dx 3.12). Besides, it seems that Animate/Animation resets flippedX and flippedY properties too (or just ignores them).

@felippeduran
Copy link
Contributor

felippeduran commented Jul 29, 2016

I've found that this issue is related to setSpriteFrame method in CCSprite and seems to be indeed a bug:

void Sprite::setSpriteFrame(SpriteFrame *spriteFrame)
{
    // retain the sprite frame
    // do not removed by SpriteFrameCache::removeUnusedSpriteFrames
    if (_spriteFrame != spriteFrame)
    {
        CC_SAFE_RELEASE(_spriteFrame);
        _spriteFrame = spriteFrame;
        spriteFrame->retain();
    }
    _unflippedOffsetPositionFromCenter = spriteFrame->getOffset();

    Texture2D *texture = spriteFrame->getTexture();
    // update texture before updating texture rect
    if (texture != _texture)
    {
        setTexture(texture);
    }

    // update rect
    _rectRotated = spriteFrame->isRotated();
    setTextureRect(spriteFrame->getRect(), _rectRotated, spriteFrame->getOriginalSize());

    if(spriteFrame->hasPolygonInfo())
    {
        _polyInfo = spriteFrame->getPolygonInfo();
    }
    if (spriteFrame->hasAnchorPoint())
    {
        setAnchorPoint(spriteFrame->getAnchorPoint());
    }
}

Every frame, _polyInfo is reset and flipped vertices and color info are lost.

I've managed to fix the issue by making the following changes to setSpriteFrame method:

    if(spriteFrame->hasPolygonInfo())
    {
        _polyInfo = spriteFrame->getPolygonInfo();
        if (_flippedX) flipX();
        if (_flippedY) flipY();
        updateColor();
    }

And refactored setFlippedX and setFlippedY to the following:

void Sprite::setFlippedX(bool flippedX)
{
    if (_flippedX != flippedX)
    {
        _flippedX = flippedX;
        flipX();
    }
}

void Sprite::setFlippedY(bool flippedY)
{
    if (_flippedY != flippedY)
    {
        _flippedY = flippedY;
        flipY();
    }
}

void Sprite::flipX() {
    for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
        auto& v = _polyInfo.triangles.verts[i].vertices;
        v.x = _contentSize.width -v.x;
    }
    if (_textureAtlas) {
        setDirty(true);
    }
}

void Sprite::flipY() {
    for (ssize_t i = 0; i < _polyInfo.triangles.vertCount; i++) {
        auto& v = _polyInfo.triangles.verts[i].vertices;
        v.y = _contentSize.height -v.y;
    }
    if (_textureAtlas) {
        setDirty(true);
    }
}

Hope this issue gets officially fixed in an upcoming release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants