2012-12-23 32 views
1

我想創建一個程序,當用戶觸摸屏幕時,會出現一個精靈。但是,如果用戶將手指放在該Sprite上,則該精靈會變得更大,直到用戶放開它。Cocos2d CCTouchBegan

現在,我在Cocos2d 1.x中創建了這個程序,它工作正常。但是,當我在2.x中嘗試它時,精靈將被創建,但它不會幫助精靈成長。 代碼如下:

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 

redBall = [CCSprite spriteWithFile:@"Circle.png"]; 
redBall.position = ccp(touchLocation.x, touchLocation.y); 
redBallRect = CGRectMake(redBall.anchorPoint.x, redBall.anchorPoint.y, redBall.contentSize.width, redBall.contentSize.height); 

[self addChild:redBall]; 




if (CGRectContainsPoint(redBallRect, touchLocation)) { 
    NSLog(@"Hello"); 
    growForever = [CCRepeatForever actionWithAction: [CCScaleBy actionWithDuration: .5 scale: 1.2]]; 
    [growForever setTag:1]; 
    [redBall runAction:growForever]; 

} 

return YES; 

} 

可能是什麼問題,我該如何解決呢?

回答

0

確保您啓用觸摸:

-(void)onEnter 
{ 
    [super onEnter]; 

     self.touchEnabled = YES; 
} 

使用boundingBox的得到矩形。

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


    if(CGRectContainsPoint([redBall boundingBox], touchLocation)) 
    { 
     NSLog(@"Hello"); 
     growForever = [CCRepeatForever actionWithAction: [CCScaleBy actionWithDuration: .5 scale: 1.2]]; 
     [growForever setTag:1]; 
     [redBall runAction:growForever]; 

    } 

} 
+1

非常感謝!我想使用BoundingBox要容易得多。 –