Quickly refreshing NSViews
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
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?
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];
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
Steven
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...
Thanks,
Steven
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(){Steven
Ok, I refined the code a bit and put some more in to verify the validity of the selector:
The invoke() call works fine, but the timer never fires!
Thanks for any help,
Steven
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
In case it helps, this is the output my debug statements produce:
Steven
Quote:NSSelector doNextFrameThe NextFrame and Moved lines show that the doNextFrame() method was actually called.
public void SSView.doNextFrame()
NextFrame
Moved.
DrawRect
DrawRect
Created timer...true
Steven
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?]
[Edit: wait... you were the author of that thread. Did OSC's solution not work?]
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
The selector was valid, calling invoke() worked; but the timer never fired.
Steven
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.
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.
I said that, in the last thread. I assume that Steven's done it...
It still does nothing
I do wish that the Cocoa interfaces were more friendly...
Steven
I do wish that the Cocoa interfaces were more friendly...
Steven
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..:[
while 1.0/40.0 gives you 1/40...
and cocoa is VERY friendly interface... java on the other hand..:[
*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.
Thanks everyone for trying, but it still does nothing. I have the code to this:
Anything else to try?
Steven
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.

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
run and flee in mercy?
*Thinks, decides 'Why not?' ; screams and runs around; calms down; falls alseep*

Steven

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

