/*********************************************************************************
关于换动作的bug
在动作实现函数里有这样一句话
SpriteFrameCache* frameCache1 = SpriteFrameCache::getInstance();
这个用完之后清空掉就可以了,否则下次用同样的代码执行别的动作时,SpriteFrameCache会保留以前的图片
frameCache1->removeSpriteFrames();
**********************************************************************************/
在3.x中,我选择Menu做按钮。
menu是菜单,新建一个菜单,然后绑定一个菜单项MenuItemImage就可以了,在MenuItemImage可以设置按钮的按下和普通的两种状态,以及回调函数,值得一提的是,3.x版本支持C++11的拉姆达表达式,也就是说,在回调函数这个参数上,不用再定义一个函数,可以在参数的位置上使用闭包函数。
auto button_jump = MenuItemImage::create("button_jump.png",
"button_jump.png",
[](Ref* obj){log("XXXXXXXXXXXXXX");});
auto menu = Menu::create(button_jump, NULL);
this->addChild(menu);
拉姆达表达式中,[ ]中写&,就可以引入外部变量了,我们在里面写关于跳跃的功能,只需要在拉姆达表达式中加一句。
[&](Ref* obj){player->getPhysicsBody()->setVelocity(Vec2(0,100));})
不getPhysicsBody是不行的,因为setVelocity方法是作用在node上。
设置世界重力
scene->getPhysicsWorld()->setGravity(Vect(0,-2000));
设置精灵不旋转
player->getPhysicsBody()->setRotationEnable(false);
但是当我做滑翔动作的时候我突然发现我去啊menu没有长按事件,只能试试用controButton了
addTargetWithActionForControlEvents,注意后面的s。
新的controlButton的写法如下
ControlButton* button_slip=ControlButton::create(LabelTTF::create(" ","Maker Felt",30),Scale9Sprite::create("button_slip.png"));
button_slip->addTargetWithActionForControlEvents(this,cccontrol_selector(Stage::button_slip_touchDown),Control::EventType::TOUCH_DOWN );
button_slip->setPreferredSize(Size(120,120));
button_slip->setPosition(100,100);
this->addChild(button_slip);
就是事件的第三个参数变了。看源代码就行。
注意回调函数button_slip_touchDown,声明时<span style="background-color: rgb(255, 0, 0);">void button_slip_touchDown(Ref* pSender,Control::EventType event);</span>
暂时不知道addTargetWithActionForControlEvents的拉姆达表达式怎么写
如果你知道,请告之,