Hello,
@Lem and @pabitrapadhy as well as many other users have struggled with swipe gestures on the new 3.6 version. Also i’ve added MoveBy actions to show also how to slide a sprite.
Inspired by the tutorial from @SonarSystems , i have created this basic swipe gesture demo:
Github:
Lets add more gestures to Cocos2D-X and this demo.
Half-circle swipe for example
2 Likes
Pretty cool, thanks for doing this
Instead of
listener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled, this);
.
.
.
void HelloWorld::onTouchCancelled(Touch *touch, Event *event)
{
onTouchEnded(touch, event);
}
can’t we just do
listener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
?
That seems simpler.
@i__d__k Good catch! Thank you. I’ll change that. Seems simpler indeed.
Can you help me with the update() function?
Line 60 the touch sprite:
auto icoTouch = Sprite::create("touchgfx/touch.png");
icoTouch->setPosition(Point(currentTouchPos[0], currentTouchPos[1]));
this->addChild(icoTouch,1);
FadeOut* fadeOut = FadeOut::create(0.5f);
icoTouch->runAction(fadeOut);
How do i delete the created sprite object from scene after it fades out?
This would be really helpful.
I’ve tried icoTouch->release(); and icoTouch->autorelease(); both crash the application.
1 Like
gkapur
July 12, 2015, 8:13pm
4
Just remove it form its parent,
icoTouch->removeFromParent();
But you will need to create an action sequence… You can see here for some guidance: http://www.cocos2d-x.org/wiki/Actions
@gkapur No, it doesn’t work in this context. Not sure about a sequence needed here:
Again, it happens in the update function, so the logic should be create the sprite, fadeOut and when void HelloWorld::onTouchEnded(Touch *touch, Event *event) removeFromParent().
Otherwise the touch graphic will create too many verts overtime.
It needs to be removed after the trail is drawn at the same second by one.
I set a tag to the sprite, still not working:
void HelloWorld::onTouchEnded(Touch *touch, Event *event)
{
isTouchDown = false;
this->getChildByTag(25)->removeFromParentAndCleanup(true);
}
Any ideas? Its seems such a trivial thing.
gkapur
July 12, 2015, 9:19pm
6
It’s further down in that doc file.
auto removeIt = CallFunc::create([&icoTouch]() { icoTouch->removeFromParent() } ),
It should look something like that, I think. Might have to change the lambda expression capture but it should look something like that.
Looking good, check out our Cocos Helper project as it would be great if you wanted to integrate gestures into that.
@gkapur Trying further. This throws a EXC_BAD_ACCESS:
Even if icoTouch is removed:
Maybe there is another way to get them all?
Or really iterate through the node for all icoTouch sprites?
@SonarSystems Thank you! Sure, as soon as time allows it.
What do you think on how to remove the sprite in this case?
@gkapur Thank you very much again. Figured this out now:
auto removeIt = CallFunc::create([icoTouch]() {
icoTouch->removeFromParent();
});
It was the unnescessary & . Works perfectly now! Updating the repo…
EDIT: https://github.com/snlbase/Cocos2D-X/tree/master/Swipe%20Gestures
Great to see you have fixed it.