I have been using as2 for a few years now but want to learn as3. I always learn better by example so I have uploaded a simple AS2 file in CS4 and was hoping someone could convert to as3 so I could dissect and learn. I know it involves a string class and am have difficulty wrapping my head around the whole class issue.
function newPassword(){
var totalChar = 8; // number of chars in the password
var xcom = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXY Z123456789"; // xcom to select chars from
var xcomLength=xcom.length;
var userPass=""; // set the inital variable
for (var xx=0;xx<totalChar;xx++){ // loop and create password
userPass+=xcom.charAt(Math.floor(Math.random()*xco mLength));
}
return userPass;
}
I was so close simple syntax error in my efforts. Thanks so much realMakc. Now I need to figure out how to make a class file out of it so I can take code off the timeline. Thanks again.
OK remember this is my first attempt at building a class file. The idea here is to create a random password generator. Here is what I have:
Code:
package com.sample.PWClass {
import flash.display.MovieClip;
public class PasswordClass extends MovieClip {
private var totalChar = 8; // number of chars in the password
private var xcom = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; // xcom to select chars from
private var xcomLength=xcom.length;
private var userPass=""; // set the inital variable
public function PasswordClass() {
for (var xx=0;xx<totalChar;xx++){ // loop and create password
userPass+=xcom.charAt(Math.floor(Math.random()*xcomLength));
}
return userPass;
trace ("Password Class Trace recognized.");
button1.addEventListener("click",function(e:*) :void{
mark.text=newPassword();
}
}
}
package com.sample.PWClass {
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class PasswordClass extends MovieClip {
private var totalChar = 8; // number of chars in the password
private var xcom = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
private var xcomLength=xcom.length;
private var userPass=""; // set the inital variable
public function PasswordClass() {
for (var xx=0;xx<totalChar;xx++){ // loop and create password
userPass+=xcom.charAt(Math.floor(Math.random()*xcomLength));
}
return userPass;
public class passBtn extends SimpleButton {
public function button1() {
addEventListener(MouseEvent.CLICK, passButtonFunction);
}
function passButtonFunction(event:MouseEvent):void {
MovieClip(root)mark.text=newPassword();
}
}
}
public class DocumentClass extends MovieClip {
private var totalChar=8;// number of chars in the password
private var letter:String="abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
private var number:String="0123456789";
private var l_length=letter.length;
private var n_length=number.length;
private var new_password:String=new String();// set the inital variable
public function DocumentClass() {
button.addEventListener(MouseEvent.CLICK,onClickHandler);
}
private function onClickHandler(evt:MouseEvent):void {
mark.text = newPassword();
trace(mark.text);
}
private function newPassword() {
new_password=new String();
for (var i=0; i < totalChar; i++) {// loop and create password
var a=letter.charAt(Math.floor(Math.random() * l_length));
var b=number.charAt(Math.floor(Math.random() * n_length));
var char=Math.floor(Math.random() * totalChar) % 2 == 0?a:b;
new_password+= char;
}
return new_password;
}
}
}
Thanks artix, thanks to your sample I see where I was making my mistake in nesting the functions. I have modified my code per your sample but I am still getting the error:
ReferenceError: Error #1056: Cannot create property button1 on com.sample.PWClass.PClass.
public class PClass extends MovieClip {
private var totalChar=8;// number of chars in the password
private var xcom:String="abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
private var xcomnum:String="0123456789";
private var l_length=xcom.length;
private var n_length=xcomnum.length;
private var new_password:String=new String();// set the inital variable
public function PClass() {
button1.addEventListener(MouseEvent.CLICK,onClickHandler);
}
private function onClickHandler(evt:MouseEvent):void {
mark.text = newPassword();
trace(mark.text);
}
private function newPassword() {
new_password=new String();
for (var i=0; i < totalChar; i++) {// loop and create password
var a=xcom.charAt(Math.floor(Math.random() * l_length));
var b=xcomnum.charAt(Math.floor(Math.random() * n_length));
var char=Math.floor(Math.random() * totalChar) % 2 == 0?a:b;
new_password+= char;
}
return new_password;
}
}
}
Thanks again I learne a lot from looking at your sample.
My AS code was good the problem was I needed to click on the Stage option "Automatically declare a stage instance" Thanks to everyone here that helped. Great community.
My AS code was good the problem was I needed to click on the Stage option "Automatically declare a stage instance" Thanks to everyone here that helped. Great community.
Sorry! Hope you made some mistake to understand. The class I created is a document class which you have to keep in the main folder and declare in the FLA as a document class.