more samples using node::schedule(labmda)

This commit is contained in:
Ricardo Quesada 2014-10-03 17:11:39 -07:00
parent 3731343260
commit 84da993677
7 changed files with 82 additions and 100 deletions

View File

@ -7,8 +7,6 @@
#define IDC_PAUSE 200
IntervalLayer::IntervalLayer()
{
_time0 = _time1 = _time2 = _time3 = _time4 = 0.0f;
@ -31,10 +29,37 @@ IntervalLayer::IntervalLayer()
_label4 = Label::createWithBMFont("fonts/bitmapFontTest4.fnt", "0");
scheduleUpdate();
schedule(CC_SCHEDULE_SELECTOR(IntervalLayer::step1));
schedule(CC_SCHEDULE_SELECTOR(IntervalLayer::step2), 0);
schedule(CC_SCHEDULE_SELECTOR(IntervalLayer::step3), 1.0f);
schedule(CC_SCHEDULE_SELECTOR(IntervalLayer::step4), 2.0f);
schedule([&](float dt){
_time1 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time1);
_label1->setString( str );
}, "step_1");
schedule([&](float dt){
_time2 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time2);
_label2->setString( str );
}, 0.5, "step_2");
schedule([&](float dt){
_time3 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time3);
_label3->setString( str );
}, 1, "step_3");
schedule([&](float dt){
_time4 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time4);
_label4->setString( str );
}, 2, "step_4");
_label0->setPosition(s.width*1/6, s.height/2);
_label1->setPosition(s.width*2/6, s.height/2);
@ -85,42 +110,6 @@ void IntervalLayer::update(float dt)
_label0->setString(time);
}
void IntervalLayer::step1(float dt)
{
_time1 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time1);
_label1->setString( str );
}
void IntervalLayer::step2(float dt)
{
_time2 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time2);
_label2->setString( str );
}
void IntervalLayer::step3(float dt)
{
_time3 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time3);
_label3->setString( str );
}
void IntervalLayer::step4(float dt)
{
_time4 +=dt;
char str[10] = {0};
sprintf(str, "%2.1f", _time4);
_label4->setString( str );
}
void IntervalTestScene::runThisTest()
{
auto layer = new (std::nothrow) IntervalLayer();

View File

@ -5,27 +5,22 @@
class IntervalLayer : public Layer
{
public:
IntervalLayer(void);
virtual ~IntervalLayer();
virtual void update(float dt) override;
//CREATE_NODE(IntervalLayer);
protected:
Label* _label0;
Label* _label1;
Label* _label2;
Label* _label3;
Label* _label4;
float _time0, _time1, _time2, _time3, _time4;
public:
IntervalLayer(void);
virtual ~IntervalLayer();
public:
void step1(float dt);
void step2(float dt);
void step3(float dt);
void step4(float dt);
void update(float dt);
//CREATE_NODE(IntervalLayer);
};
class IntervalTestScene : public TestScene

View File

@ -1,7 +1,6 @@
#include "LabelTest.h"
#include "../testResource.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCCustomCommand.h"
#include "cocos2d.h"
#if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
@ -257,8 +256,7 @@ LabelAtlasTest::LabelAtlasTest()
label2->setPosition( Vec2(10,200) );
label2->setOpacity( 32 );
schedule(CC_SCHEDULE_SELECTOR(LabelAtlasTest::step));
schedule(CC_CALLBACK_1(LabelAtlasTest::step, this), "step_key");
}
void LabelAtlasTest::step(float dt)
@ -313,7 +311,7 @@ LabelAtlasColorTest::LabelAtlasColorTest()
_time = 0;
schedule( CC_SCHEDULE_SELECTOR(LabelAtlasColorTest::step) ); //:@selector(step:)];
schedule(CC_CALLBACK_1(LabelAtlasColorTest::step, this), "step_key");
}
void LabelAtlasColorTest::actionFinishCallback()
@ -438,7 +436,7 @@ Atlas3::Atlas3()
label2->setPosition( VisibleRect::center() );
label3->setPosition( VisibleRect::rightTop() );
schedule( CC_SCHEDULE_SELECTOR(Atlas3::step) );//:@selector(step:)];
schedule(CC_CALLBACK_1(Atlas3::step, this), "step_key");
}
void Atlas3::step(float dt)
@ -534,7 +532,7 @@ Atlas4::Atlas4()
auto lastChar = (Sprite*) label2->getChildByTag(3);
lastChar->runAction( rot_4ever->clone() );
schedule( CC_SCHEDULE_SELECTOR(Atlas4::step), 0.1f);
schedule(CC_CALLBACK_1(Atlas4::step, this), 0.1f, "step_key");
}
void Atlas4::step(float dt)
@ -799,7 +797,7 @@ LabelsEmpty::LabelsEmpty()
addChild(label3, 0, kTagBitmapAtlas3);
label3->setPosition(Vec2(s.width/2, 0+100));
schedule(CC_SCHEDULE_SELECTOR(LabelsEmpty::updateStrings), 1.0f);
schedule(CC_CALLBACK_1(LabelsEmpty::updateStrings, this), 1.0f, "update_strings_key");
setEmpty = false;
}

