直接调用的话, void HelloWorld::update(float delta); 方法是可以正常回调的.
this->scheduleUpdate();
bool HelloWorld::init() {
// 1. super init first
if ( !Layer::init() ) {
return false;
}
this->testPhysics();
// this->scheduleUpdate();
return true;
}
// 添加了 3个 button 2个精灵
void HelloWorld::testPhysics() {
// left btn
auto btn_left = cocos2d::ui::Button::create();
btn_left->setPosition(Vec2(50, 250));
btn_left->setTitleText("Left");
btn_left->setTitleFontSize(20);
btn_left->addTouchEventListener(CC_CALLBACK_2(HelloWorld::buttonTouchLeftEvent, this));
this->addChild(btn_left);
// right btn
auto btn_right = cocos2d::ui::Button::create();
btn_right->setPosition(Vec2(50, 200));
btn_right->setTitleText("Right");
btn_right->setTitleFontSize(20);
btn_right->addTouchEventListener(CC_CALLBACK_2(HelloWorld::buttonTouchRightEvent, this));
this->addChild(btn_right);
// jump btn
auto btn_jump = cocos2d::ui::Button::create();
btn_jump->setPosition(Vec2(350, 250));
btn_jump->setTitleText("Jump");
btn_jump->setTitleFontSize(20);
btn_jump->addTouchEventListener(CC_CALLBACK_2(HelloWorld::buttonTouchJumpEvent, this));
this->addChild(btn_jump);
// floor
auto physicsBody_floor = PhysicsBody::createBox(Size(300.0f, 50.0f));
physicsBody_floor->setDynamic(false);
physicsBody_floor->getShape(0)->setRestitution(0.0f);
auto floor = Sprite::create("floor.png", Rect(0, 0, 300, 50));
floor->setPhysicsBody(physicsBody_floor);
floor->setPosition(Vec2(150, 25));
this->addChild(floor);
// box
auto physicsBody_player = PhysicsBody::createBox(Size(30.0f, 30.0f));
physicsBody_player->setDynamic(true);
// physicsBody_player->setGravityEnable(false);
physicsBody_player->getShape(0)->setRestitution(0.0f);
this->player = Sprite::create("line.png", Rect(0, 0, 30, 30));
player->setPosition(30, 165);
player->setPhysicsBody(physicsBody_player);
this->addChild(player);
}
// left right jump 按钮响应方法
#pragma mark - Touch Event
void HelloWorld::buttonTouchLeftEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type) {
if (type == cocos2d::ui::Widget::TouchEventType::BEGAN) {
schedule(schedule_selector(HelloWorld::moveLeft), 0.1, kRepeatForever, 0.0);
}else {
unschedule(schedule_selector(HelloWorld::moveLeft));
}
}
void HelloWorld::buttonTouchRightEvent(Ref *pSender, cocos2d::ui::Widget::TouchEventType type) {
if (type == cocos2d::ui::Widget::TouchEventType::BEGAN) {
schedule(schedule_selector(HelloWorld::moveRight), 0.1, kRepeatForever, 0.0);
}else {
unschedule(schedule_selector(HelloWorld::moveRight));
}
}
void HelloWorld::buttonTouchJumpEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type) {
if (type == cocos2d::ui::Widget::TouchEventType::BEGAN) {
Action *jump = player->getActionByTag(1000);
if (!jump || jump->isDone()) {
Vec2 position = player->getPosition();
ActionInterval *jump = JumpBy::create(1, Vec2(0, 0), 60, 1);
jump->setTag(1000);
player->runAction(jump);
};
}
}
// 调度器方法
#pragma mark -
static float speed = 5.0f;
void HelloWorld::moveLeft(float dt) {
ActionInterval *move = MoveBy::create(0.1, Vec2(-speed, 0));
player->runAction(move);
}
void HelloWorld::moveRight(float dt) {
ActionInterval *move = MoveBy::create(0.1, Vec2(speed, 0));
player->runAction(move);
}