Become a MacRumors Supporter for $25/year with no ads, private forums, and more!
  • Did you order new AirTags? We've opened a dedicated AirTags forum.

snukid

macrumors newbie
Original poster
Jan 7, 2012
4
0
Hello, everyone!
I'm trying to make a simple game. And I'm stuck on a simple thing. I need to create many UIImageViews(bullets jr something like that), shoot them and after that check their collision whith my hero. I serched, how to do that and I understood, that I have to put them in an MutableArray, but I haven't programmed for a while and so I need help.

Now to the thing, I made a NSTimer in the ViewDidLooad, that calls the method in wich I create the UIImageViews but the problem is that I need to create the UIImage and the MutableArray only once, so I made them in the ViewDidLoad but the UIImageView can't find the UIImage. This is the proble. Ineed help. I'd really appretiate it if someone would help me.

Thank you very much!
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,130
5,101
It's very difficult to help someone fix their code when they don't share the code that needs fixing...
 
Comment

snukid

macrumors newbie
Original poster
Jan 7, 2012
4
0
Well, sorry about that!

I'm using my iPad and I do not have a wifi for my MacBookPro, so I'll write everything again :).
This is onely the part, I have to get fixed:
Code:
-(void)createEnemy
{
randomPos = random() % 974;
UIImageView *bombImgView = [[UIImageView alloc] initWithImage:bullet];
[bombImgView setFrame:CGRectMake(200, 30, 30, 30)];
[self.view addSubView:bombImgView];
}

-(void)viewDidLoad
{
UIImage *bullet = [UIImage imageNamed:@"bomb.png"];

[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(createEnemy) userInfo:nil repeats:YES];

[super viewDidLoad];
}

Sorry, if there are some spelling mistakes, I typed it again.:)
 
Comment

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
What is the scope of your bullet variable? If you don't understand what scope means, it's probably time to take a step back and learn some programming fundamentals.
 
Comment

snukid

macrumors newbie
Original poster
Jan 7, 2012
4
0
Answer

Well, I do not know the lexicon but I think I do understand what that means. I know, why the UIImageView cann't load the image, but I do not whant to each time create a new UIImage. And here, I do not know, how to do that.:(
 
Comment

admanimal

macrumors 68040
Apr 22, 2005
3,531
2
Then you'll need to make bullet an instance variable of the class and make sure it is properly memory managed. In reality, this is probably a premature optimization, especially since UIImages are cached when loaded using [UIImage imageNamed:]. You should still know how to make an instance variable anyway.
 
Comment

snukid

macrumors newbie
Original poster
Jan 7, 2012
4
0
Help me, please

Oh, thank you very much, I figured it out. Thank you!
But now I have an enother question. I'll tomorrow go back home from my dacha and I'll send you my code and ask my question.
Again thank you for your help!
 
Last edited:
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.