Your if statement sets the spriteâs x position whenever this x value is less than or equal to 300, therefore when it is equal to 300 setPositionX(float x) will still be called, moving the sprite further (over 300). To make sure that the final spriteâs x position is exactly 300 you also need to add another if in your update(float delta) method which directly sets the spriteâs x position to 300 when delta is 1 (animation done).
Note: if you donât have a specific reason for moving the sprite whenever update(float delta) is called in HelloWorld I strongly suggest you check out this documentation page about actions, which are the most common way to do animations in cocos2d-x (basically actions handle performing updates of specific parameters for the nodes you specify over a finite amount of time). Especially, check out the MoveBy and MoveTo classes.
This will work, however I would check for delta to be 1 (1.0f given that it is a float), not for the position to be greater than 300. The reason is that if you start with an x position already greater than 300 setPosition(float x) will still be called many times, and that would be useless. On the other end delta is 1 only twice (I do not remember why), therefore setPosition(float x) will be called only twice.
Let me challenge you a bit, because it might turn out to be helpful for you:
what if in the future you need to move other sprites, maybe in multiple directions?
what if in the future you need to change the amount of movement of the sprite?
what if in the future you need to stop the animation before it has ended?
what if in the future you need to check whether the animation is still running?
I find âdynamicâ codes to be better, as they can be more easily adapted to future needs. This because I used to write really âstaticâ codes, and ended up having to rewrite everything every time some changes where needed, and it was a big waste of time. For example, right now the snippet you posted is pretty static, as you would have to rewrite it fully in order to meet one or more of the possible requirements in the list. Letâs assume you want to move the sprite by 500 instead of 300. Right now you have to replace each â300â with a â500â. To make the code more dynamic you could for example use a variable which defines how much the sprite has to move. A variable would also prevent possible incongruences in your code, which means less troubleshooting and therefore a greater peace of mind.
I know that right now it might seem excessive to think about future needs. In fact, it is generally impossible to precisely know what we will need in the future, and even if we do, things might change. Still, needed changes might arise, and if the code is dynamic you will generally be able to implement them more easily.
Also, I find dynamic codes to be more readable, but this is more of a personal thing.
I am more on the side of âdo it yourself only if nobody has already done exactly what you need and made it available for others, or if no existing possibility works well enoughâ. It saves a hell of a lot of time and energy in the long run. For example in this case it is true that the questions I listed can be answered (as you have shown), but it would have been a better investment to simply take advantage of the existing actions classes and use the saved time and energy to work on something else. This is for when one just wants to get the job done though. If one enjoys programming and wants to write code, no matter if it already exists, he/she is obviously free to do it.
Youâre right I think, to some extent. Certainly no use re-inventing the wheel.
However, I have spent far too long on far too many frameworks trying to work-around limitations and figure out bugs or inconsistencies to use with any confidence some of the frameworksâ functionality.
As a really simple example, my character walks from left to right. the tempatation to create an action to animate him through the series of 6 or 7 images while moving the sprite from left to right with another action is quite strong, so I might choose to do so.
Now, I want to move him at a different speed depending on some criteria (say I press the ârunâ button). So I need to maybe change the animation or maybe speed it up - and I need to do this in relation to the current speed which is dependent on all sorts of things (terrain friction, slope whatever.)
I am sure it can be done with various actions, starting and stopping and raising events etc.
But it is certainly a learning curve - and with little documentation one is relying on the forums or trial and error to help you.
Rolling my own has proved very simple and flexible - and the problem with
is that I am not one to spec my game first - I think of a new idea and implement it - so I donât know if it will work well enough unless I write it myself - in which case I will write it (or refactor it) to do so.
Well, exactly. If one didnât I donât think one would be using cocos2d-x, one would probably go a different route?
Itâs all down to personal preference, though, in the end.