Cocoa sprite/canvas/key input help

Discussion in 'Mac Programming' started by Gandolfmatt, Jul 18, 2008.

  1. Gandolfmatt macrumors member

    Joined:
    Jul 18, 2008
    #1
    Hey, I'm new to this forum and to Cocoa, but I have really gotten into it. I can do most things, but I just need some help with a few NS commands.

    (1) I'm creating a canvas in which a sprite will be able to move around and have animation. Which is the best, NSView or NSImageView?

    (2) What are the NS commands for sticking a sprite from resources onto the canvas, and to make a color of it transparent? Also if there are any other pict commands.

    (3) How would I get Xcode to listen for a key down, and if so, activate some code?

    (4) Lastly, what are all the string commands? Like, to get the string count, mid, nthfield, or anything like that.

    Thanks,
    -Matt
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    1) I don't know, abstain.
    2) I don't know, abstain.
    3) Do you mean how to get XCode to do some replacement or something in your code? Like some sort of Macro? Or do you mean how do you listen for a keypress event in a Cocoa application?
    4) http://developer.apple.com/document...lasses/NSString_Class/Reference/NSString.html
    There is a message
    Code:
    -length
    . I don't know what mid means. There is a message
    Code:
    -characterAtIndex
    to get a character at a particular position. Not sure if this is what you mean by nthfield, but maybe this is it. If you want to tokenize the string there is
    Code:
    -componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
    Hopefully others can help with the rest.

    -Lee
     
  3. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #3
    Abstain?

    Keypress event.

    Thanks :)

    Code:
    MID$(source, start, length)
    Returns the characters starting from "start" onward in "source". Length is optional, specifies the number of characters to return.
    
    NTHFIELD$(source, separator, fieldNumber)
    Returns the field value from the source that precedes the fieldNumber occurrence of the separator in the source.

    Thanks lee1210,
    -Gandolf
     
  4. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #4
    Does anyone know the answer for questions 1,2, and 3?


    Thanks,
    -Matt
     
  5. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #5
    There isn't any short answer to that question! ;)

    First, you probably need to choose which graphics API/framework to use, so I'd recommend reading this.

    Quartz 2D sounds best suited to your needs, IMO. You can draw sprites to bitmaps (or even better, layers), then copy to screen as and where needed.
    Try here.


    As for keyboard input, I've never written a Cocoa game, but I'd guess you'd have to subclass either the application or window class, then implement the following methods as required:

    - (void)keyDown: (NSEvent *)theEvent;
    - (void)keyUp: (NSEvent *)theEvent;
    - (void)flagsChanged: (NSEvent *)theEvent;
     
  6. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #6
    Thanks. :) All my questions are just about answered.

    I will research more on what you wrote, if I have any more questions, I will post here.


    Thanks,
    -Gandolf
     
  7. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #7
    There aren't too many tuts about quartz and most of them are stuffed with extra features that makes your brain fry when you look at the code. So, I'm going to take a look at Open Gl. Open Gl will be way faster and have 3D support.


    Thanks,
    -Matt
     
  8. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #8
    Alright, I've been researching into this big time and it seems good, except for one small problem.

    I can't find any tuts on how to stick a pic from my resources into OpenGl. To make 2D pics instead of 3D.

    Anyone know how?


    -Matt
     
  9. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #9
    I'm not that familiar with OpenGL, so can't help you much, sorry.

    If you want to go the Quartz2D route, there's a guide here on drawing with CGLayers here, with some example code. It's good for quick offscreen sprite drawing.
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    I lost track of this thread, just saw this...

    abstain, verb: "to refrain deliberately and often with an effort of self-denial from an action or practice <abstain from drinking>"

    Looks like someone covered this.

    for NTHFIELD:
    Code:
    componentsSeparatedByCharactersInSet:
    should do this for you.

    for MID:
    Code:
    – substringFromIndex:  
    – substringWithRange:
    The start would be the parameter to substringFromIndex. The NSRange for substringWithRange would have the location field set to start, and the length field set to length.

    -Lee
     
  11. Gandolfmatt thread starter macrumors member

    Joined:
    Jul 18, 2008
    #11
    Everything is running good so far. I will continue to add things and will post when I run into a problem.


    Thanks guys,
    -Gandolf
     

Share This Page