在 xcode 中拖放 CCSprite

发布于 2025-01-08 02:14:47 字数 4305 浏览 0 评论 0原文

在我的应用程序中,我使用了 CCSprites。它们以正确的格式定义,并且 ccTouchesBegan 正常运行。然后我添加了拖放代码。现在

- (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:] 中断言失败, /Users/sandeepkuttiyatur/Documents/Dropbox/ibiz_completed_apps/WhoonuV9/CatRace/libs/cocos2d/CCLayer.m:259

2012-02-23 10:12:47.378 whoonu[3574:207] * 终止应用程序由于未捕获的异常'NSInternalInconsistencyException',原因:'Layer#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”实例后调用终止

In my application i have used CCSprites. They are defines in the proper format and ccTouchesBegan functions properly. Then I added the codes for drag and drop. they are

- (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];    
}

now when i click on the sprite the program gets terminated with the following error code.

2012-02-23 10:12:47.377 whoonu[3574:207] * Assertion failure in -[WhooseitView ccTouchBegan:withEvent:], /Users/sandeepkuttiyatur/Documents/Dropbox/ibiz_completed_apps/WhoonuV9/CatRace/libs/cocos2d/CCLayer.m:259

2012-02-23 10:12:47.378 whoonu[3574:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Layer#ccTouchBegan override me'

*** 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

)
terminate called after throwing an instance of 'NSException'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-01-15 02:14:47

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

原因:“Layer#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;
}

更新:

如果您查看“...”中的文件“CCLayer.m”,您可以看到该错误消息来自何处。 /libs/cocos2d/...”文件夹(假设您有一个典型的项目结构,例如模板)
给出此错误的代码位于第 ~257 行(cocos2d v1.0.1),如下所示:

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

You did not implement the method "ccTouchBegan" in your layer.
The parent class method tells you it needs to be over-ridden.

reason: 'Layer#ccTouchBegan override me'

Implement it like this:

- (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;
}

Update:

You can see where that error message is coming from if you look into the file "CCLayer.m" in your ".../libs/cocos2d/..." folder (assuming you have a typical project structure like from a template)
The code that gives this error is at line ~257 (cocos2d v1.0.1) and looks like this:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSAssert(NO, @"Layer#ccTouchBegan override me");
    return YES;
}
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文