1

我正在尝试创建一个程序,当用户触摸屏幕时,会出现一个精灵。但是,如果用户将手指放在该 Sprite 上,则该 sprite 会变大,直到用户放开它。

现在,我在 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;

}

可能是什么问题,我该如何解决?

4

1 回答 1

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

    }

}
于 2012-12-23T07:31:01.820 回答