View File

@ -231,7 +231,7 @@ LabelFNTColorAndOpacity::LabelFNTColorAndOpacity()
label2->setPosition( VisibleRect::center() );
label3->setPosition( VisibleRect::rightTop() );
schedule( CC_SCHEDULE_SELECTOR(LabelFNTColorAndOpacity::step) );//:@selector(step:)];
schedule(CC_CALLBACK_1(LabelFNTColorAndOpacity::step, this), "step_key");
}
void LabelFNTColorAndOpacity::step(float dt)
@ -313,7 +313,7 @@ LabelFNTSpriteActions::LabelFNTSpriteActions()
auto lastChar = (Sprite*) label2->getLetter(3);
lastChar->runAction( rot_4ever->clone() );
schedule( CC_SCHEDULE_SELECTOR(LabelFNTSpriteActions::step), 0.1f);
schedule(CC_CALLBACK_1(LabelFNTSpriteActions::step, this), 0.1f, "step_key");
}
void LabelFNTSpriteActions::step(float dt)
@ -504,7 +504,7 @@ LabelFNTandTTFEmpty::LabelFNTandTTFEmpty()
addChild(label3, 0, kTagBitmapAtlas3);
label3->setPosition(Vec2(s.width/2, 100));
schedule(CC_SCHEDULE_SELECTOR(LabelFNTandTTFEmpty::updateStrings), 1.0f);
schedule(CC_CALLBACK_1(LabelFNTandTTFEmpty::updateStrings, this), 1.0f, "update_strings_key");
setEmpty = false;
}
@ -1356,7 +1356,7 @@ LabelCharMapTest::LabelCharMapTest()
label2->setPosition( Vec2(10,200) );
label2->setOpacity( 32 );
schedule(CC_SCHEDULE_SELECTOR(LabelCharMapTest::step));
schedule(CC_CALLBACK_1(LabelCharMapTest::step, this), "step_key");
}
void LabelCharMapTest::step(float dt)
@ -1411,7 +1411,7 @@ LabelCharMapColorTest::LabelCharMapColorTest()
_time = 0;
schedule( CC_SCHEDULE_SELECTOR(LabelCharMapColorTest::step) ); //:@selector(step:)];
schedule(CC_CALLBACK_1(LabelCharMapColorTest::step, this), "step_key");
}
void LabelCharMapColorTest::actionFinishCallback()

View File

@ -474,7 +474,7 @@ bool AudioControlTest::init()
timeLabel->setPosition(timeSliderPos.x - sliderSize.width / 2, timeSliderPos.y);
addChild(timeLabel);
this->schedule(CC_SCHEDULE_SELECTOR(AudioControlTest::update), 0.1f);
this->schedule(CC_CALLBACK_1(AudioControlTest::update, this), 0.1f, "update_key");
return ret;
}
@ -627,7 +627,7 @@ bool AudioProfileTest::init()
addChild(timeSlider);
_timeSlider = timeSlider;
this->schedule(CC_SCHEDULE_SELECTOR(AudioControlTest::update), 0.05f);
this->schedule(CC_CALLBACK_1(AudioProfileTest::update, this), 0.05f, "update_key");
return ret;
}

