2012-02-23 56 views
0

在我的应用程序中,我使用了CCSprites。它们以正确的格式和ccTouchesBegan函数正确定义。然后我添加了拖放代码。他们是在xcode中拖放CCSprite

- (void)selectSpriteForTouch:(CGPoint)touchLocation { 
CCSprite * newSprite = nil; 
NSMutableArray *movableSprites=[[NSMutableArray alloc] initWithObjects:Tocken1,Tocken2,Tocken3]; 
    for (CCSprite *sprite in movableSprites) 
    { 
     if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) 
     {    
      newSprite = sprite; 
      break; 
     } 
}  
if (newSprite != selSprite) { 
    [selSprite stopAllActions]; 
    [selSprite runAction:[CCRotateTo actionWithDuration:0.1 angle:0]]; 
    CCRotateTo * rotLeft = [CCRotateBy actionWithDuration:0.1 angle:-4.0]; 
    CCRotateTo * rotCenter = [CCRotateBy actionWithDuration:0.1 angle:0.0]; 
    CCRotateTo * rotRight = [CCRotateBy actionWithDuration:0.1 angle:4.0]; 
    CCSequence * rotSeq = [CCSequence actions:rotLeft, rotCenter, rotRight, rotCenter, nil]; 
    [newSprite runAction:[CCRepeatForever actionWithAction:rotSeq]];    
    selSprite = newSprite; 
} 
} 
- (CGPoint)boundLayerPos:(CGPoint)newPos { 
CGSize winSize = [CCDirector sharedDirector].winSize; 
CGPoint retval = newPos; 
retval.x = MIN(retval.x, 0); 
retval.x = MAX(retval.x, -background.contentSize.width+winSize.width); 
retval.y = self.position.y; 
return retval; 
} 
- (void)panForTranslation:(CGPoint)translation {  
if (Tocken1) { 
    CGPoint newPos = ccpAdd(Tocken1.position, translation); 
    Tocken1.position = newPos; 
} else { 
    CGPoint newPos = ccpAdd(self.position, translation); 
    self.position = [self boundLayerPos:newPos];  
} 
} 
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event 
{  
CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 

CGPoint oldTouchLocation = [touch previousLocationInView:touch.view]; 
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation]; 
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation]; 

CGPoint translation = ccpSub(touchLocation, oldTouchLocation);  
[self panForTranslation:translation];  
} 

现在当我点击精灵程序终止与下面的错误代码。

2012-02-23 10:12:47.377 whoonu [3574:207]在*断言故障 - [WhooseitView ccTouchBegan:withEvent:方法],/用户/ sandeepkuttiyatur /文档/升降梭箱/ ibiz_completed_apps/WhoonuV9/CatRace /库/ cocos2d的/ CCLayer.m:259

2012-02-23 10:12:47.378 whoonu [3574:207] *终止应用程序由于未捕获的异常 'NSInternalInconsistencyException',原因:“层#ccTouchBegan覆盖我'

*** Call stack at first throw: 
(
0 CoreFoundation    0x02c23b99 __exceptionPreprocess + 185 
1 libobjc.A.dylib   0x02d7340e objc_exception_throw + 47 
2 CoreFoundation    0x02bdc238 +[NSException raise:format:arguments:] + 136 
3 Foundation     0x02674e37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116 
4 whoonu      0x00033873 -[CCLayer ccTouchBegan:withEvent:] + 179 
5 whoonu      0x0008efa7 -[CCTouchDispatcher touches:withEvent:withTouchType:] + 1255 
6 whoonu      0x0008fb6f -[CCTouchDispatcher touchesBegan:withEvent:] + 111 
7 whoonu      0x00091a21 -[EAGLView touchesBegan:withEvent:] + 113 
8 UIKit      0x007db324 -[UIWindow _sendTouchesForEvent:] + 395 
9 UIKit      0x007bccb4 -[UIApplication sendEvent:] + 447 
10 UIKit      0x007c19bf _UIApplicationHandleEvent + 7672 
11 GraphicsServices   0x03236822 PurpleEventCallback + 1550 
12 CoreFoundation    0x02c04ff4 
       __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52 
13 CoreFoundation    0x02b65807 __CFRunLoopDoSource1 + 215 
14 CoreFoundation    0x02b62a93 __CFRunLoopRun + 979 
15 CoreFoundation    0x02b62350 CFRunLoopRunSpecific + 208 
16 CoreFoundation    0x02b62271 CFRunLoopRunInMode + 97 
17 GraphicsServices   0x0323500c GSEventRunModal + 217 
18 GraphicsServices   0x032350d1 GSEventRun + 115 
19 UIKit      0x007c5af2 UIApplicationMain + 1160 
20 whoonu      0x000bceaf main + 127 
21 whoonu      0x00001ed5 start + 53 
22 ???      0x00000001 0x0 + 1 

) 终止呼叫在抛出“NSException”实例后编辑

+0

:(没有回答.... – priya 2012-02-23 09:15:38

回答

0

您没有在图层中实现方法“ccTouchBegan”。 父类方法告诉你它需要被覆盖。

原因: '图层#ccTouchBegan重写我的'

这样实现:

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    // add code to handle touch... 

    // return YES to claim all touches when they begin 
    // or return only YES for those touches you want to deal with 
    return YES; 
} 

更新:

您可以看到该错误消息来了从如果您在“.../libs/cocos2d/...”文件夹中查看文件“CCLayer.m”(假设您有一个典型的项目结构再从模板等) ,让这个错误代码是在行〜257(cocos2d的1.0.1版),看起来像这样:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    NSAssert(NO, @"Layer#ccTouchBegan override me"); 
    return YES; 
} 
#endif 
+0

我添加了这一点。 。:( – priya 2012-02-24 05:13:43

+0

检查你的'ccTouchBegan'方法*是否真正*在正确的类中实现。这部分从错误消息清楚地表明,该方法发送到CCLayer父类:“原因:'层#ccTouchBegan覆盖我'” – phlebotinum 2012-02-24 11:49:43

+0

我删除了该代码并添加了一些教程,现在它开始工作。我不知道它出错了。无论如何,谢谢你robo :) – priya 2012-02-24 11:52:27