_secondsRound = Math.round(_theSound.length / 10) * 10 / 1000;
Showing posts with label Actionscript. Show all posts
Showing posts with label Actionscript. Show all posts
Tuesday, July 7, 2009
Math.round decimal places
Here is some rounding help. This converts miliseconds to seconds with two decimal places. Note the "/10) * 10" part, that rounds it to 2 decimal places, "/100) * 100" would round it to one decimal place.
Set Focus
Just sharing some info about focus in As3.
Opon experimenting with text input boxes I discovered that setFocus(), when uses on a hidden text input box was a great way to hide a cursor after ENTER event.
Opon experimenting with text input boxes I discovered that setFocus(), when uses on a hidden text input box was a great way to hide a cursor after ENTER event.
drawFocus() also comes in handy to bring a field back into perspective.
Top of display list
Example of setChildIndex to bring your display object to the top.
import flash.display.Sprite;
import flash.events.MouseEvent;
var container:Sprite = new Sprite();
addChild(container);
var circle1:Sprite = new Sprite();
circle1.graphics.beginFill(0xFF0000);
circle1.graphics.drawCircle(40, 40, 40);
circle1.addEventListener(MouseEvent.CLICK, clicked);
var circle2:Sprite = new Sprite();
circle2.graphics.beginFill(0x00FF00);
circle2.graphics.drawCircle(100, 40, 40);
circle2.addEventListener(MouseEvent.CLICK, clicked);
var circle3:Sprite = new Sprite();
circle3.graphics.beginFill(0x0000FF);
circle3.graphics.drawCircle(70, 80, 40);
circle3.addEventListener(MouseEvent.CLICK, clicked);
container.addChild(circle1);
container.addChild(circle2);
container.addChild(circle3);
addChild(container);
function clicked(event:MouseEvent):void {
var circle:Sprite = Sprite(event.target);
var topPosition:uint = container.numChildren - 1;
container.setChildIndex(circle, topPosition);
}
Variable behaviors
While working with public variables within a custom class i had the following observations:
Created a new TextField in the constructor function and change the variable.text in another function by mouseclick. The result was that the textfield was updated at each mouse click.
Conversely if you create the TextField within the mouse click event handler function the textfield keeps placing text on top of text each time the mouse is clicked.
Conclusion, creating TextField by a function variable stacks instance upon instance instead of just changing the single "public variable" each time the mouse is clicked. Might be handy for adding a bunch of graphics on top of one another in the display list.
Created a new TextField in the constructor function and change the variable.text in another function by mouseclick. The result was that the textfield was updated at each mouse click.
Conversely if you create the TextField within the mouse click event handler function the textfield keeps placing text on top of text each time the mouse is clicked.
Conclusion, creating TextField by a function variable stacks instance upon instance instead of just changing the single "public variable" each time the mouse is clicked. Might be handy for adding a bunch of graphics on top of one another in the display list.
As3 tool
Here is some code to check the name and object class of buttons you click on in your project. The function is great for troubleshooting.
Notice that the up and down "volume" buttons return a target of [object VolUp] but they each have their own instance names "down and up". This is because I used the same symbol-"class" for both instances. Great for learning/finding errors.
Also notice you can click on the unnamed movieclips(level bars) and it displays their instance names.
The code snippet is the function that displays mouse click info. You will need to format the TextField to get it to display where and how you want it.(
Click anywhere on the player face above.
Notice that the up and down "volume" buttons return a target of [object VolUp] but they each have their own instance names "down and up". This is because I used the same symbol-"class" for both instances. Great for learning/finding errors.
Also notice you can click on the unnamed movieclips(level bars) and it displays their instance names.
The code snippet is the function that displays mouse click info. You will need to format the TextField to get it to display where and how you want it.(
_text.width = 400;) for example.Click anywhere on the player face above.
var _text:TextField = new TextField();
//testfunction
public function testIt(evt:MouseEvent):void
{
_text.text = "Event:\n" + evt + "\n\nClicked target = " +
evt.target + "\nClicked target name = " + evt.target.name;
addChild(_text);
trace("Ran button test:");
trace("clicked target = " + evt.target);
trace("clicked target name = " + evt.target.name)
}
Access display object name
Function of parent MC
Class function com
Seems through experimentation that within classes it is impossible to access public class variables outside of a function, period. Public variables are accessible from document class but only from within its functions, not code within the class brackets.
Public variables get declared within the class brackets.
Public variables get declared within the class brackets.
Combo box tip
Just stumbled upon a combo box issue that might boggle ya. When adding listeners for keyboard KEY_DOWN type events you may run into trouble when opening a combo box. It may disable your key pressed listener.
Solution is simple. The Combo box draws focus away from the stage because it has its own keyboard commands for navigating the combo box. It gets left on even after your selection. Solution =
Solution is simple. The Combo box draws focus away from the stage because it has its own keyboard commands for navigating the combo box. It gets left on even after your selection. Solution =
someObject.setFocus()to draw focus away after combo box dropdown closed or a selection made.
Test Input field quirk
Along the same lines of the last post about combo box focus and its issues, here is a Text input field issue I found.
If you are listening for keyboard events be aware that whatever text input field has focus on it will pick up the key pressed in its field. It's weird too, you can't get rid of it even by setting the field's text to nothing.
If you set the text to something else the key you hit still occupies the first slot, shaky bug. To avoid it make sure that even when invisible, make sure that no text input fields have focus on them. You could set up a hidden 'ghost' field to have focus on if you need to send focus elsewhere.
If you are listening for keyboard events be aware that whatever text input field has focus on it will pick up the key pressed in its field. It's weird too, you can't get rid of it even by setting the field's text to nothing.
textfield.text = "";
If you set the text to something else the key you hit still occupies the first slot, shaky bug. To avoid it make sure that even when invisible, make sure that no text input fields have focus on them. You could set up a hidden 'ghost' field to have focus on if you need to send focus elsewhere.
Oop
The ten main terms describing Object Oriented Programming:
1) Class -Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features).
2) Object -A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have
3) Instance -One can have an instance of a class or a particular object. The instance is the actual object created at runtime.
4) Method -An object's abilities. In language, methods (sometimes referred to as "functions") are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods.
5) Message passing -The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing. For example, the object called Breeder may tell the Lassie object to sit by passing a "sit" message
6) Inheritance -"Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.For example, the class Dog might have sub-classes called Collie, Chihuahua, and GoldenRetriever.
7) Abstraction -Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
8) Encapsulation -Encapsulation conceals the functional details of a class from objects that send messages to it.
9) Polymorphism -Polymorphism allows the programmer to treat derived class members just like their parent class' members.
10) Decoupling -Decoupling allows for the separation of object interactions from classes and inheritance into distinct layers of abstraction.
Above terms copied from wikipedia.org
1) Class -Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features).
2) Object -A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have
3) Instance -One can have an instance of a class or a particular object. The instance is the actual object created at runtime.
4) Method -An object's abilities. In language, methods (sometimes referred to as "functions") are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods.
5) Message passing -The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing. For example, the object called Breeder may tell the Lassie object to sit by passing a "sit" message
6) Inheritance -"Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.For example, the class Dog might have sub-classes called Collie, Chihuahua, and GoldenRetriever.
7) Abstraction -Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
8) Encapsulation -Encapsulation conceals the functional details of a class from objects that send messages to it.
9) Polymorphism -Polymorphism allows the programmer to treat derived class members just like their parent class' members.
10) Decoupling -Decoupling allows for the separation of object interactions from classes and inheritance into distinct layers of abstraction.
Above terms copied from wikipedia.org
Subscribe to:
Posts (Atom)
