| ID | Creation date | August 1, 2009 | |
| Platform | S60 5th Edition | Tested on devices | Nokia 5800 XpressMusic |
| Category | Flash Lite | Subcategory | S60 Platform Services |
| Keywords (APIs, classes, methods, functions): Service.Sensor, Platform Services, Accelerometer |
The following snippet demonstrates how to create a moving ball using the Motion Sensor (Accelerometer) provided by Platform API in S60 5th Edition devices.
Just create a new flash file (mobile) having resolution 360x640 and flash lite 3 in publish settings. Create 2 layers one having action scripts and one having a movie clip (which have to move) named "ball_mc" .
'Note:'The FPS in properties must be increased to 100 fps for smooth animation without any lagging. The uploaded new files have been changed to FPS
Put this on the first frame of the action script :
_global.X = 0;
_global.Y = 0;
import com.nokia.lib.Service;
var sensors = new Service("Service.Sensor", "ISensor");
fscommand2("DisableKeypadCompatibilityMode"); //It Disables the Virtual Keypad
fscommand2("FullScreen", true );
var inParam = {SearchCriterion:"AccelerometerAxis"}; // It Is Used For Searching XYZ axis data
var outParams = sensors.FindSensorChannel(inParam);
var channelInfo = outParams.ReturnValue;
var channelId = channelInfo[0].ChannelId;
var contextType = channelInfo[0].ContextType;
var quantity = channelInfo[0].Quantity;
var channelType = channelInfo[0].ChannelType;
var location = channelInfo[0].Location;
var vendorId = channelInfo[0].VendorId;
var dataItemSize = channelInfo[0].DataItemSize;
var channelDataTypeId = channelInfo[0].ChannelDataTypeId;
var channelInfo = {ChannelId:channelId, ContextType:contextType, Quantity:quantity, ChannelType:channelType, Location:location, VendorId:vendorId, DataItemSize:dataItemSize, ChannelDataTypeId:channelDataTypeId};
var inParams = {ListeningType:"ChannelData", ChannelInfoMap:channelInfo};
sensors.RegisterForNotification(inParams, callBack);
function callBack(transactionID:String, eventID:String, outParam:Object) {
if (outParam.ErrorCode == 0) {
var channelData = outParam.ReturnValue;
_global.X = channelData.XAxisData; //Retrieved X Axis Data
_global.Y = channelData.YAxisData; // Retrieved Y Axis Data
_global.X = _global.X/4;
_global.Y = _global.Y/4; // just divide the retrieved values with some constant to make motion sensor less sensitive.
} else {
var errorId = outParam.ErrorCode;
txt.text = "Error: "+errorId;
}
};
and on the second frame , write this code :
fscommand2("FullScreen", true);
_global.speed = 5;
onEnterFrame = function():Void{;
//Calculate the respective constant acceleration.;
if(_global.X>0)acelX = -1;
else acelX = 1;
if(_global.Y<0)acelY = -1;
else acelY = 1;
posX = Math.abs(_global.X/4) * acelX * _global.speed;
posY = Math.abs(_global.Y/4) * acelY * _global.speed;
wallColl(_root.ball_mc._x, _root.ball_mc._y); //This Function Checks for Boundry Walls
moveBall(posX, posY); //This Function Moves the ball
};
function wallColl(posX:Number, posY:Number):Void {
// All the values like 15, 620 , 350 are arbitrary .For creating a game , u have to put
// walls and to detect collision with wall u can use hitTest
if (posY<=15) {
ball_mc._y = 15;
}
if (posY>=620) {
ball_mc._y = 620;
}
if (posX<=15) {
ball_mc._x = 15;
}
if (posX>=350) {
ball_mc._x = 350;
}
}
function moveBall(posX:Number, posY:Number):Void {
_root.ball_mc._x += posX;
_root.ball_mc._y += posY;
}
stop();
No related wiki articles found