Archive

Posts Tagged ‘actionscript’

The Date Class in AS3

February 24th, 2009 No comments

The Date class will allow you to check the current date and time on a user's computer. You'll find this class valuable when developing widgets like an event count down clock. To work with the Date class, you must instantiate a Date object:

  1. var myDate:Date = new Date();
  2. trace(myDate);

When the Date object has been built, you can call methods or get properties of the Date class to get date and time values:

  1. var myDate:Date = new Date();
  2. trace(myDate.getDate());

The getDate() method gets the current day of the month.

The getMonth() method gets the month of the year as a number value. Numeric references to the current month are not the normal month numbers we are used to working with.

For example, January is equal to 0 and December is equal to 11. For display reasons, always add 1 to the returned month number to get the true calendar month value.

Available methods for getting Time and Date properties of the Date class are:

  • getDate();
  • getDay();
  • getFullYear();
  • getHours();
  • getMilliseconds();
  • getMinutes();
  • getMonth();
  • getSeconds();

Note: For a full description of each method search "date class" via the Flash help menu.

Easy.. now get fancy with it and tell us what you created..

How to create bubbles in flash (ActionScript 2.0)

February 4th, 2009 7 comments

Here is a simple peice of ActionScript that can also be used to create snow or rain. I've seen a few particle engines on the net that I easily could have used but I wanted something simpler.

So this is what I came up with.

  1. var total:Number = 30; //total number of bubble
  2. var sw:Number = Stage.width; //stage width
  3. var sh:Number = Stage.height; //stage height
  4.  
  5. for (var i=0;i<total;i++){
  6.  
  7. var bubble:MovieClip = this.attachMovie("mcBubble",
  8. "mcBubble"+i,this.getNextHighestDepth());
  9.  
  10. // set the initial random speed
  11. bubble.yspeed = random(500) + 3;
  12.  
  13. // set the initial random position
  14. bubble._y = sh;
  15. bubble._x = random(sw);
  16.  
  17. // set the initial wobble factor (the X movement)
  18. bubble.xspeed = random(3);
  19.  
  20. if (random(2) eq 1) {
  21. bubble.xspeed = -bubble.xspeed;
  22. }
  23.  
  24. bubble.onEnterFrame = function(){
  25.  
  26. // the bubble movement
  27. this._y = this._y - this.yspeed;
  28.  
  29. // check if the bubble is out of the top of the screen
  30. if (this._y <= -15) {
  31.  
  32. // then reset to the bottom
  33. this._y = sh;
  34. this._x = random(sw);
  35. this.yspeed = random(5) + 3;
  36.  
  37. // now to randomise the scale of the bubble so
  38. // we get a few wee ones too
  39.  
  40. scale = 2 + (random(8));
  41. this._width = scale;
  42. this._height = scale;
  43.  
  44. // set the initial wobble factor (the X movement)
  45. this.xspeed = random(3);
  46.  
  47. if (random(2) eq 1) {
  48. this.xspeed = -this.xspeed;
  49. }
  50. }
  51.  
  52. // change the X value of the bubble
  53. this._x = this._x + this.xspeed;
  54.  
  55. // the bounce code for either the left or right of the
  56. // margin for the bubble
  57. if (this._x > 100 or this._x < 50) {
  58. this.xspeed = -this.xspeed;
  59. }
  60.  
  61. }
  62.  
  63. }

Easy.. now this is what it looks like.

You could make it more realistic by tweaking the speed, wobble values and add blurs or better graphics but what I needed  it for, this is perfect.

Download the Source Code - Use it any way you want, just let others know how you used it by leaving a comment. Peace!

Enjoy.

Disable all buttons in flash or disable 1 button in flash

February 3rd, 2009 3 comments

Recently I needed to disable all buttons on my flash site. I trawled the net looking for answers but all I got was what I already knew. Which is disabling each button 1 by 1 using an array like so:

  1.  
  2. //an array of button instance names
  3. var buttonInstanceNames:Array = new Array("button1","button2",
  4. "button3", "button4");
  5.  
  6. //function that sets the buttons enabled state
  7. function enableButtons(e){
  8. for(var i=0;i<buttons.length;i++){>
  9. this[buttonInstanceNames[i]].enabled = e;
  10. }
  11. }
  12.  
  13. //call function and assign enabled state
  14. enableButtons(false);

This is all good if you have a few buttons but what if your site is complex like the 1 I was working on and you wanted to disable all button interactivity without knowing buttons instance names.

Solution
Basically place a movie clip button over the entire movie or just over the area where your buttons are with _alpha set to 0. Remember to set the movie clip with a onRollOver, or onRelease event. Just set the blocking movie clip's _visible state to false when you want interactivity back.

Here is the code I used to get this working.

  1.  
  2. //The instance name of my blocker movieclip is "mcBlocker"
  3. //show or hide the button blocker
  4. function enableButtons(e){
  5. mcBlocker._visible = e;
  6.   mcBlocker.onRollOver = doNothing;
  7.  }
  8.  
  9. //do nothing when the blocker active
  10. function doNothing(){}
  11.  
  12. //just enable or disable the blocker
  13. enableButtons(false);
  14.  

Hope this helps..

Making the first letter in a textfield Uppercase

March 19th, 2008 No comments
  1. var myKeyListener = new Object();
  2. myKeyListener.onKeyUp = function() {
  3. var myString = myTextfield.text;
  4. var firstLetter = (myString.charAt(0)).toUpperCase();
  5. var remainderLetters = (myString.substring(1)).toLowerCase();
  6. var myNewString = firstLetter+remainderLetters;
  7. myTextfield.text = myNewString;
  8. };
  9. Key.addListener(myKeyListener);
SEO Powered by Platinum SEO from Techblissonline