精灵数组不响应触摸移动

时间:2013-02-21 19:07:17

标签: ios xcode ipad cocos2d-iphone

我正在实现一种移动我的精灵的方法,我把一个数组放在我放置所有精灵的地方,并且我希望通过触摸来移动该数组。但是当我运行它时,没有任何反应:

-(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;
}

1 个答案:

答案 0 :(得分: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:

在init中:

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;

在init中:

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;
        }
}