Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocos2d iphone Cocos2d_Cocos2d Iphone_Touch - Fatal编程技术网

Cocos2d iphone Cocos2d

Cocos2d iphone Cocos2d,cocos2d-iphone,touch,Cocos2d Iphone,Touch,我试图创建一个程序,当用户触摸屏幕时,会出现一个精灵。但是,如果用户将手指放在精灵上,精灵将变大,直到用户放开它 现在,我在cocos2d1.x中创建了这个程序,它运行得很好。然而,当我在2.x中尝试它时,精灵被创建了,但它不能帮助精灵成长。 代码如下: -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ CGPoint touchLocation = [self convertTouchToNodeSpace:t

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

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

}

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

请确保启用了触摸:

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

    }

}

非常感谢你!我想使用边界框要容易得多。