Issue with Update() function

Hello,

I want to move a sprite in the update fonction. I do like this :

void HelloWorld::update(float delta){

if ((sprite->getPosition().x >= 0) && (sprite->getPosition().x <= 300))
{
sprite->setPositionX(sprite->getPosition().x + 100 * delta);
}
}

The issue is when the condition is finish, my sprite positionX have random position, like 301 or 302.

Do you know how i can do for in the end of thecondition, my sprite positionX have always for value 300.

Do you know what technique use the games developers ?

In advance thanks,

1 Like

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.

1 Like

Thanks a lot for your answer,

Yes like something like that maybe:

void HelloWorld::update(float delta){

if ((sprite->getPosition().x >= 0) && (sprite->getPosition().x < 300))
{
sprite->setPositionX(sprite->getPosition().x + 100 * delta);
}

if (sprite->getPosition().x > 300)
{
sprite->setPositionX(300);
}

}

Yes with the Action too, MoveTo and MoveBy. Its a good way to do this. But in my case, look like more easy to use the Update method.

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.

1 Like

Good points.

One golden rule I live by is never, ever use a number on code. Really. never.

OK - so when I am R&Ding there might be the occasional number, like a ‘1’ for a loop counter.

But everything else should be either a constant, variable or enumeration.

then, when something changes, you change it in one place and do’t have to search your code for ‘magic’ numbers.

Your points regarding animation vs update method don’t really hod too much water - I think it’s a preference thing.

Use the sprite’s update() method rather than the Layer’s.

Using constants/variables sorts that issue - no different than if you hard-coded values in an animation.

bool runAnimation = false;

if (runAnimation){}

I tend to find the flexibility of ‘doing it yourself’ can outweigh the convenience of things like actions in all but the most simple of cases.

But I get that its a personal thing - I’m just a control freak!

3 Likes

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.

2 Likes

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.

1 Like

what is the difference in using the sprites update() method rather than the layers ?