Drawing and moving rectangles in drawRect

Discussion in 'iOS Programming' started by teawithmilk, Oct 11, 2010.

  1. teawithmilk macrumors newbie

    Joined:
    Oct 11, 2010
    #1
    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?
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. teawithmilk thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #3
    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).
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  5. teawithmilk thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #5
    Can you give me a code example on how one would accomplish this?
     
  6. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    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?
     
  8. teawithmilk thread starter macrumors newbie

    Joined:
    Oct 11, 2010
    #8
    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.
     

Share This Page