View File

@ -222,8 +222,8 @@ Test4::Test4()
addChild(sp1, 0, 2);
addChild(sp2, 0, 3);
schedule( CC_SCHEDULE_SELECTOR(Test4::delay2), 2.0f);
schedule( CC_SCHEDULE_SELECTOR(Test4::delay4), 4.0f);
schedule(CC_CALLBACK_1(Test4::delay2, this), 2.0f, "delay2_key");
schedule(CC_CALLBACK_1(Test4::delay4, this), 4.0f, "delay4_key");
}
void Test4::delay2(float dt)
@ -235,7 +235,7 @@ void Test4::delay2(float dt)
void Test4::delay4(float dt)
{
unschedule(CC_SCHEDULE_SELECTOR(Test4::delay4));
unschedule("delay4_key");
removeChildByTag(3, false);
}
@ -271,7 +271,7 @@ Test5::Test5()
sp1->runAction(forever);
sp2->runAction(forever2);
schedule( CC_SCHEDULE_SELECTOR(Test5::addAndRemove), 2.0f);
schedule(CC_CALLBACK_1(Test5::addAndRemove, this), 2.0f, "add_and_remove_key");
}
void Test5::addAndRemove(float dt)
@ -331,7 +331,7 @@ Test6::Test6()
sp2->runAction(forever2);
sp21->runAction(forever21);
schedule( CC_SCHEDULE_SELECTOR(Test6::addAndRemove), 2.0f);
schedule(CC_CALLBACK_1(Test6::addAndRemove, this), 2.0f, "add_and_remove_key");
}
void Test6::addAndRemove(float dt)
@ -373,12 +373,12 @@ StressTest1::StressTest1()
sp1->setPosition( Vec2(s.width/2, s.height/2) );
schedule( CC_SCHEDULE_SELECTOR(StressTest1::shouldNotCrash), 1.0f);
schedule(CC_CALLBACK_1(StressTest1::shouldNotCrash, this), 1.0f, "should_not_crash_key");
}
void StressTest1::shouldNotCrash(float dt)
{
unschedule(CC_SCHEDULE_SELECTOR(StressTest1::shouldNotCrash));
unschedule("should_not_crash_key");
auto s = Director::getInstance()->getWinSize();
@ -442,14 +442,14 @@ StressTest2::StressTest2()
fire->runAction( RepeatForever::create(copy_seq3) );
sublayer->addChild(fire, 2);
schedule(CC_SCHEDULE_SELECTOR(StressTest2::shouldNotLeak), 6.0f);
schedule(CC_CALLBACK_1(StressTest2::shouldNotLeak,this), 6.0f, "should_not_leak_key");
addChild(sublayer, 0, kTagSprite1);
}
void StressTest2::shouldNotLeak(float dt)
{
unschedule( CC_SCHEDULE_SELECTOR(StressTest2::shouldNotLeak) );
unschedule("should_not_leak_key");
auto sublayer = static_cast<Layer*>( getChildByTag(kTagSprite1) );
sublayer->removeAllChildrenWithCleanup(true);
}
@ -473,10 +473,10 @@ SchedulerTest1::SchedulerTest1()
addChild(layer, 0);
//CCLOG("retain count after addChild is %d", layer->getReferenceCount()); // 2
layer->schedule( CC_SCHEDULE_SELECTOR(SchedulerTest1::doSomething) );
layer->schedule(CC_CALLBACK_1(SchedulerTest1::doSomething, this), "do_something_key");
//CCLOG("retain count after schedule is %d", layer->getReferenceCount()); // 3 : (object-c viersion), but win32 version is still 2, because Timer class don't save target.
layer->unschedule(CC_SCHEDULE_SELECTOR(SchedulerTest1::doSomething));
layer->unschedule("do_something_key");
//CCLOG("retain count after unschedule is %d", layer->getReferenceCount()); // STILL 3! (win32 is '2')
}
@ -1326,7 +1326,7 @@ void NodeNameTest::onEnter()
{
TestCocosNodeDemo::BaseTest::onEnter();
this->scheduleOnce(CC_SCHEDULE_SELECTOR(NodeNameTest::test),0.05f);
this->scheduleOnce(CC_CALLBACK_1(NodeNameTest::test, this), 0.05f, "test_key");
}
void NodeNameTest::test(float dt)

