私はゲームに取り組んでいます.以下のスクリーンショットは、私のゲームの全プロセスを示しています.
まず第一に、私のゲームが始まると、viewdidload で NStimer を使って赤いボールを上から下に動かします。
if ((CGRectIntersectsRect(missels.frame,Ball.frame))
{
[self performSelector:@selector(createball) ];
}
私のミッセルがボールを打つと、スクリーン ショットに示すようにボールが 2 つの部分に分割されます。これら 2 つの新しいボールのコードは次のとおりです。
-(void)createball{
imageMove1 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];
UIImage *imag = [UIImage imageNamed:@"ball1.png"];
[imageMove1 setImage:imag];
imageMove1.tag=i;
[self.view addSubview:imageMove1];
[ballArray addObject:imageMove1];
imageMove2 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];
UIImage *imag1 = [UIImage imageNamed:@"ball1.png"];
[imageMove2 setImage:imag1];
imageMove2.tag=k;
[self.view addSubview:imageMove2];
[ball1Array addObject:imageMove2];
[self performSelector:@selector(moveball) ];
}
その後、CAKeyframeAnimation を使用してこれらの新しいボールに動きを与えます。コードは次のとおりです。
-(void)moveball{
for (int i=0; i<[ballArray count]; i++) {
moveimg=[ballArray objectAtIndex:i];
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 15.0;
pathAnimation.repeatCount = 1;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, x+15, y);
CGPathAddQuadCurveToPoint(curvedPath, NULL, 20, 10, 100, 330);
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
moveimg.center=CGPointMake(x, y);
[moveimg.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
}
for (int k=0; k<[ball1Array count]; k++) {
move1img=[ball1Array objectAtIndex:k];
CAKeyframeAnimation *pathAnimation1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
pathAnimation1.calculationMode = kCAAnimationPaced;
pathAnimation1.fillMode = kCAFillModeForwards;
pathAnimation1.removedOnCompletion = NO;
pathAnimation1.duration = 15.0;
pathAnimation1.repeatCount = 1;
CGMutablePathRef curvedPath1 = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath1, NULL, x+40, y);
CGPathAddQuadCurveToPoint(curvedPath1, NULL, 350, 10, 380, 330);
pathAnimation1.path = curvedPath1;
CGPathRelease(curvedPath1);
move1img.center=CGPointMake(x, y);
[move1img.layer addAnimation:pathAnimation1 forKey:@"moveTheSquare"];
}
}
私のこの方法は、ミセルとボールの交差の後に最初の 2 つのボールを作成する場合にのみ機能します。その後、ボールが動き始めると、交差では機能しません。要するに、このゲームでは 2 つの主な問題に直面しています。
- ボールとミセルが交差するたびに 2 つの新しいボールを作成する方法。
これらのボールを作成した後、ミセルとの交差のために移動中にこれらのボールのフレームを取得する方法.ちょうどこのように..
if ((CGRectIntersectsRect(missels.frame,mynewballs.frame))
誰でもこの問題を解決するための最良のアイデアを提供できます。事前に感謝します。