Working on a game, need some pointers on classes...noob question im sure =(

Discussion in 'iOS Programming' started by lolitsdaelan, Jul 31, 2009.

  1. lolitsdaelan macrumors newbie

    Jul 31, 2009
    Hey guys, I've been going at Objective-C and iPhone programming for a few days now and I feel as though I've gotten a decent grasp on it so far. Where I'm absolutely blanking right now, is with classes.

    Here's a quick example I made up to illustrate what I'm trying to do.

    Let's say that I have a cloud, and I want to create a raindrop and have it fall to the ground when the user taps the screen. I understand that I will need to create a raindrop class to get raindrop.h and raindrop.m, and I know how to create a new object, and I'm pretty sure I can even handle the user click, but where I'm having the problems is how do I assign all raindrops created from the raindrop class to an image?

    Really sorry if this has been asked a bunch or if it's painfully obvious and I'm overlooking it.

    Thanks in advance!

  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    What do you mean by this? Do all raindrops look alike or do they each look different? Is each one associated with one of a set of n images?

    Are these real raindrops or metaphorical raindrops?
  3. Kingbombs macrumors member

    Jun 24, 2009
    I think something you want is like:

    Make sure Raindrop subclass Image (think its UIImage)
    then create a new instance of that class and add it to a image created
    so Raindrop *foo = [[Raindrop alloc] initWithImage:bar.png];
    then something like self.image = foo;

    this is not quite right but maybe might help you along the right track
    (also self.image would mean you have defined image in the .h file)
  4. moral-hazard macrumors regular

    Jul 27, 2009
    Palo Alto, CA
    I don't think you need to make a class for what youre doing. Just initialize a plain vanilla UIView with a background image. You can apply an animation to move it down the screen.

    You want to assign the image to the view, not the view to the image.
  5. lolitsdaelan thread starter macrumors newbie

    Jul 31, 2009
    Thanks for the help guys. I think I'm starting to see what I need to do here :D

    I was just using the cloud/raindrop things as an example. Basically just trying to illustrate that I wanted to load multiple instances of an object, or i guess instances of a class to be more correct. Like another example, let's say every time the user taps the screen, it creates a basketball that will fall with gravity and bounce when it hits the floor/walls.

    The last time I've really done anything OOP related was with Java back in highschool :p
  6. moral-hazard macrumors regular

    Jul 27, 2009
    Palo Alto, CA
    You can just implement a function in the view or viewcontroller to receive touches:

    -(void) touchesBegan (NSSet *)touches withEvent(UIEvent*) event {
     ImageView *raindropView = [[ImageView alloc] initWithImage:...]; = [[touches anyObject] locationInView:...];
      //store this object in an array or something so you don't lose track of it. 
      //this will also retain the object, so we need to release from our intitial allocation
      [raindropView release];
      [view addSubview:raindropView];
      [view bringSubviewToFront:raindropView];
      //  do animation here
    This isn't "real code" but it should give you an idea what you need to do.

Share This Page