1

I'm trying to do the tutorial for this:

https://www.makegameswith.us/gamernews/384/build-your-own-2048-with-spritebuilder-and-cocos2d

I want to add CCSprite for the above tutorial. I laid out the CCSprite in Spritebuilder and turn their visibility off at first. When the respective tile appear, only then the CCSprite will be visible. For example, when Tile A first appear, then CCSprite A will appear. When Tile A merge with another Tile A, Tile B will appear and so does CCSprite B and so on.

The problem is that the switch case used to determine the tile value is located at Grid.m but the CCSprite added to Spritebuilder is under the CCNode of MainScene class. If i don't put custom class for CCSprite, XCode couldn't find the member variable. If i put custom class of "Grid" for CCSprite, XCode would return error of "This class is not key value coding-compliant for the key spriteFrame." What should i do here?

1 Answers1

3

Assuming in your CCB the root node is a CCNode (A) and you add a child CCSprite (B) to it, then:

  • if specified, the custom class of node A must be a subclass of CCNode
  • if specified, the custom class of node B must be a subclass of CCSprite

Regarding the variable assignment:

  • the variable of node A should be empty (at best it will create a reference to itself)
  • if needed, the variable of node B should be set to "doc root" and given a legal ObjC variable name (ie _nodeB)

Now when you set the custom var of node B and it is set to "doc root", you must add a correspondingly named property or ivar to the custom class for node A. Which means node A must have a custom class for "doc root" variables to work. Let's name it CustomClassA:

@interface CustomClassA : CCNode
{
    CCSprite* _nodeB;
}
@end

Assuming you made node B use a custom class (inherited from CCSprite) you can also specify the custom class:

@class Grid;

@interface CustomClassA : CCNode
{
    Grid* _grid;
}
@end

And add #import CCSpriteCustomClassB.h to the implementation file of CustomClassA.

Note that variable assignment does work without using a custom class in the node where you enter the variable name, but the root node must use a custom class for variable assignment to work.

PS: I'm not entirely clear on the "owner" variable setting. Normally owner will be nil, it's only non-nil when you specify it in code using CCBReader's load:owner: method. This serves some purpose but I haven't found and can't think of a use case for it at the moment.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217