Shaking Sprites?

Hi Again!
It is long time ago, i have posted something here…

But i want to know how to make a shake action, or yea just skake my background sprite…

and the source code does not Work for me
(http://www.frozax.com/blog/2012/02/how-to-create-shake-action-cocos2d-x-source-code/)

But please give me a Little tutorial or something like that, so i can do the shaking!:))))))))

Thanks!

Many regards, Nichlas

I implemented a shake and a wiggle in my game. Essentially I do most of the heavy lifting in update().

For wiggle I do a slow back and forth movement by adjusting the rotation.

for shake. I keep changing the position of the sprite, but not to far so it isn’t jumping around.

I then set back to the original pre wiggle and shake values.

Thaaannkkksss very much for ur support!:slight_smile:
But did u get a very nice result, or could something have been better about what u did, with ur shaking method…

And can u give me a little code preview…
So I can see what a 100% mean:-):wink:

void EggplantSprite::update(float dt)
{
    if (bWiggleNode)
    {
        if ((fCounter == 0) || ((int) fCounter % 2 != 0 )) // 0 or odd
        {
            if (rCounter == 0) // first time in only
            {
                this->setRotation(m_initial_rotation + m_amount_rotation);
            }
            else if ( (int) rCounter % 2 == 0 ) // can be divided by 2 so it is even
            {
                this->setRotation(this->getRotation() + (m_amount_rotation * 2));
            }
            else // odd
            {
                this->setRotation(this->getRotation() - (m_amount_rotation * 2));
            }
            
            m_current_rotation = this->getRotation();
            
            ++rCounter;
        }
        
        ++fCounter;
    }
    
    if (bShakeNode)
    {
            float randx = fgRangeRand( -m_strength_x, m_strength_x ) * dt;
            float randy = fgRangeRand( -m_strength_y, m_strength_y ) * dt;
        
            // move the target to a shaked position
            this->setPosition(cocos2d::Vec2(cocos2d::Vec2(m_initial_x, m_initial_y) + cocos2d::Vec2( randx, randy)));
    }
}

I just use a secuence of animations when I have to shake something. For example:

myShakeAnimation = cc.Sequence.create(
	cc.MoveBy.create(0.1, cc.p(10, 0)),
	cc.MoveBy.create(0.1, cc.p(0, -10)),
	cc.MoveBy.create(0.1, cc.p(0, 10)),
	cc.MoveBy.create(0.1, cc.p(-10, 0))
);

This should work just fine and leave the “shaked” object right where it started when the last animation ends, UNLESS another animation is added to the object before it stops shaking (this causes some irregular behaviour).

PS: yes, this is JS code, sorry for that, but the animation principle should still apply, right?

but slackmorhle how shall I use and apply that function to my sprite???

if my sprite is named myBG, then how shall I apply it, and do I need a header file for that or,…pretty confused xD:->

you will need to sub-class cocos2d::Sprite* and override the update() function.

1 Like