Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lolitsdaelan

macrumors newbie
Original poster
Jul 31, 2009
3
0
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!

Daelan
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
how do I assign all raindrops created from the raindrop class to an image?

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?
 

Kingbombs

macrumors member
Jun 24, 2009
98
0
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)
 

moral-hazard

macrumors regular
Jul 27, 2009
197
3
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.
 

lolitsdaelan

macrumors newbie
Original poster
Jul 31, 2009
3
0
Thanks for the help guys. I think I'm starting to see what I need to do here :D

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?

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
 

moral-hazard

macrumors regular
Jul 27, 2009
197
3
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

You can just implement a function in the view or viewcontroller to receive touches:

Code:
-(void) touchesBegan (NSSet *)touches withEvent(UIEvent*) event {
 ImageView *raindropView = [[ImageView alloc] initWithImage:...];
  raindropView.center = [[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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.