View File

@ -420,7 +420,7 @@ SpriteColorOpacity::SpriteColorOpacity()
addChild(sprite7, 0, kTagSprite7);
addChild(sprite8, 0, kTagSprite8);
schedule( CC_SCHEDULE_SELECTOR(SpriteColorOpacity::removeAndAddSprite), 2 );
schedule( CC_CALLBACK_1(SpriteColorOpacity::removeAndAddSprite, this), 2, "remove_add_key" );
}
// this function test if remove and add works as expected:
@ -513,7 +513,7 @@ SpriteBatchNodeColorOpacity::SpriteBatchNodeColorOpacity()
batch->addChild(sprite8, 0, kTagSprite8);
schedule( CC_SCHEDULE_SELECTOR(SpriteBatchNodeColorOpacity::removeAndAddSprite), 2);
schedule( CC_CALLBACK_1(SpriteBatchNodeColorOpacity::removeAndAddSprite, this), 2, "remove_add_key");
}
// this function test if remove and add works as expected:
@ -575,7 +575,7 @@ SpriteZOrder::SpriteZOrder()
sprite->setScaleX( 6 );
sprite->setColor(Color3B::RED);
schedule( CC_SCHEDULE_SELECTOR(SpriteZOrder::reorderSprite), 1);
schedule( CC_CALLBACK_1(SpriteZOrder::reorderSprite, this), 1, "reorder_key");
}
void SpriteZOrder::reorderSprite(float dt)
@ -643,7 +643,7 @@ SpriteBatchNodeZOrder::SpriteBatchNodeZOrder()
sprite->setScaleX( 6 );
sprite->setColor(Color3B::RED);
schedule( CC_SCHEDULE_SELECTOR(SpriteBatchNodeZOrder::reorderSprite), 1);
schedule( CC_CALLBACK_1(SpriteBatchNodeZOrder::reorderSprite, this), 1, "reorder_key");
}
void SpriteBatchNodeZOrder::reorderSprite(float dt)
@ -789,7 +789,7 @@ Sprite* SpriteBatchNodeReorderIssue766::makeSpriteZ(int aZ)
void SpriteBatchNodeReorderIssue766::reorderSprite(float dt)
{
unschedule(CC_SCHEDULE_SELECTOR(SpriteBatchNodeReorderIssue766::reorderSprite));
unschedule("issue_766_key");
batchNode->reorderChild(sprite1, 4);
}
@ -809,7 +809,7 @@ SpriteBatchNodeReorderIssue766::SpriteBatchNodeReorderIssue766()
sprite3 = makeSpriteZ(4);
sprite3->setPosition(Vec2(328,160));
schedule(CC_SCHEDULE_SELECTOR(SpriteBatchNodeReorderIssue766::reorderSprite), 2);
schedule(CC_CALLBACK_1(SpriteBatchNodeReorderIssue766::reorderSprite, this), 2, "issue_766_key");
}
std::string SpriteBatchNodeReorderIssue766::title() const
@ -885,7 +885,7 @@ SpriteBatchNodeReorderIssue767::SpriteBatchNodeReorderIssue767()
l3b2->setPosition(Vec2(0+l2bSize.width/2,+50+l2bSize.height/2));
l2b->addChild(l3b2, 1);
schedule(CC_SCHEDULE_SELECTOR(SpriteBatchNodeReorderIssue767::reorderSprites), 1);
schedule(CC_CALLBACK_1(SpriteBatchNodeReorderIssue767::reorderSprites, this), 1, "issue_767_key");
}
std::string SpriteBatchNodeReorderIssue767::title() const
@ -1273,7 +1273,7 @@ SpriteFlip::SpriteFlip()
sprite2->setPosition( Vec2( s.width/2 + 100, s.height/2 ) );
addChild(sprite2, 0, kTagSprite2);
schedule( CC_SCHEDULE_SELECTOR(SpriteFlip::flipSprites), 1);
schedule( CC_CALLBACK_1(SpriteFlip::flipSprites,this), 1, "sprite_flip_key");
}
void SpriteFlip::flipSprites(float dt)
@ -1321,7 +1321,7 @@ SpriteBatchNodeFlip::SpriteBatchNodeFlip()
sprite2->setPosition( Vec2( s.width/2 + 100, s.height/2 ) );
batch->addChild(sprite2, 0, kTagSprite2);
schedule( CC_SCHEDULE_SELECTOR(SpriteBatchNodeFlip::flipSprites), 1);
schedule(CC_CALLBACK_1(SpriteBatchNodeFlip::flipSprites, this), 1, "flip_sprites_key");
}
void SpriteBatchNodeFlip::flipSprites(float dt)
@ -1746,7 +1746,7 @@ void SpriteFrameTest::onEnter()
_sprite2->setFlippedX(false);
_sprite2->setFlippedY(false);
schedule(CC_SCHEDULE_SELECTOR(SpriteFrameTest::startIn05Secs), 0.5f);
schedule(CC_CALLBACK_1(SpriteFrameTest::startIn05Secs, this), 0.5f, "in_05_secs_key");
_counter = 0;
}
@ -1771,8 +1771,8 @@ std::string SpriteFrameTest::subtitle() const
void SpriteFrameTest::startIn05Secs(float dt)
{
unschedule(CC_SCHEDULE_SELECTOR(SpriteFrameTest::startIn05Secs));
schedule(CC_SCHEDULE_SELECTOR(SpriteFrameTest::flipSprites), 1.0f);
unschedule("in_05_secs_key");
schedule(CC_CALLBACK_1(SpriteFrameTest::flipSprites, this), 1.0f, "flip_sprites_key");
}
void SpriteFrameTest::flipSprites(float dt)
@ -2392,7 +2392,7 @@ SpriteHybrid::SpriteHybrid()
_usingSpriteBatchNode = false;
schedule( CC_SCHEDULE_SELECTOR(SpriteHybrid::reparentSprite), 2);
schedule(CC_CALLBACK_1(SpriteHybrid::reparentSprite, this), 2, "reparent_sprite_key");
}
void SpriteHybrid::reparentSprite(float dt)
@ -4353,7 +4353,7 @@ NodeSort::NodeSort()
_sprite5->setPosition(Vec2(356, 160));
_node->addChild(_sprite5, -3, 5);
schedule(CC_SCHEDULE_SELECTOR(NodeSort::reorderSprite));
schedule(CC_CALLBACK_1(NodeSort::reorderSprite, this), "reorder_sprite_key");
}
std::string NodeSort::title() const
@ -4368,7 +4368,7 @@ std::string NodeSort::subtitle() const
void NodeSort::reorderSprite(float dt)
{
unschedule(CC_SCHEDULE_SELECTOR(NodeSort::reorderSprite));
unschedule("reorder_sprite_key");
log("Before reorder--");
@ -4415,7 +4415,7 @@ SpriteBatchNodeReorderSameIndex::SpriteBatchNodeReorderSameIndex()
_batchNode->addChild(_sprite5, 6, 5);
scheduleOnce(CC_SCHEDULE_SELECTOR(SpriteBatchNodeReorderSameIndex::reorderSprite), 2);
scheduleOnce(CC_CALLBACK_1(SpriteBatchNodeReorderSameIndex::reorderSprite, this), 2, "reorder_sprite_key");
}
std::string SpriteBatchNodeReorderSameIndex::title() const
@ -4512,7 +4512,7 @@ SpriteBatchNodeReorderOneChild::SpriteBatchNodeReorderOneChild()
l3b2->setPosition(Vec2(0+l2bSize.width/2,+50+l2bSize.height/2));
l2b->addChild(l3b2);
scheduleOnce(CC_SCHEDULE_SELECTOR(SpriteBatchNodeReorderOneChild::reorderSprite), 2.0f);
scheduleOnce(CC_CALLBACK_1(SpriteBatchNodeReorderOneChild::reorderSprite, this), 2.0f, "reorder_sprite_key");
}
void SpriteBatchNodeReorderOneChild::reorderSprite(float dt)