You Are Here:

Community: Wiki

This page was last modified on 3 September 2009, at 10:19.

Create a moving ball using motion sensor (Platform Services)

From Forum Nokia Wiki

Reviewer Approved   


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

Overview

The following snippet demonstrates how to create a moving ball using the Motion Sensor (Accelerometer) provided by Platform API in S60 5th Edition devices.

Preconditions

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

Source

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();

Download

Further reading

Author

  • This page is authored by SajiSoft. If u find any mistake, please feel free to edit it .
  • FPS edit By Siraj a.k.a Apacheking

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fScrollbarE5fComponentE5fforE5fFlashE5fE4citeX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZflashQ qfnZtopicQUqfnTopicZflashE5fliteQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X