How to slow down CCMoveTo?

I have a CCMoveTo action on a sprite that moves it from one point to another. When the user presses the button, the sprite should easily slow down and continue to move to the target location at a new speed. I do not know how to do that.

Update In fact, I replaced CCMoveTo with CCMoveBy , but the question is the same.

+4
source share
5 answers

With the current implementation of CCEaseIn/CCEaseOut you can only reduce the speed of actions from zero to zero. This means that if you lighten CCMoveBy/CCMoveTo , they will facilitate the speed of movement from / to the stop.

However, starting with cocos2d 2.1, CCMoveBy/CCMoveTo can be stacked . With this function, you can implement a workaround that will lead to the desired effect.

Setting up and simultaneously performing two CCMoveBy actions for the sprite: actionA will have a slower speed of movement, which you will get after clicking the button. actionB will have a speed corresponding to the difference between a faster speed and a lower speed.

Then, when the user clicks the button, you can CCEeaseOut actionB (stop CCMoveBy and then run it again using the desired CCEaseOut ). It will look like a sprite reduces the movement speed of actionA + actionB to the speed of actionA .


Despite this explanation, if you implement game controls that you want to fine-tune, it would be better to avoid CCActions and simply update the sprite's position in the frame by implementing a custom motion code.

+3
source

You can use the EaseIn / Out action to get this effect:

 id action = [CCMoveTo actionWithDuration:2 position:ccp(100,100)]; id ease = [CCEaseIn actionWithAction:action rate:2]; [sprite runAction: ease]; 

Taken from here

You can find various types of mitigation according to your needs.

+2
source

To dynamically change the CCMoveBy / CCMoveTo speed, there is a CCSpeed ​​action that you can implement.

First, when adding a CCMoveBy action, wrap it in the CCSpeed ​​action as follows:

 CCSpeed *action = [CCSpeed actionWithAction:[CCMoveBy actionWithDuration:myDuration position:ccp(xPos,yPos)] speed:1]; action.tag = 42; [mySprite runAction:action]; 

Then, when you want to change the speed, get the CCSpeed ​​action and change it:

 CCSpeed *action = (CCSpeed*)[mySprite getActionByTag:42]; [action setSpeed:6]; 
+2
source
 CGPoint startPoint = ccp(10,100); CGPoint endPoint = ccp(300,100); float fastTime = 5.f; CCSprite *sp = [CCSprite spriteWithFile:@"sprite.png"]; sp.position = ccp(10,100); [self addChild:sp]; [sp runAction:[CCMoveTo actionWithDuration:fastTime position:endPoint]]; //on hit float slowTime = 10.f; [sp stopAllActions]; float newSlowTime = ccpDistance(sp.position, endPoint)*slowTime/ccpDistance(startPoint, endPoint); [sp runAction:[CCMoveTo actionWithDuration:newSlowTime position:endPoint]]; 
0
source

you can replace the ccmoveto ccactionmoveto action like:

  CCActionMoveTo *mAction = [CCActionMoveTo actionWithDuration:0.2f position:_selectedSpritePos]; [_selectedSprite stopAllActions]; [_selectedSprite runAction:mAction]; 
0
source

Source: https://habr.com/ru/post/1495819/


All Articles