Following the mouse

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I am currently working on a Pong game and I wanted the paddle to 'stick' to the cursor rather than use the keyboard. How do I accomplish this? How do I hide the cursor from view while over my GameView?

Here is a link to the project if you want to see it so far (really nothing).
http://www.angelfire.com/indie/nicks_spa..._Pong.sitx

If you want to give me any tips on displaying parts of the game, collisions, or other things useful to making a game that is like pong or table tennis it would be helpful.
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #2
to make the paddle stick to the mouse add the following to your view subclass:
Code:
- (NSPoint)mouseLoc {
    return [[self window] mouseLocationOutsideOfEventStream];;
}
then set your paddle location to NSMakePoint(paddlex,[view mouseLoc].y);

To hide mouse in the view add the following to your view subclass.
Code:
static NSCursor *hiddenCursor = nil;
static BOOL initialized = NO;
+ (void)initialize {
    if (!initialized) {
        initialized = YES;
        NSImage *cursor = [[NSImage alloc] initWithSize:NSMakeSize(16,16)];
        NSCachedImageRep *compositor = [[[NSCachedImageRep alloc] initWithSize:NSMakeSize(16,16)
                                                                         depth:32
                                                                      separate:YES
                                                                         alpha:YES] autorelease];
        [cursor addRepresentation:compositor];
        [cursor lockFocusOnRepresentation:compositor]; {
            [[NSColor clearColor] set];
            NSRectFill(NSMakeRect(0,0,16,16));
        } [cursor unlockFocus];
        
        hiddenCursor = [[NSCursor alloc] initWithImage:cursor
                                               hotSpot:NSMakePoint(0,0)];
    }
}


- (BOOL)isMouseVisible; { return mouseIsVisible; }
- (void)setMouseVisible:(BOOL)flag; {
    mouseIsVisible = flag;
    [[self window] invalidateCursorRectsForView:self];
}
- (void)resetCursorRects; {
    if (!mouseIsVisible) [self addCursorRect:[self visibleRect] cursor:hiddenCursor];
}
add instance variable:
Code:
BOOL mouseIsVisible;

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
Thanks for that. As a little side question, when defining declaring functions and defining them why do some use -(void) and some are +(void)?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
+ indicates a class method, while - indicates an instance method. Example:

Code:
@interface MyClass : NSObject

+ (void) classMethod;
- (void) instanceMethod;

@end

MyClass instance = [[MyClass alloc] init];

[MyClass classMethod];
[instance instanceMethod];

Class methods are not called on any particular instance of the class. They're usually used for functions associated with the class that do not require an actual instance, or functions (such as alloc) that create an instance. Instance methods, such as init, called on a particular instance of the class.

Alex Diener
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
Thanks for that little tidbit. Just to be sure I have a grasp on this, if I wanted to change the color of one instance of a class, I would use the - whereas if I wanted to change the color of all of the instances of a class, I would use the + . Is that correct?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #6
Not really. Such a thing might be possible if in MyClass.m, you kept a static variable that contained a list of all current instances of that class. However, that wouldn't in general be safe, and I can't really imagine a situation where you'd want to do something like that.

A + method really isn't much different from a vanilla C function. It's just a function that does "something", and in a way is only loosely associated with the class that defines it. It might create or access instances of its class, but it doesn't necessarily have to. I think a + method has access to a special variable of type Class (similar to self in - methods), but I don't remember what it's called. Also note that + methods are completely separate from - methods; if I'm not mistaken, you can even have a + method and a - method with the same name.

Hope that clears things up a bit...

Alex Diener
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #7
Class methods (+) are usually reserved for accessing instances. +instanceNamed, +sharedInstance, +defaultInstance, etc. Or setting the parameters of singleton class. Remember that ObjC classes are instances of the root ObjC class-class, so + methods are really methods that work in that context.

The +initialize method is a special class-subclass method of the root class-class. It is called when the class is initialized (not the instance), and can be used to define the parameters of the class and it's instances. I used it in my code to create a static instance of the blank cursor which can be accessed by all instances of the view subclass, since it is a waste to create a new blank cursor for each instance of the view subclass.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
borges
Unregistered
 
Post: #8
Thank you for this information, and thank you SimReality/Nick for consistently being one question ahead of me! Smile
Quote this message in a reply
Post Reply