如何在 Objective-C 中创建和维护同一对象的多个?

发布于 2024-11-16 22:03:29 字数 981 浏览 2 评论 0原文

我正在尝试让多个 Ball 对象在屏幕上形成并弹跳。我已经让它工作得很好,但我在添加更多时遇到问题。理想情况下,球的数量会有所不同,但现在我试图将两个放入一个数组中以更改其余的代码。

我的问题是一切都编译并运行良好,但我仍然只有一个球。这就是我怀疑问题出现的地方。

我也觉得在 viewDidLoad 中这样做是不正确的,但我不确定它实际上属于哪里。

viewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];

balls = [[NSMutableArray alloc] init];
CGPoint newPos = CGPointMake(5,5);

for(int i = 0; i < 2; i++) {
[balls addObject:[[[Ball alloc] init] autorelease]];
}

[[balls objectAtIndex:1] setPosition:newPos];

[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

- (void)onTimer
{
for(int i = 0; i < [balls count]; i++) {
    Ball *b = [balls objectAtIndex:i];
    [b update];
    [(BallView *)self.view refresh:b];
    }
}


- (void)refresh:(Ball *)aBall {
ball = aBall;
[self setNeedsDisplay];

}

我已经添加了 onTimer,我认为添加创建球的延迟就足以使这不再是问题。所有更新所做的就是根据加速度计/与屏幕边缘的碰撞来更改速度/方向。

I'm trying to get multiple Ball objects to form on screen and bounce around. I have it working fine for one, but I am having problems adding more. Ideally, the amount of balls will vary, but for now I am trying to just get two into an array to change the rest of the code.

My problem is that everything compiles and runs fine, but I still only have one ball. This is where I suspect the problem arises.

I also feel like doing this in viewDidLoad is incorrect, but I'm not sure where it actually belongs.

viewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];

balls = [[NSMutableArray alloc] init];
CGPoint newPos = CGPointMake(5,5);

for(int i = 0; i < 2; i++) {
[balls addObject:[[[Ball alloc] init] autorelease]];
}

[[balls objectAtIndex:1] setPosition:newPos];

[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

}

- (void)onTimer
{
for(int i = 0; i < [balls count]; i++) {
    Ball *b = [balls objectAtIndex:i];
    [b update];
    [(BallView *)self.view refresh:b];
    }
}


- (void)refresh:(Ball *)aBall {
ball = aBall;
[self setNeedsDisplay];

}

I've added my onTimer, I thought adding the delay in creating the balls would be enough to make this a nonissue. All update does is change the velocity/direction based on the accelerometer/collisions with the edge of the screen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-11-23 22:03:29

事实上,两个球都应该被展示。检查它们的坐标以查看它们是否重叠。另一方面,您似乎泄漏了 balls 数组以及 ball1 和 ball2 对象。您正在分配初始化它们,因此必须在 viewDidLoad 结束时释放它们,或者,如果您使用保留属性,只需在设置它们时自动释放它们,以便它们被属性保留。

至于代码应该位于哪里,viewDidLoad 应该没问题,但请考虑将 balls 数组放入您调用来创建 ViewController 的 init 方法中,因为如果没有设置该数组,您的 ViewController 可能无法工作。

Indeed both balls should be displayed. Check their coordinates to see if they overlap. On another note, it appears you are leaking the balls array, as well as the ball1 and ball2 objects. You are alloc init'ing them so you have to release them at the end of viewDidLoad, or, if you are using retain properties, just autorelease them when you set them so that they get retained by the properties.

As for where the code should be located, viewDidLoad should be ok, but consider putting the balls array into the init method you call to create your ViewController, since there's probably no way your ViewController could work without that array being set up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文