Tutorial: How to make Gsensor Games
in Tutorials by santokiOne of the reason Cowon’s s9 was a huge hit was its use of an accelerometer. This tutorial will teach you how to utilize its latest advanced gsensor API.
If you would like to just skip the tutorial and just download the file, you can get it here.
Otherwise, let’s get started.
1. Drawing our hero
If we are to move anything with our nifty Cowon G-Sensor, we’ll need a hero object – a ball. Let’s go on ahead and grab the Oval Tool (0) and draw a circle. Once you’ve made a circle select it and make it into a movie clip (F8) and name it “ball” and make sure to check Export for ActionScript and give it an Identifier name of “ball” and press OK (see picture below).
2. Coding our hero
believe it or not, we are 1/2 way there! Well, almost there. Now go on ahead and create a new layer, label it action and lock the layer (see image below).
Now the best part… the code. Press F9 and enter the following code in the “Actions” layer:
/* Variables */
// Always Initialized Variables
var setX:Number = 0;
var setY:Number = 0;
var setZ:Number = 0;
var preX:Number = 0;
var preY:Number = 0;
var preZ:Number = 0;
var DIR:Number = 0;
var curX:Number = 0;
var curY:Number = 0;
var curZ:Number = 0;
var gCurRotation:Number = -1;
//Callibrates the current position of the s9 as the "initial position"
SetXYZ();
stop();
//creates hero with .4 speed sensitivity;
create_hero(.4);
function create_hero(spd:Number) {
//creates a mc and links the mc labeled "ball" and names it hero.
var mc:Object = _root.attachMovie("ball", "hero", _root.getNextHighestDepth());
//centers the mc to the middle of the screen.
mc._x = Stage.width/2;
mc._y = Stage.height/2;
//sensor speed;
mc.sensor_spd = spd;
mc.onEnterFrame = function() {
//the code that allows movement based off of the xyz movement.
_root.ReadXYZ();
//movement applied to this mc
this._x = this._x+this.sensor_spd*_root.curX;
this._y = this._y+this.sensor_spd*_root.curY;
//bounding restrictions.
if (this._x>=480) {
this._x = 480;
}
if (this._x<=0) { this._x = 0; } if (this._y>=272) {
this._y = 270;
}
if (this._y<=0) { this._y = 0; } }; } // s9 Accellerometer function
function SetXYZ():Void { trace("set() working!");
setX = ext_fscommand2("GetEtcTASValueX");
setY = ext_fscommand2("GetEtcTASValueY");
setZ = ext_fscommand2("GetEtcTASValueZ");
if (setZ>0) {
DIR = 1;
} else {
DIR = -1;
}
preX = setX;
preY = setY;
preZ = setZ;
}
function ReadXYZ() {
curX = ext_fscommand2("GetEtcTASValueX");
curY = ext_fscommand2("GetEtcTASValueY");
curZ = ext_fscommand2("GetEtcTASValueZ");
curX = Calibration(curX, preX);
curY = Calibration(curY, preY);
curZ = Calibration(curZ, preZ);
preX = curX;
preY = curY;
preZ = curZ;
if (setZ>=0 && curZ>=0 || setZ<0 && curZ<0) {
curY = curY-setY;
} else if (curY<0) { curY = -512-curY-setY;
} else { curY = 512-curY-setY; }
curX = curX-setX;
curY = curY*DIR*_root.gCurRotation;
curX = curX*DIR*_root.gCurRotation;
}
function Calibration(cur:Number, pre:Number):Number {
if (Math.abs(cur, pre)>80) {
return (pre);
} else if (cur == 0) {
return (pre);
} else {
return (cur);
}
}
3. In Action!
If everything went correctly, the ball hero you created earlier should be spawned in the center, but unable to move. The hero is unable to move since it is moved based on the Gsensor movement of the Cowon. If an output prompt prints, “set() working!” Then it worked. Otherwise, you may have done something wrong.
The code has been commented where needed in describing everything where needed. If you feel the movement is too fast or too slow, you can change the initial speed by changing the variable on the hero:
“create_hero(.4);” // change the .4 to any number;
Again if you would like the source code you can download it here.
Depending on the feedback, I’ll try making this into a series – a break from me making games.





Nice work and i for one would love to see a series.
you are great man, santoki, we very much waiting release of Shadow Kai! Greetings from Russia! (sorry for my bad english)
Hi Defconhe! Your English is great. Thanks for your support!
het man you are doing some nice work,
in a few days i will have my cowon s9
and looking forward to play your games
[...] This tutorial is buildup from a previous Gsensor tutorial. You can find it here. [...]
Hey Santoki!
I wannt to help you, to make this and more games from cowon s9.
But i have a problem.
I dont know what program do you use to make this game?
@justmile hey! Cowon si uses Flash s9. Anything built on AS 2.0 from version 7.0 or less.
Santoki, I dont undersant wat’s the name of the program.. can you repet pls only the name? Thanks and sorry my english is bad..
you can download it here:
https://www.adobe.com/cfusion/tdrc/index.cfm?product=flash
i’m confused. in the function Calibration(cur, pre); you used:
if (Math.abs(cur, pre)>80) {
after “cur”, the “, pre”…. what does that do? i was thinking it might be a mistake… i understand functions, but the “Math.abs(” only requires one variable; what does the second do? please help. thank you.