我正在开发一款游戏,以发现两张图片之间的差异。
我想在发现它时添加效果。画出一个圆圈会比突然显示圆圈要好得多。但我以前从未做过核心动画或opengl。
我不认为准备100个精灵并逐帧更改精灵是一个好主意。 这是我的代码:(只需在左右图像上添加圆形图像。)
-(void) show {
CCSprite* leftCircle = [CCSprite spriteWithFile:@"circle.png"];
CCSprite* rightCircle = [CCSprite spriteWithFile:@"circle.png"];
leftCircle.scaleX = size.width / [leftCircle boundingBox].size.width;
leftCircle.scaleY = size.height / [leftCircle boundingBox].size.height;
rightCircle.scaleX = size.width / [rightCircle boundingBox].size.width;
rightCircle.scaleY = size.height / [rightCircle boundingBox].size.height;
leftCircle.anchorPoint = ccp(0, 1);
rightCircle.anchorPoint = ccp(0, 1);
leftCircle.position = leftPosition;
rightCircle.position = rightPosition;
[[GameScene sharedScene] addChild:leftCircle z: 3];
[[GameScene sharedScene] addChild:rightCircle z: 3];
shown = YES;
}
那我该如何实现呢?如果你能提供一些源代码,那就太棒了。
答案 0 :(得分:2)
作为一种简单的方法,我可以建议您创建一个圆并将其缩放为零。然后创建一个CCScale操作并运行它。它会给你一个不断增长的圈子。这是代码:
CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"];
[sprite setScale:0.01];
id scale = [CCScale actionWithDuration:0.3 scale:1];
[sprite runAction:scale];
可以使用的其他动作是CCFadeIn。您可以在创建后使您的精灵不可见并使其淡入:
CCSprite *sprite = [CCSprite spriteWithFile:@"mySprite.png"];
[sprite setOpacity:0];
id fade = [CCFadeIn actionWithDuration:0.3];
[sprite runAction:fade];
您也可以将这些操作结合起来:
[sprite runAction:fade];
[sprite runAction:scale];
你也可以把它做大(例如设置比例3)和透明。并使其淡入并缩小以突出显示您的图像
要通过绘图效果绘制圆形,您可以从小部分(弧形)创建圆形,然后从它们构建圆形。我认为如果你在添加时不要让部件可见,那也会很酷,但要让它淡入。我的意思是这样的:
-(void) init
{
NSMutableArray *parts = [[NSMutableArray array] retain]; //store it in your class variable
parts_ = parts;
localTime_ = 0; //float localTime_ - store the local time in your layer
//create all the parts here, make them invisible and add to the layer and parts
}
-(void) update: (CCTime) dt //add update method to your layer that will be called every simulation step
{
localTime_ += dt;
const float fadeTime = 0.1;
int currentPart = localTime_ / fadeTime;
int i = 0;
for (CCSprite *part in parts)
{
//setup the opacity of each part according to localTime
if (i < currentPart) [part setOpacity:255];
else if (i == currentPart)
{
float localLocalTime = localTime - i*fadeTime;
float alpha = localLocalTime / fadeTime;
[part setOpacity:alpha];
}
++i;
}
}
答案 1 :(得分:1)
创建所需的效果相当简单。 您可以通过设置CAShapeLayer的strokeEnd来实现它。
以下是详细信息:
创建一个路径,然后分配给CAShapeLayer
UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100.0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RADIANS(270.01) clockwise:NO];
circle = [CAShapeLayer layer];
circle.path = circlePath.CGPath;
circle.strokeColor = [[UIColor blackColor] CGColor];
circle.fillColor = [[UIColor whiteColor] CGColor];
circle.lineWidth = 6.0;
circle.strokeEnd = 0.0;
[self.view.layer addSublayer:circle];
将strokeEnd设置为隐式设置圆形绘图的动画
circle.strokeEnd = 1.0;
就是这样!将自动为您绘制圆圈;)。以下是使用GestureRecognizer触发动画的代码。将其复制到“Single View Application”类型的空项目中,并将其粘贴到ViewController。
#define DEGREES_TO_RADIANS(angle) ((angle) / 180.0 * M_PI)
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIBezierPath* circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100.0, 100.0) radius:80.0 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RADIANS(270.01) clockwise:NO];
circle = [CAShapeLayer layer];
circle.path = circlePath.CGPath;
circle.strokeColor = [[UIColor blackColor] CGColor];
circle.fillColor = [[UIColor whiteColor] CGColor];
circle.lineWidth = 6.0;
circle.strokeEnd = 0.0;
[self.view.layer addSublayer:circle];
// add a tag gesture recognizer
// add a single tap gesture recognizer
UITapGestureRecognizer* tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
[self.view addGestureRecognizer:tapGR];
}
#pragma mark - gesture recognizer methods
//
// GestureRecognizer to handle the tap on the view
//
- (void)handleTapGesture:(UIGestureRecognizer *)gestureRecognizer {
circle.strokeEnd = 1.0;
}