首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拖动CCLayer有时工作

拖动CCLayer有时工作
EN

Stack Overflow用户
提问于 2014-10-15 17:08:55
回答 1查看 51关注 0票数 0

我很难在一个自定义类上工作

代码语言:javascript
复制
-(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*)类做了错误的操作。

"selectedSprite = nil“不起作用

有时自定义类sprite可以正确拖动,有时另一个(Bubble*)跳到触摸的当前位置(不可能将我的手指拖到屏幕的一个空部分上,让一个sprite出现在那里,而><.)

w&feature=youtu.be

代码语言:javascript
复制
//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

/

代码语言:javascript
复制
@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层:

代码语言:javascript
复制
 -(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;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-20 05:29:28

而不是CGRectContainsPoint([sprite boundingBox],location))

使用[self isTouchOnSprite:location:sprite]

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

更新项目链接

如果这有用的话请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26388171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档