Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Short Version:

I am trying to move a player around on a tilemap, keeping it centered on its tile, while smoothly controlling it with SneakyInput virtual Joystick. My movement is jumpy and hard to control. What's a good way to implement this?


Long Version:

I'm trying to get a tilemap based RPG "layer" working on top of cocos2d-iphone. I'm using SneakyInput as the input right now, but I've run into a bit of a snag.

Initially, I followed Steffen Itterheim's book and Ray Wenderlich's tutorial, and I got jumpy movement working. My player now moves from tile to tile, without any animation whatsoever.

So, I took it a step further. I changed my player.position to a CCMoveTo action. Combined with CCfollow, my player moves pretty smoothly. Here's the problem, though: Between each CCMoveTo, the movement stops, so there's a bit of a jumpiness introduced between movements.

To deal with that, I changed my CCmoveTo into a CCMoveBy, and instead of running it once, I decided to have it CCRepeatForever. My plan was to stop the repeating action whenever the player changed directions or released the d-pad.

However, when the movement stops, the player is not necessarily centered along the tiles, as it should be. To correctly position the player, I use a CCMoveTo and get the closest position that would put the player back into the proper position. This reintroduces an earlier problem of jumpiness between actions.

What is the correct way to implement a smooth joystick while smoothly animating the player and keeping it on the "grid" of tiles?

Edit:

It turns out that this was caused by a "Bug Fix" in the cocos2d engine.

share|improve this question
    
Please add a tag "sneakyinput". –  Moshe May 29 '11 at 13:26
1  
Sneakyinput is kind of irrelivant to this question. –  AttackingHobo May 29 '11 at 20:15
    
@AttackingHobo Kind of irrelevant? He doesn't mention it a single time in his question. –  The Communist Duck Jun 28 '11 at 20:33
    
I do mention it, read the long version again. –  Moshe Jun 28 '11 at 20:53
    
@Moshe No, no you did not... -1 for making me waste my time. –  AttackingHobo Jun 28 '11 at 21:20
show 2 more comments

1 Answer

up vote 0 down vote accepted

There are three ways to solve this problem :

  1. you can just implement update function for your player class and move it in that update function, this approch will eliminate use of CCMoveBy or CCMoveTo classes and you can easily control how you want the movement be applied, for example just wait until x or y is just in center of tile and then stop moving.
  2. The second approch is to just eliminate CCRepeatForever when you want to stop our player and then add CCMoveBy Directly to the player action list, this way you'll be sure that CCMoveBy will finish what all the movement you requested.
  3. And the last choice is to Implement your code to do what CCRepeatForever does, this is how CCRepeat for ever is implemented in c++, and I also added another if to make sure you have to repeat CCMoveBy just one more time.(sorry for c++ code I hope you can convert it to objective-c)

    void myCCRepeatForever::step(ccTime dt)
    {
        CCMoveByInstance->step(dt);
        if (direction is still the same)
            if (CCMoveByInstance->isDone())
            {
                ccTime diff = dt + CCMoveByInstance->getDuration() - CCMoveByInstance->getElapsed();
                CCMoveByInstance->startWithTarget(m_pTarget);
                CCMoveByInstance->step(diff);
            }
    }
    
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.