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

teawithmilk

macrumors newbie
Original poster
Oct 11, 2010
4
0
I am trying to draw a group of rectangles (as part of a space invaders-style game). So what I've done so far is to create a struct of the coordinates, and status (destroyed or alive).

After reading some documentation, I noticed that drawing will be inside of the drawRect method (that xcode creates for you if the class inherits from UIView).

What I can't figure out is how to get the rectangles to move (left to right/top to bottom/whatever) and update the view every single time.

1.) Should I create an instance variable (of type Struct) to send to the drawRect: method? If so, how would I do this?

2.) Each time I update the rectangle (by moving it or removing it after a missile hits it), do I need to call setNeedsDisplay?
 
1) How you deal with that is up to you. An array of rects might make sense. You cannot pass it to drawRect. The normal thing would be to either add an instance variable to the view that you can set or provide it was access to your data model.

2) Yes.
 
I've gotten to where I'm adding each image as a subview to the view. How would I go about animating (moving them back and forth) them?

I plan on implementing hit-detection with CGRectIntersectsRect (or any other method of hit detection).
 
Can you give me a code example on how one would accomplish this?

Nope. I don't do "code examples", "tutorials" or "sample code". From experience of many, many posters on here when I do so they simply copy the code, don't read it, don't understand it and learn nothing.

Programming is a process of taking a problem, breaking it down to simpler and simpler processes until you can solve each step at which point you build it back up.

So in this case what is animation? Animation is the process of moving objects over time. So how to we do something every so often? A quick look at the documentation would show us a useful sounding class: NSTimer. So if we combine that with setting the location of a view (the frame) we'll get what we want.

That should be more than you need
 
Can you give me a code example on how one would accomplish this?

Why aren't you using a book that teaches you about animation?

By making things up yourself, you effectively have a beginner teaching another beginner how to do animation. Would you take a class from a complete beginner, or read a book written by a beginner?
 
Yup, that's what I just got done implementing, an NSTimger. Now I'm trying to figure out if I should just tag the subviews and then remove them one by one and redraw them in the NSTimer.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.