Quickly refreshing NSViews

Member
Posts: 370
Joined: 2002.04
Post: #1
I am trying to create something akin to REALbasic's SpriteSurface in Cocoa/Java. I have a sprite moving diagonally across the screen, and I am pretty sure that the underlying code is at least ok. One problem left is that many frames are 'dropped' and the sprite moves unpredictably in 'jumps'.
Right now I'm using a NSTimer to have the view refresh itself, but this isn't working very well. Is there a faster way? (Preferably synchronous)
Thanks a bunch,
Steven

Did you ever wonder why we had to run for shelter when the promise of a brave new world unfurled beneath the clear blue sky?
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #2
NSTimer is very reliable and you can easily set it to 40fps, or 60fps... of course I'm assuming you did it with 0 as the parameter... if you do that, then you must make your game time based and not frame based. if your game IS frame based, I suggest:
Code:
[NSTimer scheduledTimerWithTimeInterval: (1.0/40.0)target:self selector:@selector(drawnext: ) userInfo:nil repeats:YES];
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #3
Well I must be doing something wrong then, as it doesn't work right. And, as I said, I am using Java. How does that call translate to Java? I would do it myself, but I don't get how the selector thingy works.
Steven
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #4
I got it; it is very similar to java.lang.reflect.Method.
Now, my problem is that nothing happens. These are the significant portions of my code...

Code:
timer = new NSTimer(1/40,this,new NSSelector("doNextFrame"),"test",true);

Code:
public void doNextFrame(){
Thanks,
Steven
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #5
Ok, I refined the code a bit and put some more in to verify the validity of the selector:
Code:
public void run(){
        NSSelector s;
        s = new NSSelector("doNextFrame",new Class[0]);
        System.out.println(s);
        try{
            System.out.println(s.methodOnObject(this));
        }catch(Exception e){
            System.out.println(e);
        }
        try{
        s.invoke(this,null);
        }catch(Exception e){
            System.out.println(e);
        }
        timer = new NSTimer(1,this,s,"test",true);
        System.out.println("Created timer..."+timer.isValid());
    }


The invoke() call works fine, but the timer never fires!
Thanks for any help,
Steven
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #6
In case it helps, this is the output my debug statements produce:

Quote:NSSelector doNextFrame
public void SSView.doNextFrame()
NextFrame
Moved.
DrawRect
DrawRect
Created timer...true
The NextFrame and Moved lines show that the doNextFrame() method was actually called.
Steven
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #7
There was *just* a discussion about this... search the forums.

[Edit: wait... you were the author of that thread. Did OSC's solution not work?]
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #8
I forgot all about that... I just tried that (having one argument, an Object) but the same results happened:
The selector was valid, calling invoke() worked; but the timer never fired.
Steven
Quote this message in a reply
anarchie
Unregistered
 
Post: #9
In Cocoa-Java, it appears you have to add the NSTimer to the current NSRunLoop yourself, like so:

NSRunLoop.currentRunLoop().addTimerForMode(myTimer, DefaultRunLoopMode);

To remove the timer from the runloop, just invalidate it:

myTimer.invalidate();

Don't take my word for it, though; I have never used Cocoa-Java, and this is only what I gleaned from the documentation.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #10
I said that, in the last thread. I assume that Steven's done it...
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #11
It still does nothing Mad
I do wish that the Cocoa interfaces were more friendly...
Steven
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #12
just so you know... 1/40 give you 0....
while 1.0/40.0 gives you 1/40...
and cocoa is VERY friendly interface... java on the other hand..:[
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #13
*Whacks himself*

And, yes Java's APIs are no prettier. I wish that everything was more like REALbasic (minus the slowness, bugginess, and huge upgrade fees every six months) :[

Ok, off to fix that.

{Back}

Still doesn't work. Mad
Thanks everyone for trying, but it still does nothing. I have the code to this:
Code:
public void run(){
        NSSelector s;
        s = new NSSelector("doNextFrame",new Class[] {Object.class});
        System.out.println(s);
        try{
            System.out.println(s.methodOnObject(this));
        }catch(Exception e){
            System.out.println(e);
        }
        try{
            s.invoke(this,new Object[] {null});
        }catch(Exception e){
            System.out.println(e);
        }
        timer = new NSTimer(1.0/40.0,this,s,null,true);
        System.out.println("Created timer..."+timer.isValid());
        NSRunLoop.currentRunLoop().addTimerForMode(timer, NSRunLoop.DefaultRunLoopMode);
        System.out.println("Added to run loop "+NSRunLoop.currentRunLoop());
    }


Anything else to try?
Steven
Quote this message in a reply
Moderator
Posts: 916
Joined: 2002.10
Post: #14
run and flee in mercy?
Quote this message in a reply
Member
Posts: 370
Joined: 2002.04
Post: #15
*Thinks, decides 'Why not?' ; screams and runs around; calms down; falls alseep*

Wink
Steven
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Quickly determining if a closed polyline is clockwise or counter-clockwise TomorrowPlusX 6 8,934 Apr 9, 2011 04:03 PM
Last Post: Skorche
  Refreshing NSViews Steven 13 5,107 Oct 26, 2002 04:08 PM
Last Post: OneSadCookie