2017-03-22 3 views
0

ラムダ内でSpawnアクションとScaleアクションを実行しようとしていますが、Lambdaはそれらの値をまったくコピーしません。以下のコードでは、私は定義されたonSelectedSpawnを持っています。これはスポーンです。 mSettingmFamilyTVはMenuItemImageです。私が間違っていることは何ですか?あなたの助けは非常に高く評価されます。Lambdaはcocos2dでスポーンとアクションをコピーできませんでしたか?

auto fadeIn = FadeTo::create(0.5f, 255); 
auto scaleIn = ScaleBy::create(0.5f, 1.4f); 
auto onSelectedSpawn = Spawn::createWithTwoActions(fadeIn, scaleIn); 

// This run without any problem 
mSetting->runAction(onSelectedSpawn); 
mFamilyTV = MenuItemImage::create("en_block5.png", "en_block5_hover.png", 
     [=](cocos2d::Ref* pSender){ 

     //Running Spawn makes app crashed because the lambda couldn't copy onSelectedSpawn's value 
    mFamilyTV->runAction(onSelectedSpawn); 
     //Running Scale action make app crashed too. It also doesn't copy scaleIn at all 
     mFamilyTV->runAction(scaleIn); 
}); 

答えて

1

ラムダでローカル変数を使用するには、明示的に渡します。また、アニメーションはメモリから解放されるため、アニメーションを保持する必要があります。

auto fadeIn = FadeTo::create(0.5f, 255); 
auto scaleIn = ScaleBy::create(0.5f, 1.4f); 
scaleIn->retain(); 
auto onSelectedSpawn = Spawn::createWithTwoActions(fadeIn, scaleIn); 
onSelectedSpawn->retain(); 

// This run without any problem 
mSetting->runAction(onSelectedSpawn); 
mFamilyTV = MenuItemImage::create("en_block5.png", "en_block5_hover.png", 
     [&, scaleIn, onSelectedSpawn](cocos2d::Ref* pSender){ 

     //this will only work once, next time you have to clone action 
     mFamilyTV->runAction(onSelectedSpawn->clone()); 
     mFamilyTV->runAction(scaleIn->clone()); 
}); 

//somewhere where you no longer need these animations, for example when leaving a scene: 
scaleIn->release(); 
onSelectedSpawn->release(); 

あなたはラムダにアクションを渡し、保持/解放とクローンについて覚えておく必要があるので、この解決策はあまりにも苦痛だと思います。 これを行う最も簡単な方法は、所定の位置にこれらのアニメーションを作成する特別な機能を、作成するために、次のようになります。

あなたのアドバイスの作品
mFamilyTV = MenuItemImage::create("en_block5.png", "en_block5_hover.png", 
     [&](cocos2d::Ref* pSender){ 

     mFamilyTV->runAction(createOnSelectedSpawn()); 
     mFamilyTV->runAction(createScaleIn()); 
}); 

ActionInterval* HelloWorld::createScaleIn(){ 
    return ScaleBy::create(0.5f, 1.4f); 
} 

ActionInterval* HelloWorld::createOnSelectedSpawn(){ 
    auto fadeIn = FadeTo::create(0.5f, 255); 
    auto scaleIn = createScaleIn(); 
    return Spawn::createWithTwoActions(fadeIn, scaleIn); 
} 
+0

は魅力が好き。ありがとう、あなたは私の日を救った。あなたの返事を100回投票することができれば幸いです。 :) –

関連する問題