I need help with my really small and easy game.

Discussion in 'iOS Programming' started by snukid, Jan 7, 2012.

  1. snukid macrumors newbie

    Joined:
    Jan 7, 2012
    #1
    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!
     
  2. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #2
    It's very difficult to help someone fix their code when they don't share the code that needs fixing...
     
  3. snukid thread starter macrumors newbie

    Joined:
    Jan 7, 2012
    #3
    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.:)
     
  4. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #4
    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.
     
  5. snukid thread starter macrumors newbie

    Joined:
    Jan 7, 2012
    #5
    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.:(
     
  6. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #6
    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.
     
  7. snukid, Jan 7, 2012
    Last edited: Jan 7, 2012

    snukid thread starter macrumors newbie

    Joined:
    Jan 7, 2012
    #7
    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!
     

Share This Page