Coded Creativity
codedcreativity.blogspot.com
codedcreativity.blogspot.com
Tuesday, July 27, 2010
Posted by Anand Varma
Tuesday, July 13, 2010
Posted by Anand Varma in ActionScript, as3, Basic, eventHandlers, events, Flash
Code ::
up_btn.addEventListener(MouseEvent.MOUSE_DOWN,up_fn);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN,down_fn);
left_btn.addEventListener(MouseEvent.MOUSE_DOWN,left_fn);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN,right_fn);
alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN,alpha_fn);
function up_fn(e:MouseEvent)
{ myObj_mc.y-=10;
// this is equivalent to: myObj_mc.y=myObj_mc.y-10; }
function down_fn(e:MouseEvent)
{ myObj_mc.y+=10;
}
function left_fn(e:MouseEvent)
{ myObj_mc.x-=10;
}
function right_fn(e:MouseEvent)
{ myObj_mc.x+=10;
}
function alpha_fn(e:MouseEvent)
{ myObj_mc.alpha=0.5;
}
up_btn.addEventListener(MouseEvent.MOUSE_DOWN,up_fn);
down_btn.addEventListener(MouseEvent.MOUSE_DOWN,down_fn);
left_btn.addEventListener(MouseEvent.MOUSE_DOWN,left_fn);
right_btn.addEventListener(MouseEvent.MOUSE_DOWN,right_fn);
alpha_btn.addEventListener(MouseEvent.MOUSE_DOWN,alpha_fn);
In these first few lines, we are creating eventListeners to detect events from the event stream and when a "MOUSE_DOWN" event is detected, it calls the corresponding functions wherein we define how it should react to such an event.
MOUSE_DOWN is a subset of class MouseEvent and when the event is recorded, it calls the corresponding function with arguments as a MouseEvent...
function up_fn(e:MouseEvent)
{ myObj_mc.y-=10;
// this is equivalent to: myObj_mc.y=myObj_mc.y-10; }
function down_fn(e:MouseEvent)
{ myObj_mc.y+=10;
}
function left_fn(e:MouseEvent)
{ myObj_mc.x-=10;
}
function right_fn(e:MouseEvent)
{ myObj_mc.x+=10;
}
function alpha_fn(e:MouseEvent)
{ myObj_mc.alpha=0.5; //sets transparency to half
}
In the above code, "x" "y" and "alpha" marked in green, are properties of movielips that define the placement along X-axis , along Y-axis and the transparency respectively.
When we say myObj_mc.y-=10; This means that we are moving the movieclip by 10pixels, upwards. Similarly, the movements along other directions are written in the other functions and the transparency is set to half when the corresponding button is pressed (refer alpha_fn function).
Note:: The origin is considered to be at the top-left corner and as we move right, x-coordinate increases and as we move down, y-coordinate increases.
And alpha (transparency) takes values from 0-1, 0 corresponding to full transparency and 1 corresponding to no transparency.
The functions defined are that the event listeners would call on recording a mouse event. We are defining functions with a parameters "e" which is a MouseEvent Object, this is because when an eventListener calls the functions, it sends the MouseEvent as an argument and if we don't define parameter "e", flash would detect an Argument Mismatch Error.
Sunday, July 11, 2010
Posted by Anand Varma in android, Augmented Reality, Google, Misc., Nokia, smartphones
Posted by Anand Varma in ActionScript, as3, Basic, eventHandlers, events, Flash, objects
Code ::
my_btn.addEventListener(MouseEvent.CLICK,handleClick);
function handleClick(e:MouseEvent)
{
trace("my_btn was clicked !!!");
// code to be executed on click here
}
my_btn.addEventListener(MouseEvent.CLICK,handleClick);In this line, we are assigning an Event Listener to the button (instance name : "my_btn") to listen to the Events Stream and call the function "handleClick" when a MouseEvent, "CLICK" is registered.In simple words, the function is called when the user clicks the button we put on the stage.Syntax for initializing an eventHandler :: Object.addEventListener(Event,function_name);
function handleClick(e:MouseEvent)
{
trace("my_btn was clicked !!!");
// code to be executed on click here
}Here, we are defining a function by name handleClick which will be called when a mouse click event is registered.
Here a parameter variable, "e" of the type MouseEvent is set because the eventHandler function passes an event variable as arguments and the function needs to accept it though we dont use it in the code.
Saturday, July 10, 2010
Posted by Anand Varma in ActionScript, classes, Flash, objects, OOP
Sample greeter Class ::package {public class greeter{private var my_name:String;public function greeter(){}public function init_name(n:String){ my_name=n;trace("Initialized Name!")}public function hello(){ trace("Welcome "+my_name+".");}}}
Code ::
var n1:greeter=new greeter();
n1.init_name("Anand");
n1.hello();
var n2:greeter=new greeter();
n2.init_name("Dimitri");
n2.hello();
var n1:greeter=new greeter();This line of code creates a new object with an instance name "n1", in this case an object of class greeter.As soon as an instance of the object is created, the constructor function is called and instances of variables and functions are created, specific to each instance.
n1.init_name("Anand");
Here, we are calling the "init_name" function from the n1 instance of the greeter object with arguments "Anand".
n1.hello();
This calls the hello function of the n1 instance of greeter object.
Friday, July 9, 2010
Creating you first class and packageA quick Summary of OOP ::In the oop model, pieces of code are considered objects and each such object is properly defined by a class with variables and functions specific to the object. Such discrete pieces of code can be easily reused and allows for encapsulation.
Code::package mypackage.as3 {class Ball{import flash.display.*;var x:Number;var y;Number;function Ball(){// some initialization code heretrace("Object Successfully created");}public function changeSize(){// code to change size here}private function secret(){// some code, whose functionality you wish to hide}}}
package mypackage.as3
in this line, we are defining a package, which acts as a repository of all our classes written for the specific application.
class Ball{
import flash.display.*;
in the first line, we are defining our class which contains properties and functions of any Ball object that we use in our program. And the second line imports all classes from the display package so that they could be used in our class.
var x:Number;var y:Number;Now we are creating two variables x and y which will exist for every instance of the ball object which we create in the future.here "var" keyword indicates that we are creating a new variable of name "x" and ":Number" tells it what type of variable it is. In this case, a number.Syntax for Creating a new Variable :: var variableName:variableType;
function Ball(){// some initialization code heretrace("Object Successfully created");}Here, we are defining a constructor. A constructor is a special function which is executed as soon as an instance of an object is created. It is distinguished from other functions as it has the same name as the class.The trace function is one that is heavily used for debugging in flash. This function prints the arguments passed into an output window. In this case, it prints "Object Successfully created".public function changeSize(){// code to change size here}Here, we are defining a normal function by the name changeSize. The function type is set to public (access modifier) which means that it can be accessed from outside the class too.
- AcessModifiers can be prefixed to classes, functions and any kind of variables.
private function secret(){// some code, whose functionality you wish to hide}Here, we are defining another function, only this time, the access modifier set to private which means it can only be called from inside the class.Syntax for Defining a function :: function functionName(parameters-here){ }
Posted by Anand Varma in ActionScript, as3, Flash