试图触摸阵列中的项目不起作用

时间:2014-10-23 09:41:58

标签: cocos2d-iphone

我在触摸开始方法中有一个数组(我希望能够触摸精灵 并且对于它来说,触摸它是否有什么东西我忘了做或做错了什么?

我可以在屏幕上记录触摸,但当我触摸气泡时没有任何反应。 任何帮助都会很棒。

-(id) init
{ 
if((self=[super initWithColor:ccc4(10, 10, 10,10)]) )  //sand 101, 116, 88
{
    size = [[CCDirector sharedDirector] winSize];
    self.touchEnabled = YES;

 //other stuff here

Bubble01 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255     gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@",     [Sortingarray objectAtIndex:BubbleAnswerBubble_1_IndexValue]]];
    Bubble02 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_2_IndexValue]]];
    Bubble03 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_3_IndexValue]]];
    Bubble04 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_4_IndexValue]]];
    Bubble05 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_5_IndexValue]]];
    Bubble06 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:AnswerBubble_6_IndexValue]]];

//other stuff here

}
return self;
}

的touchesBegan

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//set up touches
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

//log if touches are working if I touch the screen area
NSLog(@"touches screen");

//create an array from bubble class (CCSprites with labels, 
//I need to be able to determine which sprite was touched and run an action on it)

BubbleArray = [[NSMutableArray alloc]initWithObjects:Bubble01,
               Bubble02,
               Bubble03,
               Bubble04,
               Bubble05,
               Bubble06, 
               nil];

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

    Bubble *sprite = (Bubble *)[BubbleArray  objectAtIndex:i];

    //create a rect to find the position and size of the sprite 
    //BackBubble is a sprite that i'm using to detect the content size

    CGRect targetRect = CGRectMake(

           sprite.BackBubble.position.x - (sprite.BackBubble.contentSize.width/2),
           sprite.BackBubble.position.y - (sprite.BackBubble.contentSize.height/2),
           sprite.BackBubble.contentSize.width,
           sprite.BackBubble.contentSize.height);

    //use the rect and touch location to determine hit
    if(CGRectContainsPoint(targetRect, location))

    //this doesn't work possibly because Bubble class is a CClayer?

    //if(CGRectContainsPoint([sprite boundingBox], location))
    {
        selectedSprite = sprite;
        NSLog(@"touches bubble sprite");
    }
}

}

任何洞察力都可以帮助我理解我做错了什么。 干杯:)

新的数组代码(通过自定义类获取精灵的高度和宽度

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


    Bubble *sprite = (Bubble *)[BubbleArray  objectAtIndex:i];
    location = [sprite convertToNodeSpace:location];

    if(CGRectContainsPoint([sprite.BackBubble boundingBox], location))
    {
        selectedSprite = sprite;
           NSLog(@"touches bubble");
    }
   }

Bubble.m

    #import "Bubble.h"
#import "Common.h"

#define ButtonFlashTime .4
#define KBubbleColourTurqoiseBlueFlash 2323
#define ScrollSpeed 5.2f
#define DecoyTextY 5
#define DecoyTextX -2


@implementation Bubble
@synthesize BackBubble,FrontShine,BubbleLabel,startX,startY,currentY,currentX,isNotTouchActivated,myInt,bubblespeed,tagNumber;  //isTouched

-(id)init
{
    self=[super init];
    {
        //touches
        [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
        isNotTouchActivated = false;
        isTouched = NO;

         bubblespeed = 0;

        //start scrolling
        [self MoveWithoutProblem];

        [self setAnchorPoint:ccp(0.5,0.5)];

        BackBubble = [CCSprite spriteWithSpriteFrameName:Bubblepng];
        BackBubble.position = ccp(X,Y);
        [BackBubble setAnchorPoint:ccp(0,0)];
        [self addChild: BackBubble z:5];
        NSLog(@"BackBubble, %f %f",BackBubble.position.x,BackBubble.position.y);

        //other code here

        [self setContentSize:[BackBubble boundingBox].size];

    }
    return self;
}



-(BOOL) isTouchOnSprite:(CGPoint)touch{
    CGPoint local = [self convertToNodeSpace:touch];
    CGRect r = self.boundingBox;
    r.origin = CGPointZero;
    Boolean b = CGRectContainsPoint( r, local );
    //CCLOG(@"touch %f : %f : %d",touch.x,touch.y,b);
    if (b) {
        return YES;
    }else {
        return NO;
    }
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    if([self isTouchOnSprite:touchPoint]){
        //CGPoint move = [self convertTouchToNodeSpace:touch];
        isNotTouchActivated = TRUE;
        //isTouched = YES;
         //NSLog(@"isTouched = %@", self.isTouched ? @"YES" : @"NO");
        currentX = touchPoint.x;
        currentY = touchPoint.y;
        self.position = touchPoint;
        return YES;
    }
    // NSLog(@"isTouched = %@", self.isTouched ? @"YES" : @"NO");
    return NO;

}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint touchPoint = [touch locationInView:[touch view]];
    touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    if (isNotTouchActivated) {
        currentX = touchPoint.x;
        currentY = touchPoint.y;
        self.position = touchPoint;
    }
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    isNotTouchActivated = FALSE;
    //isTouched = NO;
}
-(void)MoveWithoutProblem{


    CGSize size = [[CCDirector sharedDirector] winSize];

    int MaxHeightofBubbles = 350;
    int minHeightofBubbles = 150;
    int RandomNumber = [self generateRandomNumberBetweenMin:minHeightofBubbles Max:MaxHeightofBubbles];
    float ConvertedRandom = [[NSNumber numberWithInt: RandomNumber] floatValue];

    int MaxWidthofBubbles = 0;
    int minWidthofBubbles = 900;
    int RandomNumber02 = [self generateRandomNumberBetweenMin:MaxWidthofBubbles Max:minWidthofBubbles];
    float ConvertedRandom02 = [[NSNumber numberWithInt: RandomNumber02] floatValue];

    startX = ConvertedRandom02;

    startY = ConvertedRandom;

    currentX = startX+myInt;
    currentY = startY;
    self.position = ccp(startX,startY);

    [self schedule:@selector(startMoving)];
}



-(void)startMoving{
    if (!isNotTouchActivated) {



        currentX+=bubblespeed;
        [self setPosition:ccp(currentX,currentY)];
    }

    if (self.position.x >= 1024+50) {
         //NSLog(@"off screen");

        isrestartscrolling = YES;
    }

    if (isrestartscrolling == YES) {
        //[self RandomYPOs];
        [self scheduleOnce:@selector(newRandomX) delay:0.2];
        isrestartscrolling = NO;
    }

}

@end

1 个答案:

答案 0 :(得分:1)

您的代码有点难以阅读,但您对触摸不起作用的直接问题是因为您假设您的图层(气泡)具有内容宽度和高度。如果你没有设置它,它就没有它,这就是你[sprite boundingBox]的注释掉行不起作用的原因。试试[sprite.BackBubble boundingBox]。向图层添加项目不会自动调整该图层的内容大小。

您可以尝试的另一件事是添加:

location = [sprite convertToNodeSpace:location];

如果您的泡泡层或后泡泡在任何位置移动,那么只需将后泡泡的位置添加到CGRect就不太可能。首先尝试第一个想法然后尝试这个,如果它不起作用。

希望这能帮助你娜塔莉。