×
Namespaces

Variants
Actions
Revision as of 05:59, 14 February 2012 by hamishwillee (Talk | contribs)

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

From Nokia Developer Wiki
Jump to: navigation, search

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 Feb 2012)

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

99 page views in the last 30 days.
×