我无法接触定制类
-(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;
}
//奇怪的事情...... 我可以拖动精灵(6个泡泡),但不是每次都 (他们突然失速,或者如果我将手指拖到屏幕的空白部分,精灵突然从无处跳到那个位置
我可以拖动但不能重新拖动相同的精灵 我对这些东西很陌生,所以我想我错过了一些简单的东西,任何帮助都会很棒
我使用我的旧代码制作了一个正在发生的事情的视频,该代码在我的其他项目中有效 - 您会看到触摸工作正常,但对象(Bubble *)无法理解它被取消选择。 -
通常情况下,我只使用过CCSprite,所以我认为我很可能在使用触摸的(Bubble *)类做错了
&#34; selectedSprite = nil&#34;没有工作
有时自定义类精灵可以正确拖动,有时另一个(泡泡*)跳转到触摸的当前位置(应该不可能将手指拖到屏幕的空白部分并在那里出现精灵&GT;&LT; ....)
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;
}
泡泡精灵正在作为孩子添加到level01图层 然后移动了以下代码:
-(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;
}
}
答案 0 :(得分:1)
而不是CGRectContainsPoint([sprite boundingBox],location))
使用[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;
}
}
更新了项目link
如果有效,请告诉我。