drag on CCLayer sometimes working

ⅰ亾dé卋堺 提交于 2019-12-24 20:17:57

问题


Im having trouble to get the touches to work on a Custom class

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

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


DragItems = [[NSMutableArray alloc]initWithObjects:

             Bubble01,
             Bubble02,
             Bubble03,
             Bubble04,
             Bubble05,
             Bubble06,
             nil];

for(int i = 0; i < [DragItems count]; i++)
{
    sprite = (Bubble *)[DragItems  objectAtIndex:i];
    //sprite = (CCSprite *)[DragItems  objectAtIndex:i];
    //if(CGRectContainsPoint([sprite boundingBox], location))
        if(sprite.tag ==12 && CGRectContainsPoint([sprite boundingBox],location))
    {
        selectedSprite = sprite;
        //[self reorderChild:sprite z:BubbleDepthOntop];
    }
    }
  }


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

selectedSprite.position = ccp(location.x, location.y);
NSLog(@"Position: %f %f",location.x, location.y);
}

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


selectedSprite = nil;
isTouched = NO;

}

//little weird things... I can drag the sprites (6 bubbles) but but not every-time (they suddenly stall or if i drag my finger on an empty part of the screen a sprite suddenly jumps to that location from nowhere

I can drag but cant re-drag the same sprite I'm pretty new at this stuff so I guess I'm missing something simple, any help would be great

I have made a video of what is happening, using my old code, which has worked in other projects of mine - you'll see that the touches are working, but the object (Bubble*) is having trouble understanding that it is deselected. -

Normally I have only been using CCSprite, so I think that it is likely that im doing something wrong with the (Bubble*) class working with the touches

"selectedSprite = nil" isn't working

sometimes the custom class sprite can drag correctly, sometimes another (Bubble*) jumps to the current position of the touch (it should be impossible to drag my finger on an empty section of the screen and have a sprite appear there and yet ><....)

https://www.youtube.com/watch?v=VHaTpiVPP_w&feature=youtu.be

//custom sprite
#import "CCSprite.h"
#import "cocos2d.h"


@interface Bubble : CCLayer
{
CCSprite *BackBubble;
CCSprite *FrontShine;
CCRepeatForever *REP;
CCLabelTTF *BubbleLabel;
}


@property (nonatomic, strong)CCSprite *BackBubble;
@property (nonatomic, strong)CCSprite *FrontShine;
@property (nonatomic, strong) CCLabelTTF *BubbleLabel;

 -(id)initWithBubbleWithLabel:(NSString*)Bubblepng opacity:(float)myOpacty gloss:(NSString*)glossypng posX:(float)X posY:(float)Y data:(int)myNumber;

@end

////

@implementation Bubble
@synthesize BackBubble,FrontShine,BubbleLabel;


-(id)initWithBubbleWithLabel:(NSString*)Bubblepng opacity:(float)myOpacty gloss:(NSString*)glossypng posX:(float)X posY:(float)Y data:(int)myNumber
{
self=[super init];
{
    BackBubble = [CCSprite spriteWithSpriteFrameName:Bubblepng];
    BackBubble.position = ccp(X,Y);
    [self addChild: BackBubble z:5];


    FrontShine = [CCSprite spriteWithSpriteFrameName:glossypng];
    FrontShine.position =     ccp(BackBubble.contentSize.width/2,BackBubble.contentSize.height/2);
    [BackBubble addChild: FrontShine z:5];

    //BubbleLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",myNumber]] fontName:@"Grinched" fontSize:35];
    BubbleLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",myNumber]] fontName:@"Helvetica" fontSize:35];
    BubbleLabel.position = ccp(FrontShine.contentSize.width/2, FrontShine.contentSize.height/2);
    BubbleLabel.color = ccc3(255,255,255);
    [BackBubble addChild:BubbleLabel z:200];
    BubbleLabel.opacity =myOpacty;

}
return self;
}

the Bubble sprites are being being added to level01 layer as child then moved wth the following code:

 -(void)Scrolltick:(ccTime)ScrollTime
{
[self ScrollSprite:ScrollTime mySprite:Bubble01];
[self ScrollSprite:ScrollTime mySprite:Bubble02];
[self ScrollSprite:ScrollTime mySprite:Bubble03];
[self ScrollSprite:ScrollTime mySprite:Bubble04];
[self ScrollSprite:ScrollTime mySprite:Bubble05];
[self ScrollSprite:ScrollTime mySprite:Bubble06];
}
-(void)ScrollSprite:(ccTime)dt mySprite:(Bubble*)mySprite
{
int MaxHeightofBubbles = 350;
int minHeightofBubbles = 150;

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

mySprite.position = ccp(mySprite.position.x+BubbleSpeed,mySprite.position.y);
if (mySprite.position.x >= 1024+1)
{
    mySprite.position = ccp (0-[mySprite boundingBox].size.width,ConvertedRandom);  //mySprite.position.y+ConvertedRandom
    bubbleStartAgain = YES;
}
}

回答1:


instead of CGRectContainsPoint([sprite boundingBox],location))
use [self isTouchOnSprite:location:sprite]

-(BOOL) isTouchOnSprite:(CGPoint)touch:(CCSprite*)clip{
    CGPoint local = [self convertToNodeSpace:touch];
    Boolean b = CGRectContainsPoint([clip boundingBox], local);
    if (b) {
        return YES;
    }else {
        return NO;
    }
}

updated project link
let me know if this works .



来源:https://stackoverflow.com/questions/26388171/drag-on-cclayer-sometimes-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!