检测是否在Cocos2d-iphone上触摸了特定的精灵 [英] Detecting if a specific sprite was touched on Cocos2d-iphone

查看:156
本文介绍了检测是否在Cocos2d-iphone上触摸了特定的精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是按照Ray的教程制作一个简单的iPhone游戏(这里: http://goo.gl/fwPi ),并决定我希望敌人在被触动时被消除。

I was following Ray`s tutorial for making a simple iPhone game (here: http://goo.gl/fwPi) , and decided that i wanted the enemies to be eliminated when they get touched.

我的初始方法是在触摸位置产生一个小的CCSprite sprite,然后使用CGRectMake创建一个所述精灵的边界框,以检测敌人精灵是否被触摸。就像雷与射弹/敌人一样。但是,当然,我的做法是不工作,我不能挖出这个洞。

My initial approach was to spawn a small CCSprite sprite on the touch location, then use CGRectMake to create a bounding box of said sprite to detect if the enemy sprite was touched. Much like Ray does with the projectile/enemy. But of course, my way of doing it isnt working and i cant dig myself out of this hole.

这里是相关的代码片段。任何帮助是赞赏:

Here is the relevant code snippet. Any help is appreciated:


- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace: touch];
    location = [[CCDirector sharedDirector] convertToGL:location];
    CCSprite *touchedarea = [CCSprite spriteWithFile:@"Icon-72.png" rect:CGRectMake(location.x, location.y, 2, 2)];
    touchedarea.tag = 2;
    [self addChild:touchedarea];
    [_touchedareas addObject:touchedarea];

}



- (void)update:(ccTime)dt {

    NSMutableArray *touchedareasToDelete = [[NSMutableArray alloc] init];
    for (CCSprite *touchedarea in _touchedareas) {
        CGRect touchedareaRect = CGRectMake(
                                           touchedarea.position.x, 
                                           touchedarea.position.y, 
                                           touchedarea.contentSize.width, 
                                           touchedarea.contentSize.height);

        NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
        for (CCSprite *target in _targets) {
            CGRect targetRect = CGRectMake(
                                           target.position.x - (target.contentSize.width/2), 
                                           target.position.y - (target.contentSize.height/2), 
                                           target.contentSize.width, 
                                           target.contentSize.height);

            if (CGRectIntersectsRect(touchedareaRect, targetRect)) {
                [targetsToDelete addObject:target];             
            }                       
        }

        for (CCSprite *target in targetsToDelete) {
            [_targets removeObject:target];
            [self removeChild:target cleanup:YES];                                  
        }

        if (targetsToDelete.count > 0) {
            [touchedareasToDelete addObject:touchedarea];
        }
        [targetsToDelete release];
    }

    for (CCSprite *touchedarea in touchedareasToDelete) {
        [_touchedareas removeObject:touchedarea];
        [self removeChild:touchedarea cleanup:YES];
    }
    [touchedareasToDelete release];
}


推荐答案

这看起来是一个非常困难的方式去做。我没有自己编码长,但也许以下可能会帮助你。
让说有一个名为敌人的nsmutablearray,当你创建一个新的敌人对象添加到这个数组。敌人对象将是一个ccnode,并有一个ccsprite在其中调用_enemySprite
然后触摸

That looks like a very difficult way to go about doing it. I havent been coding long myself but maybe the following might help you. lets say u have a nsmutablearray called enemies and you add the new enemy object to this array when ever you create one. enemy object would be a ccnode and have a ccsprite within it called _enemySprite then do the touch

 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {

    NSSet *allTouches = [event allTouches];
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
    //UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    int arraysize = [enemies count];
    for (int i = 0; i < arraysize; i++) {


        if (CGRectContainsPoint( [[[enemies objectAtIndex:i] _enemySprite] boundingBox], location)) {

            //some code to destroy ur enemy here


        }
    }
    //  NSLog(@"TOUCH DOWN");

}

希望这有助于

这篇关于检测是否在Cocos2d-iphone上触摸了特定的精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