开发者

Cocos2D-iPhone: ShadowPaths ala Core Animation

In core animation it is possible to put a shadow round a CGPathRef, while maintaining very high graphics performance.

if (self.displayShadow)
{
    self.shadowLayer = [CALayer layer];

    self.shadowLayer.shadowPath = self.pOutline;
    self.shadowLayer.shadowOpacity = 1.0;
    self.shadowLayer.zPosition = -1.;
    self.shadowLayer.position = self.center;
    self.shadowLayer.shadow开发者_如何学编程Offset = CGSizeMake(+3., +3.);

    [self.layer addSublayer: self.shadowLayer];
}

How does it do this? And is it possible to get the same behaviour from Cocos2D?


Create a subclass of CCSprite and create a dark shadow sprite shaped like your sprite.

Create an instance of the subclassed CCSprite and add the shadow as a child to the sprite.

Override the "visit" method with:

-(void)visit{
    if(shadow!=NULL){
        int cx = self.contentSize.width / 2;
        int cy = self.contentSize.height / 2;

        float rot = (int)self.rotation%360 + 30;
        float px = cx + SHADOW_OFFSET*sinf(rot*M_PI / 180.0);
        float py = cy - SHADOW_OFFSET*cosf(rot*M_PI / 180.0);
        shadow.position = CGPointMake(px, py);
    }
    [super visit];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