我正在尝试创建一个程序,当用户触摸屏幕时,会出现一个精灵。但是,如果用户将手指放在该Sprite上,则精灵会变大,直到用户离开它为止。
现在,我在Cocos2d 1.x中创建了这个程序,它运行正常。但是,当我在2.x中尝试它时,精灵会被创建,但它不会帮助Sprite Grow。 代码如下:
-(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 :(得分:0)
确保您启用了触摸功能:
-(void)onEnter
{
[super onEnter];
self.touchEnabled = YES;
}
使用boundingBox获取rect。
- (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];
}
}