淡出被点击的CCSprite

时间:2014-06-29 08:55:35

标签: ios iphone objective-c cocos2d-iphone ccsprite

一旦我用cocos2d制作iOS游戏,我想淡出一个精灵。目前,sprite产生并且声明如下:

@implementation GameScene
{
    CCSprite *_shapeSprite;
}

然后我在addShape:方法

中每0.2秒出现一次
- (void)onEnter
{
    [super onEnter];
    [self schedule:@selector(addShape:) interval:0.2];
}

然而,我想在其中一个形状被敲击时才这样做,然后只有被敲击的形状消失了。到目前为止,我有这个:

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    _shapeSprite.opacity = 0;
}

然而,这只是删除了所产生的最后一个形状,而不是那个被点击的形状 - 有人可以请指出我在正确的方向吗?

1 个答案:

答案 0 :(得分:0)

试试这个,一定要有self.userInteractionEnabled = YES;在init

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLoc = [touch locationInNode:self];

    // detect Sprite
    for (CCSprite *mySprite in self.children) {

        float distance = pow(mySprite.position.x - touchLoc.x, 2) + pow(mySprite.position.y - touchLoc.y, 2);
        distance = sqrt(distance);
        if (distance <= 60) { //60 is radius from sprite center, change it
            // do something with this sprite
            mySprite.opacity = 0;

        }
    } 
}