1

我在HelloWorldLayer下面有一个图层,触摸可以在任何地方工作,但我希望它仅在触摸图层中的精灵时才起作用 -turtle下方。

如果我尝试添加self.isTouchEnabled = YES;CCTurtle它说的图层

isTouchEnabled在对象类型 CCTurtle 上找不到属性

我的输出如下

2013-01-08 20:30:14.767 FlashToCocosARC[6746:d503] cocos2d:解除分配

2013-01-08 20:30:15.245 FlashToCocosARC[6746:d503] 播放行走动画2

这是我的 HelloWorldLayer 代码:

#import "HelloWorldLayer.h"
#import "CCTurtle.h"

@implementation HelloWorldLayer

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    HelloWorldLayer *layer = [HelloWorldLayer node];
    [scene addChild: layer];
    return scene;
}

-(id) init
{
    if( (self=[super init])) {
        turtle= [[CCTurtle alloc] init];
        [turtle setPosition:ccp(300, 100)];
        [self addChild:turtle];
        ///addChild:child z:z tag:aTag;
        self.isTouchEnabled = YES;
        turtle. tag=4;
    //

    }
return self;
}



//- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    // Processing all touches for multi-touch support
//    UITouch *touch = [touches anyObject];
//    if ([[touch view] isKindOfClass:[turtle class]]) {
//        NSLog(@"[touch view].tag = %d", [touch view].tag);
//        [self toggleTurtle];
//    }
//}
-(BOOL)containsTouch:(UITouch *)touch {
    CGRect r=[turtle textureRect];
    CGPoint p=[turtle convertTouchToNodeSpace:touch];
    return CGRectContainsPoint(r, p );
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //////GENERAL TOUCH SCREEN
    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInView:[touch view]];
        touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
        [self toggleTurtle];
        /////
    }

}
-(void) toggleTurtle
{
    NSLog(@"playing walk animation2");
   [turtle playAnimation:@"walk_in" loop:NO wait:YES];
}



@end

//你好世界.h

    #import "cocos2d.h"
    #import "CCTurtle.h"

    @interface HelloWorldLayer : CCLayer 
    {
    CCTurtle                *turtle;
    }

    +(CCScene *) scene;

    @end

//CC乌龟

    #import <Foundation/Foundation.h>
    #import "FTCCharacter.h"

    @interface CCTurtle : FTCCharacter <FTCCharacterDelegate, CCTargetedTouchDelegate>
    {

    }
    @end

我正在使用 Cocos2D cocos2d v1.0.1(启用了拱门),并且正在 ipad 4.3 模拟器上进行测试。感谢娜塔莉

我试图将触摸直接放入 ccturtle.m 以便它可以使用 CCTargetedTouchDelegate 处理自己的触摸,但使用

CCturtle/// 我把文件改成这个,尝试用不同的方式找到被触摸的区域...

    - (CGRect)rect
   {
   CGSize s = [self.texture contentSize];
   return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
   }

   -(BOOL) didTouch: (UITouch*)touch {
   return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
   //return CGRectContainsPoint( [self rect], [self convertTouchToNodeSpaceAR: touch] );
   }

   -(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event {
   NSLog(@"attempting touch.");
   if([self didTouch: touch]) {
   return [self tsTouchBegan:touch withEvent: event];
   }
   return NO;
   }

但仍然不会编译,因为仍然返回错误“在对象类型'CCTurtle *'上找不到属性'is TouchEnabled'

我真的不确定我能做些什么来让它现在运行......并且真的需要让它工作(我想我可以制作隐形按钮,但能够正确找到 ccturtle 并了解什么会更好我做错了...希望有人可以提供帮助

4

3 回答 3

1

首先,我看不到任何containsTouch:方法调用。这里有几个建议:

使用boundingBox而不是textureRect获取节点的本地矩形(在这种情况下是您的海龟)。或者只是将containsTouch:方法替换为您的海龟类以封装它。例如,如果您想让乌龟的触摸区域更大/更小,这可能会有所帮助。你只需要在你的龟类中改变一个小方法。

在您的ccTouchesBegan:withEvent:方法中,只需检查每只海龟是否被此触摸击中。然后,例如,您可以创建字典,以 touch 作为键,并以对应的海龟数组作为值。然后你只需要在你的ccTouchesMoved:withEvent:方法中更新所有海龟的位置来移动触摸,并从字典中删除这个海龟数组ccTouchesEnded:withEvent:和 ccTouchCancelled:withEvent: 方法。

于 2013-01-08T13:28:05.740 回答
0

如果您希望您的 CCTurtle 对象接受目标触摸,您可以通过指定它符合 CCTargetedTouchDelegate 协议来实现。在您的 CCTurtle @interface 中,您可以这样声明:

@interface CCTurtle : CCNode <CCTargetedTouchDelegate>

然后在实现中,告诉它接受触摸并实现 ccTouch 方法:

@implementation CCTurtle

-(id) init {
.
.
    [[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:1 swallowsTouches:YES];
    return self;
}

-(void) onExit {
    [[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
    CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
    if (CGRectContainsPoint([self boundingBox], location] {
        // some code here
    }
}


-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { // code here }
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { // code here}
-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event { // code here}
于 2013-01-08T20:29:00.117 回答
0

多亏了 Prototypical,问题得到了解决,尽管 CCTurtle 是一个带有一种精灵类型的图层,但它是嵌套精灵,这意味着 cocos2d 在为我的“包含触摸方法”创建正确的边界框时遇到问题

所以他用一点魔法将他的“获取完整边界框”方法结合到包含触摸方法中来解释精灵的孩子,并且代码现在依赖于碰撞检测来处理触摸。目前我很高兴为他制作一些漂亮的图标作为回报

但想对所有帮助过的人说声谢谢,并希望这种方法对遇到同样问题的人有用!

于 2013-01-10T08:25:42.860 回答