Iphone SDK:图像数组?
我正在制作一个“点击应用程序”,你必须通过点击僵尸来杀死它们。目前我无法取得任何进展,因为我无法正确使用我需要的方法。
我的应用程序是这样工作的:
我有一个每秒生成图像 3 次的计时器:
[NSTimer scheduledTimerWithTimeInterval:1.0/3
target:self
selector:@selector(Spawn)
userInfo:nil
repeats:YES];
然后我有 Spawn 命令:
- (void) Spawn {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release]
}
我还有一个“TouchesBegan”命令:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
}
我想要做的是将所有生成图像放入一个数组中;如果其中一张图像被触摸,我希望将其从超级视图中删除。我想知道数组如何工作以及如何从“TouchesBegan”等不同函数中删除添加到数组中的对象。
请帮助我解决我的问题!
编辑:
没关系...我在互联网上搜索,发现一些有用的资源几乎就是我所需要的。我终于弄清楚了它是如何工作的,并让我的生成器函数能够处理碰撞和“TouchesBegan”。
如果有人想要代码,请问我。
DD
I'm making a "Tapping-App" where you have to kill zombies by tapping on them. Currently i can't make any progress because i can't use the methods i need properly.
My App works like that:
I have a timer that spawns an image 3 times per second:
[NSTimer scheduledTimerWithTimeInterval:1.0/3
target:self
selector:@selector(Spawn)
userInfo:nil
repeats:YES];
then i have the Spawn command:
- (void) Spawn {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 109.0f);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
[myImage setImage:[UIImage imageNamed:@"myImage.png"]];
myImage.opaque = YES;
[self.view addSubview:myImage];
[myImage release]
}
I also have a "TouchesBegan" command:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
}
What I want to do, is put all the spawning images into an Array; If one of the images is touched i want it to be removed from the superview.I want to know how an array works and how i can remove objects added to the array from a different function like "TouchesBegan".
Please help me with my problem!
EDIT:
Nevermind... I searched around the internet and found some useful resources witch were almost what i needed. I finally figured out how it works and got my spawner function to work with collision and with "TouchesBegan".
If somebody wants the code, just ask me.
DD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的界面中使用 [NSMUtableArray addObject:] 和 [NSMutableArray removeObject:]
,类似于:
在您的实现中,初始化数组后,更改您的生成方法以包含该行
并在您的触摸处理程序中,在您确定哪个视图被触摸后,虽然
,考虑一下之后,您可能最好只创建 UIButton 实例而不是 UIIMageViews 并编写触摸处理程序来确定触摸了哪个视图。
Use [NSMUtableArray addObject:] and [NSMutableArray removeObject:]
In your interface, something like:
In your implementation, after initializing the array, change your spawn method to include the line
and in your touch handler, after you determine which view was touched, something like
Although, after thinking about it, you may be better off just creating UIButton instances instead of UIIMageViews and writing touch handlers to determine which view was touched.