【问题标题】:Sprite not adding to scene?雪碧没有添加到场景中?
【发布时间】:2012-10-31 11:53:33
【问题描述】:

我有一个奇怪的问题。

在我的游戏中,我在第一关的场景中添加了一个“目标,如下所示:

//Adds the "targets" or in this case falling objects, to the scene and spawns/moves them
-(void)addTarget {
CCSprite *target = [CCSprite spriteWithFile:@"bankercatch.png"
                                       rect:CGRectMake(0, 0, 21, 40)];

target.tag = 1;

[_targets addObject:target];

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = target.contentSize.width/2;
int maxX = winSize.width - target.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;//Randomizes the place it will spawn on X-Axis

// Create the target slightly off-screen along the top edge,
// and along a random position along the X axis as calculated above
target.position = ccp(actualX, winSize.height + (target.contentSize.height/2));
[self addChild:target];

// Determine speed of the target
int minDuration = 3.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;//Speed is randomized between 2 and 5

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration
                                    position:ccp(actualX, -target.contentSize.height/2)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
                                         selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

}

这一切都很好,直到我想让那个“目标”在它下落时有一个动画。所以我要做的是在 init 中创建 batchNodes 和诸如此类的东西

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: @"DuckSpriteSheet_default.plist"];

    CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"DuckSpriteSheet_default.png"];
    [self addChild:spriteSheet];
NSMutableArray *fallAnimFrames = [NSMutableArray array];
    for(int i = 1; i <=4; ++i) {
        [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"duckling%d.png", i]]];

    }

    CCAnimation *fallAnim = [CCAnimation animationWithFrames:fallAnimFrames delay:0.1f];
    self.Duckling = [CCSprite spriteWithSpriteFrameName:@"duckling1.png"];
    _Duckling.position = ccp(winSize.width*1.5, winSize.height*1.5);
    self.fallAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:fallAnim restoreOriginalFrame:NO]];
    //[_Banker runAction:_WalkAction];
    [spriteSheet addChild:_Duckling];

然后我尝试将我的 -AddTarget 方法更改为如下所示:

//Adds the "targets" or in this case falling objects, to the scene and spawns/moves them
-(void)addTarget {
//CCSprite *target = [CCSprite spriteWithFile:@"bankercatch.png"
//                                       rect:CGRectMake(0, 0, 21, 40)];
_Duckling.tag = 1;

[_targets addObject:_Duckling];
[_Duckling runAction:_fallAction];
// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = _Duckling.contentSize.width/2;
int maxX = winSize.width - _Duckling.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;//Randomizes the place it will spawn on X-Axis
NSLog(@"About to add Duckling to self");
// Create the target slightly off-screen along the top edge,
// and along a random position along the X axis as calculated above
_Duckling.position = ccp(actualX, winSize.height + (_Duckling.contentSize.height/2));
[self addChild:_Duckling];//PROBLEM HERE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
NSLog(@"Added _Duckling to self");
// Determine speed of the target
int minDuration = 3.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;//Speed is randomized between 2 and 5

// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration
                                    position:ccp(actualX, -_Duckling.contentSize.height/2)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
                                         selector:@selector(spriteMoveFinished:)];
[_Duckling runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

}

我通过使用 NSLogs 查找行来评论问题所在。我得到了错误

Assertion failure in -[DuckL1Layer addChild:z:tag:], /Users/tyler_reynold/Desktop/Programming/Games/Catch It/Catch It/libs/cocos2d/CCNode.m:388

不是在启动时,而是在 AddTarget 运行时(它在 CCScheduleTimer 上运行)。

有什么想法吗?谢谢

【问题讨论】:

  • 显示控制台输出,它会说出断言的类型。

标签: ios cocos2d-iphone ccsprite addchild


【解决方案1】:

由于 cocos2d 是开源的,你可以在它的代码中看到实际的 assert 行。我假设您尝试添加到已添加到另一个父级的 self 精灵。在这种情况下,它是您的 CCSpriteBatchNode 实例。如果您尝试将 CCNode 的子类添加到某个父级,而它已经有父级,则任何 CCNode 的子类都会导致断言失败。

【讨论】:

  • 那么我将如何解决问题@Morion
  • 我摆脱了 [spriteSheet addChild: _Duckling];在初始化中,只是做了 [self addChild: _Duckling];在 addTarget 方法中,它一直通过该方法(由 NSLogs 检查)但在之后立即崩溃并且不添加目标。 @Morion
  • 如果没有崩溃消息,很难说问题出在哪里
  • 这是我的问题,它不会在调试器中给我一个错误它只是崩溃
  • 如果它确实没有向控制台打印任何崩溃原因,但总是崩溃,请添加所有异常断点。它会让你看到导致崩溃的行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
相关资源
最近更新 更多