iosobjective-ccocos2d-iphoneccscene

Unable to cast CCScene


I am trying to load a CCScene and then change some of it's properties before using the scene. When I try to change it's properties it says [CCScene setProperty] - unrecognized selector sent to instance, even though I casted the CCScene to the correct class with the public properties!

#import "MainScene.h"
#import "Gameplay.h"

@implementation MainScene
-(void)play:(CCButton *)sender{
    Gameplay *gameplay = (Gameplay *)[CCBReader loadAsScene:@"Gameplay"];
    NSLog(@"%@\n",[gameplay class]);
}
@end

This code outputs CCScene instead of outputting Gameplay. How do I fix this?


Solution

  • This is not a problem of the cast. If you peform loadAsScene the CCBReader will wrap the Gameplay.ccb root node into a scene. You can read that in the documentation:

    ... Loads a ccbi-file with the specified name and wraps it in a CCScene node.

    If you use the console to print the children of this node:

    (lldb) po scene.children
    

    You will get following result:

    <__NSArrayM 0x13a38720>(
      <Gameplay = 0x13a3f8a0 | Name = >
    )
    

    This shows how your gameplay class is wrapped into a CCScene. If you want to access the Gameplay node you need to access the first child of the CCScene:

        Gameplay *gameplay = (Gameplay *)[[CCBReader loadAsScene:@"GameplayScene"] children] [0];
    

    Feel free to use a different solution that doesn't do all the work in one line.