Problem With Motion in NSView

Moderator
Posts: 1,560
Joined: 2003.10
Post: #16
[NSDate timeIntervalSinceReferenceDate]
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #17
I've been trying this for a while now and I can't figure out how that works exactly. I know that returns the time since Jan 1, 2001 and I could (assuming this is allowed) subtract two of those to get my elapsed time. Is that how it's supposed to be done? Here's what I've been trying (but with no luck):

(edit: I figured out that all I had to do was make them doubles instead of floats and it worked. Thanks for the help. I'm going to leave this here for others.)
Code:
//don't use floats, they don't work
double newTime,lastTime,elapsedTime;
...
- (void)checkKeys
{
    newTime = [NSDate timeIntervalSinceReferenceDate];
    elapsedTime = newTime - lastTime;
    lastTime = newTime;
    if(up)
        [myCharacter setY:[myCharacter y]+(5 * elapsedTime)];
    if(down)
        [myCharacter setY:[myCharacter y]-(5 * elapsedTime)];
    if(left)
        [myCharacter setX:[myCharacter x]-(5 * elapsedTime)];
    if(right)
        [myCharacter setX:[myCharacter x]+(5 * elapsedTime)];
    [self setNeedsDisplay:YES];
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Motion Sensor Query for 3D game iphone-query 1 4,484 Sep 2, 2011 09:26 PM
Last Post: PoseMotion
  Device motion detection using Acceleration data. RockyDilse 4 8,237 Feb 16, 2011 12:57 AM
Last Post: RockyDilse
  Mouse Events in NSView Chandhu 1 4,099 Feb 28, 2008 02:08 AM
Last Post: kuon_
  NSView loaded from bundle won't draw... Joseph Duchesne 1 3,474 Feb 12, 2006 03:33 AM
Last Post: Cochrane
  NSImage and NSView problems Joseph Duchesne 1 3,850 Aug 19, 2005 07:36 AM
Last Post: unknown