gpt4 book ai didi

iphone - CGRect 与多个 CGRect 的碰撞

转载 作者:行者123 更新时间:2023-11-28 22:58:00 25 4
gpt4 key购买 nike

我是 iPhone 编程的新手,目前正在开发 ballsplit 游戏。在这个游戏中,当我的子弹击中任何球时,我希望它产生两个新球。我的子弹和球都是uiimageview。

这是我的代码:

if ((CGRectIntersectsRect(bullet.frame,Ball.frame)) && !(ball)){
ball=TRUE;
x=Ball.frame.origin.x;
y=Ball.frame.origin.y;
[self performSelector:@selector(createball)];
}

这是我的创建球函数..

-(void)createball{
if (ball) {
imageMove1 = [[UIImageView alloc] initWithFrame:CGRectMake(x,y,50 ,50)];
UIImage *imag = [UIImage imageNamed:@"ball1.png"];
[imageMove1 setImage:imag];
[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];
[self.view addSubview:imageMove2];
[ballArray addObject:imageMove2];
ball=FALSE;
[self performSelector:@selector(moveball)];
}
}

现在,在创建这两个 uiimgeview 之后,当子弹击中这两个 uiimageview 之一时,我希望它创建另外两个 uiimageview。但是我面临的问题是我们如何获得这些新的 uiimageview 的框架......

最佳答案

将子弹迭代移动到存储球图像引用的数组后:

for (UIImageView *ball in ballArray){
//check for collision
if (CGRectIntersectsRect(bullet.frame,ball.frame)){
//hit
}
}

检查球之间的碰撞:

for (UIImageView *ballOne in ballArray){
for (UIImageView *ballTwo in ballArray){
//check for collision
if (CGRectIntersectsRect(ballOne.frame,ballTwo.frame) && ballOne != ballTwo){
//hit
}
}
}

关于iphone - CGRect 与多个 CGRect 的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10436429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com