Action in the loop while button is pressed

Im wondering if it’s possible to implement action in the loop when my button is pressed

auto button = ui::Button::create(“enemy1.png”);
button->setTitleText(“Button Text”);
button->setPressedActionEnabled(true);
Size winSize = Director::sharedDirector()->getWinSize();
button->setPosition(ccp(winSize.width - (button->getContentSize().width / 2) - 10, button->getContentSize().height / 2 + 10));
button->addTouchEventListener([&](Ref* sender, ui::Widget::TouchEventType type){
switch (type)
{
case ui::Widget::TouchEventType::BEGAN:
herobody->SetLinearVelocity(b2Vec2(1.5, 0));
break;
case ui::Widget::TouchEventType::ENDED:
break;
default:
break;
}
});
this->addChild(button);

I think i can make boolen in (case ui::Widget::TouchEventType::BEGAN:) and add herobody->SetLinearVelocity(b2Vec2(1.5, 0)); in time loop but is there any simpliest way to do that ?

So you want to have a ui::button and when you press it, have an Action run in a loop?

This is easily achieved.

http://cocos2d-x.org/programmersguide/4/index.html

i mean i want to pressed button and my b2body move constantly until i release button, and im not sure if i can use mySprite->runAction(RepeatForever::create(animate)); sth like this for box2d body ;/

have you tried? :slight_smile:

yup ;d there is no function in box2d like runAction ;d and if i want to move my hero by the sprite, b2body doesnt move :frowning: