1

我正在尝试为此做教程:

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

我想为上面的教程添加 CCSprite。我在 Spritebuilder 中布置了 CCSprite,并首先关闭了它们的可见性。当相应的图块出现时,只有 CCSprite 才可见。例如,当 Tile A 第一次出现时,CCSprite A 就会出现。当 Tile A 与另一个 Tile A 合并时,会出现 Tile B,CCSprite B 也会出现,以此类推。

问题是用于确定 tile 值的 switch case 位于 Grid.m 但添加到 Spritebuilder 的 CCSprite 在 MainScene 类的 CCNode 下。如果我不为 CCSprite 放置自定义类,XCode 找不到成员变量。如果我为 CCSprite 放置自定义类“网格”,XCode 将返回错误“此类与键 spriteFrame 的键值编码不兼容”。我应该在这里做什么?

4

1 回答 1

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.

于 2014-05-14T17:19:57.907 回答