【问题标题】:Sprite touch detection精灵触摸检测
【发布时间】:2013-09-23 08:53:47
【问题描述】:

在这里有一个问题。我在我的 (id)init 函数中创建了几个精灵(带有标签),然后只是试图检测哪个精灵被触摸了?下面粘贴了来自我的 init 函数的代码的 sn-p。

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"blue_sheet.plist"];
    //create a sprite batch node
    CCSpriteBatchNode *TrainerSprites = [CCSpriteBatchNode batchNodeWithFile:@"blue_sheet.png"];
    [self addChild:TrainerSprites z:1];

    //create a sprite from that node
    CCSprite *Horse = [CCSprite spriteWithSpriteFrameName:@"horse_blue.png"];
    [TrainerSprites addChild:Horse z:1 tag:1];
    //Horse.position = ccp(winSize.width/5, winSize.height/2);
    [Horse setScaleX: 138.5/Horse.contentSize.width];
    [Horse setScaleY: 80/Horse.contentSize.height];

    //create a sprite from that node
    CCSprite *Cow = [CCSprite spriteWithSpriteFrameName:@"cow_blue.png"];
    [TrainerSprites addChild:Cow z:1 tag:2];
    //Cow.position = ccp(winSize.width/2, winSize.height/2);
    [Cow setScaleX: 126/Cow.contentSize.width];
    [Cow setScaleY: 100/Cow.contentSize.height];

    Horse.position = ccp(4*winSize.width/5, winSize.height/2);
    Cow.position = ccp(winSize.width/5, winSize.height/2);

    CGRect pos1 = CGRectMake(Cow.position.x, Cow.position.y, 200, 100);
    CGRect pos2 = CGRectMake(Horse.position.x, Horse.position.y, 200, 100);

    self.touchEnabled = YES;

一切看起来都很好......并且精灵出现在它们应该出现的位置。当我触摸屏幕上的任何位置时,我的 ccTouchBegan 函数就会触发。没有看到 CGRect 发生任何事情,我想我需要确定分配的标签触发了哪一个。是的,确实,我知道我缺少代码,我只是无法在任何地方找到好的可靠文档如何执行这个看似基本的 ios 功能。我假设“精灵触摸检测”代码应该驻留在 ccTouchBegan 函数中?真诚感谢任何帮助或指导。 :)

【问题讨论】:

    标签: ios iphone cocos2d-iphone ccsprite cgrect


    【解决方案1】:

    另一种方法是继承 CCSprite 并实现 TargetedTouchDelegate

    类似:

    @interface AnimalSprite:CCSprite<CCTargetedTouchDelegate>
    

    这种方法的优点是您不必在添加精灵的图层中进行大量“如果”检查。该链接提供了您必须实现的方法以实现协议以及您可以在何处以及如何注册触摸调度程序。

    【讨论】:

    • 感谢您的 cmets。以后我一定会更深入地研究这个。然而,现在我对“简单”感兴趣,因为“更好”。这意味着在不添加额外基础设施的情况下保持代码尽可能简单。我真正想做的是检测精灵标签(因为只有一个精灵会被标记正确和特定的标签,表明选择了正确的动物)。谢谢...
    【解决方案2】:

    要检测精灵触摸,您可以使用它

    在你的 .h 部分声明 CCSprite *Cow

    在 .m 部分使用这个
    在初始化方法中

    //create a sprite from that node
        Cow = [CCSprite spriteWithSpriteFrameName:@"cow_blue.png"];
        [TrainerSprites addChild:Cow z:1 tag:2];
        //Cow.position = ccp(winSize.width/2, winSize.height/2);
        [Cow setScaleX: 126/Cow.contentSize.width];
        [Cow setScaleY: 100/Cow.contentSize.height];
    

    接触开始方法

     -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
        {    
            UITouch * touch =[touches anyObject];
            CGPoint location=[touch locationInView:[touch view]];
            location =[[CCDirector sharedDirector] convertToGL:location];
            if (CGRectContainsPoint( [Cow boundingBox], location)) {
    
                   /* CCScene *scene = [CCScene node];
                    [scene addChild:[ClassicScene node]];
                    [[CCDirector sharedDirector] replaceScene:scene];*/
                }
    
        }
    

    【讨论】:

    • 终于回到这个...谢谢@Abhijit,确实我正在尝试你之前建议的东西。关于您发布的代码,但出现了几个问题。 (1) UITouch 行被标记为“触摸”被重新定义,(2) 牛边界框未声明。感谢您一直以来的建议...
    • 那么在后续,Cow 边界框需要在哪里声明以便 ccTouchBegan 例程知道呢?谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多