拖在CCLayer有时工作 [英] drag on CCLayer sometimes working
问题描述
我无法获得触摸在自定义类上工作
- (void)ccTouchesBegan:(NSSet *)触摸事件:(UIEvent *)事件
{
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
{
//移动被触摸的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气泡),但不是每次
(他们突然失速,或者如果我拖动我的手指在屏幕的一个空白部分突然跳跃从那里的位置
我可以拖动但不能重新拖动相同的sprite
我在这个东西很新,所以我想我错过了一些简单的东西,任何帮助将是伟大的
我已经做了一个视频,发生了什么,使用我的旧代码,这在我的其他项目中工作 - 你会看到触摸是工作,但对象(Bubble *)在理解它被取消选择时遇到麻烦 -
通常我只使用CCSprite,所以我认为这可能是im做错了有时候,使用触摸操作的(Bubble *)类
selectedSprite = nil无效
自定义类精灵可以正确拖动,有时另一个(Bubble *)跳转到当前位置的触摸(应该是不可能拖动我的手指在屏幕的空白部分,并有一个精灵出现在那里,然后><。 ...)
https://www.youtube.com/watch?v=VHaTpiVPP_w&feature=youtu.be
// custom sprite
#importCCSprite.h
#importcocos2d.h
@interface泡泡: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: ;
@end
////
@implementation泡泡
@synthesize BackBubble,FrontShine,BubbleLabel;
- (id)initWithBubbleWithLabel:(NSString *)Bubblepng opacity:(float)myOpacty gloss:(NSString *)glossypng posX:(float)X posY: (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:@GrinchedfontSize:35];
BubbleLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@%@,[NSString stringWithFormat:@%d,myNumber]] fontName:@HelveticafontSize: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;
}
}
CGRectContainsPoint([sprite boundingBox],location))
使用 [self isTouchOnSprite:location:sprite] c $ c>
- (BOOL)isTouchOnSprite:(CGPoint)touch:(CCSprite *)剪辑{
CGPoint local = [self convertToNodeSpace:touch];
Boolean b = CGRectContainsPoint([clip boundingBox],local);
if(b){
return YES;
} else {
return NO;
}
}
更新项目链接
让我知道这是否有效。
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;
}
}
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 .
这篇关于拖在CCLayer有时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!