1

我正在实现一种移动精灵的方法,我在放置所有精灵的地方放置了一个数组,并且我想为该数组提供触摸移动的动作。但是当我运行它时,什么也没有发生:

-(void)onEnter
{
    [super onEnter];
    self.isTouchEnabled = YES;
}

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    mSpriteOnHand = nil;

    for(CCSprite *cloth in mSpriteArray)
    {
        if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
    }
}


- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if(mSpriteOnHand)
        mSpriteOnHand.position = location;

}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    mSpriteOnHand = nil;
}

-(void)onExit
{
    [mSpriteArray release];
    mSpriteArray = nil;
    [super onExit];
}

有什么不对?我认为问题出在 Touch Began 上,但我不确定...

编辑:

我的精灵没有响应,但方法似乎没问题。这是我的初始化:

-(id) init
{
    if( (self=[super init]) )
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
        backGround.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:backGround z:0];

        cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
        cloth1.position = ccp(380, 500);
        [self addChild:cloth1 z:1];

        cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
        cloth2.position = ccp(530, 500);
        [self addChild:cloth2 z:2];

        cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
        cloth3.position = ccp(700, 500);
        [self addChild:cloth3 z:3];

        cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
        cloth4.position = ccp(380, 270);
        [self addChild:cloth4 z:4];

        cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
        cloth5.position = ccp(530, 270);
        [self addChild:cloth5 z:5];

        cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
        cloth6.position = ccp(700, 270);
        [self addChild:cloth6 z:6];

        [mSpriteArray addObject: cloth1];
        [mSpriteArray addObject: cloth2];
        [mSpriteArray addObject: cloth3];
        [mSpriteArray addObject: cloth4];
        [mSpriteArray addObject: cloth5];
        [mSpriteArray addObject: cloth6];
    }
    return self;
}
4

1 回答 1

1

如果这是从 CCSprite 派生的,它将无法工作。在这种情况下,使用:

-(void) onEnter {
    [[CCDirector sharedDirector].touchDispatcher addStandardDelegate:self priority:0];
    [super onEnter];
} 

-(void) onExit {
    [[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
    [super onExit];
}

编辑 1:

CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];

MPLOG(@"Touch at location %@", NSStringFromCGPoint(convertedLocation));

编辑 2:

在初始化中:

mSpriteArray = [[NSMutableArray array] retain]; // without ARC

在 dealloc 中:

-(void) dealloc {
    [mSpritesArray release];
    [super dealloc];
}

我没有足够的“ARC”知识来提出任何建议。此外,取决于“自我”是什么,即你有哪些孩子,你可能不需要一个数组,因为每个 CCNode 已经有一个。如下:

for (CCSprite* cloth in self.children) {        
    if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
}

编辑 3:排除背景:

一个 iVar:

int tag:UNIQUE_TAG_FOR_BACKGROUND;

在初始化中:

UNIQUE_TAG_FOR_BACKGROUND = 4367;  // pick a number 
[self addChild:backGround z:0 tag:UNIQUE_TAG_FOR_BACKGROUND];

在循环中:

for (CCSprite* cloth in self.children) { 
    if(cloth.tag == UNIQUE_TAG_FOR_BACKGROUND) continue;       
    if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
}
于 2013-02-21T19:56:45.627 回答