Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Archived:Create a moving ball controlled by a motion sensor using Flash Lite

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

We do not recommend Flash Lite development on current Nokia devices, and all Flash Lite articles on this wiki have been archived. Flash Lite has been removed from all Nokia Asha and recent Series 40 devices and has limited support on Symbian. Specific information for Nokia Belle is available in Flash Lite on Nokia Browser for Symbian. Specific information for OLD Series 40 and Symbian devices is available in the Flash Lite Developers Library.

This Flash Lite code example demonstrates how to create a moving ball using the Motion Sensor (Accelerometer) provided by the S60 Platform API in Category:S60 5th Edition devices.

Article Metadata
Code Example
Installation file: Motion_Sensor_Ball_PS.swf
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: Service.Sensor, Platform Services, Accelerometer
Created: sajisoft (01 Aug 2009)
Last edited: hamishwillee (14 May 2013)

Contents

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" .

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

This page was last modified on 14 May 2013, at 06:20.
77 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×