Game Development Community

iphone jerky movement

by Jason Oda · in iTorque 2D · 05/15/2009 (4:24 pm) · 3 replies

I understand that the iphone has a lot less of a capacity to handle intensive things going on when compared to my mac...but I am encountering jerky animation when trying to build the simplest of games. I'm not sure it's just the way I'm setting things up (bad coding practices) or what.

In my game, the player stays in the middle and as I tilt the phone, the background scrolls...however, the background is composed of a green bar at the bottom (which doesn't move) and a small tree (that does move). So all that's happening is a small tree is moving. This is kinda jerky and lame looking on my iphone.

So I have a few questions:

1. I have found something like this:

new t2dSceneGraph(t2dScene);
sceneWindow2D.setSceneGraph( t2dScene );
t2dScene.setScenePhysicsFPSActive(1);
t2dScene.setScenePhysicsTargetFPS(30);

which I just stick my game.cs file. It's supposed to make the game run at 30 fps (hopefully), but when I try to test it by changing the 30 to a 3 (or looking at the fps in the debugger) it doesn't seem to have any effect. Why isn't this working? ...and is this a proper solution to the problem?



2. I am coming from flash, and perhaps I've adapted some coding practices that are not effective in TGB:

I generally do as2 flash stuff and so I've set my game up like this (below isn't perfect code, just a representation to give you an idea):

the scenegraph class is: root

function root::onLevelLoaded(%this){
$action="game";
%this.engine();
}

function root::engine(%this){

//set the game so it runs at 30 FPS

%this.schedule( (1000/30), "engine" );

if($action$="game"){

if($xspeed>=0.2){
tree.setLinearVelocityX(25);
}else{
tree.setLinearVelocityX(0);
}

}

}

function joystickMoveX(%val)
{

if( %val < $joyThreshold && %val > -$joyThreshold ) {
$xspeed = 0;
} else {
$xspeed = %val;
}


}

obviously there is some code here that I haven't included for brevity...so basically, what this code says is check 30/second if the phone is leaning and move the tree if it is.

...is this bad coding practice? I know there are many ways to do stuff, but this is just the most intuitive for me. This is just how i would structure it in flash. What would be a better way? and would this better way make the game more smooth?

3. Any other ideas?

Thank you very much,

Jason


#1
05/15/2009 (6:17 pm)
A few things to check....

1.) Do you have a lot of objects in your scene? The more objects you have, the lower the framerate will become.

2.) Are you dynamically creating and deleting objects? If so, I would suggest rewriting this to use a cache of pre-created objects (created at runtime) and dynamically repurposing them as needed.

3.) Add some echo statements to your code, and watch the debugger as you play. Check to see the things that scroll up frequently, or seem to take 'a second too long' to scroll up. Anything that fits those rules you should move into the C++ engine code, as anything there is going to run much faster.

4.) If you are dynamically loading and unloading lots of images, this can also cause slowdowns.

Just a few hints I've picked up along the way... hopefully something there will stick out for you!
#2
05/16/2009 (12:59 am)
It looks like you've got your own main loop of sorts (called 30 times a second) that manages all your object. TGB handles that for you, just move you objects around using Torque functions in behaviors, or if you really like the main loop approach, you t2dScenegraphs have a per-frame callback, but behaviors is really the better approach.

Flash is more of a graphics engine than a game engine, whereas TGB does both, so let it do all the heavy lifting instead of re-inventing the wheel in script.

In this case, you could just call setlinearvelocity() on the object from you joystick bind, instead of plugging it into you loop.
#3
05/16/2009 (5:54 am)
If your having to run script code more than 1 or two times a second your going to see a significant performance impact. Either rewrite the code to be more "event" driven or you have to move your frequent script calls into native c++.