简体   繁体   English

精灵触摸检测

[英]Sprite touch detection

Have a question here. 有一个问题在这里。 I created a couple sprites (with tags) in my (id)init function, and then merely trying to detect which sprite was touched? 我在(id)init函数中创建了几个Sprite(带有标签),然后仅尝试检测触摸了哪个Sprite? A snippet of the code from my init function is pasted below. 我的init函数的代码片段粘贴在下面。

    [[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;

All looks fine... and the sprites appear where they are supposed to. 一切看起来都很好……精灵将出现在它们应有的位置。 When I touch anywhere on screen my ccTouchBegan function fires. 当我触摸屏幕上的任意位置时,ccTouchBegan函数将触发。 Not seeing anything happen with the CGRect and I suppose I need to determine which one fired by the assigned tag. CGRect没有看到任何事件,我想我需要确定分配的标签触发了哪一个。 Yes indeedy, I know I'm missing code, I just cannot locate good solid documentation anywhere how to do this seemingly basic ios functionality. 是的,的确如此,我知道我缺少代码,我只是无法在任何地方找到良好的可靠文档,以实现该看似基本的ios功能。 I assume the "sprite touch detection" code should reside inside the ccTouchBegan function? 我假设“ sprite触摸检测”代码应驻留在ccTouchBegan函数内部? Any help or guidance sincerely appreciated. 真诚的感谢您的帮助或指导。 :) :)

to detect the sprite touch you can use this 可以检测到精灵触摸

declare CCSprite *Cow in your .h section 在您的.h部分中声明CCSprite *Cow

and in .m section use this 在.m部分使用
in init method 初始化方法

//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];

in touches began method 接触开始的方法

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

    }

Another approach could be to subclass CCSprite and implement the TargetedTouchDelegate . 另一种方法可能是CCSprite的子类并实现TargetedTouchDelegate

Something like: 就像是:

@interface AnimalSprite:CCSprite<CCTargetedTouchDelegate>

The advantage of this approach is that you wont have to do a lot of "If" checks in the layer where you are adding the sprites. 这种方法的优点是,您无需在要添加精灵的图层中进行大量的“ If”检查。 The link provides the method that you must implement in orer to implement the protocol and where and how you could register with the touch dispatcher. 该链接提供了您必须在orer中实现的方法才能实现协议,以及在何处以及如何向touch dispatcher注册。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM