1
\$\begingroup\$

I am trying to drag a row of sprites using ccTouchesMoved. By that I mean that there is a row of sprites (they are colored squares) lined up next to each other and if I grab one with a touch the rest follow it. If a sprite moves off screen I want to append it to the rest of the sprites in formation. However, if the sprite formation moves too fast it creates a slight gap between it and the appended sprites. How do I go about limiting the speed that I can drag the sprite with ccTouchesMoved? This is the only solution I could think of to my problem. If anyone has another suggestion to prevent this sprite gap from happening I would appreciate it.

In ccTouchesBegan I loop through the sprites, mark the one that is touched(used for another part of game), and save the distance between the touch point and every other sprite.

touch = (CCTouch*)(touches->anyObject());
location = touch->getLocation();
for (int i = 0; i < grid->getGridSizeY(); i++) {
    for (int j = 0; j < grid->getGridSizeX(); j++) {
        button = grid->button(i, j);
        button->setDistanceX(location.x-button->getPositionX());
        button->setDistanceY(location.y-button->getPositionY());
        if (button->boundingBox().containsPoint(location)) {
            button->setTouched(true);
        }
    }
}

Then in ccTouchesMoved I loop through all the sprites again and set them to always be the same saved distance from the touch point.

touch = (CCTouch*)(touches->anyObject());
location = touch->getLocation();
for (int i = 0; i < grid->getGridSizeY(); i++) {
    for (int j = 0; j < grid->getGridSizeX(); j++) {
        button = grid->button(i, j);
        button->setPositionX(touchLocation.x - button->getDistanceX());
        button->setPositionY(touchLocation.y - button->getDistanceY());
        }
    }
}

This is the update method code only for a sprite moving off the left side of the screen. No point in writing all the sides until I get one side to work:

for (int i = 0; i < grid->getGridSizeY(); i++) {
    for (int j = 0; j < grid->getGridSizeX(); j++) {
        button = grid->button(i, j);
        //Replaces button if it goes off left side of grid
        if (button->getPositionX() < grid->button(0, 0)->getPositionX() - button->boundingBox().size.height / 2) {
            button->setPositionX(grid->button(grid->getGridSizeY() - 1, 0)->getCoord().x + button->boundingBox().size.height / 2);
            button->setDistanceX(location.x-button->getPositionX());
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ how do you move the sprites? post the code. Sounds like a 1-frame delay between moving the dragged sprite and updating the other sprite's positions. Make sure that happens in the same frame. It sounds as if you set the position of the dragged sprite, but you're using move actions for the others, which will cause a 1-frame delay. \$\endgroup\$ – LearnCocos2D Nov 2 '13 at 10:42
  • \$\begingroup\$ I edited the question and added the code. I realized I missed explaining part of the problem properly. I'm appears the frame delay happens when I append a sprite to the rest of the sprites, but I don't know how I would do it in the same frame. \$\endgroup\$ – Frozsht Nov 2 '13 at 17:09
  • \$\begingroup\$ Hmmm no move actions, good. I suppose this may be the common cocos2d "black line" gap issue. Try casting all sprite positions to (int) while dragging, usually these small lines between tiled sprites only appear when the sprites are not placed at exact pixel locations. \$\endgroup\$ – LearnCocos2D Nov 2 '13 at 23:21
  • \$\begingroup\$ I still get a problem after casting to int. Btw, the faster the sprite moves the larger the gap is. It can be several pixels wide. \$\endgroup\$ – Frozsht Nov 4 '13 at 19:20
1
\$\begingroup\$

It turns out the issue was being caused by

button->setDistanceX(location.x-button->getPositionX());

Since the distance was set regarding the touch location instead of the position of the sprite itself, if the touchlocation moved too fast it caused a gap depending on how fast it was moving, since the touchlocation updates before the sprite position. All I had to do was change the distance to be set relative to the sprite position instead of the touch position and it works flawlessly now.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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