A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [RESOLVED] moving from as2 to as3 need some help

  1. #1
    Senior Member
    Join Date
    Mar 2008
    Posts
    168

    resolved [RESOLVED] moving from as2 to as3 need some help

    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.
    Attached Files Attached Files
    Last edited by ADVaughn; 05-15-2013 at 08:37 PM.

  2. #2
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    here is the code I need to convert to AS3;

    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;
    }

    button1.onRelease=function(){
    mark.text=newPassword();
    }

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    921
    button1.addEventListener("click",function(e:*) :void{
    mark.text=newPassword();
    });
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

  5. #5
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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();
    			
    		}
    		
    	}
    	
    }

  6. #6
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    Am I getting any closer?


    Code:
    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();
    	
    		}
    		
    	}
    	
    }
    Last edited by ADVaughn; 05-16-2013 at 06:32 PM.

  7. #7
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    PHP Code:
    package {
        
    import flash.display.MovieClip;
        
    import flash.events.MouseEvent;
        
    import flash.text.TextField;

        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=0totalChari++) {// 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) % == 0?a:b;
                    
    new_password+= char;
                }
                return 
    new_password;
            }
        }




    arkitx

  8. #8
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

    Here is my new code:

    PHP Code:

    package com
    .sample.PWClass {

        
    import flash.display.MovieClip
        
    import flash.events.MouseEvent
        
    import flash.text.TextField

        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=0totalChari++) {// 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) % == 0?a:b
                    
    new_password+= char
                } 
                return 
    new_password
            } 
        } 

    Thanks again I learne a lot from looking at your sample.

  9. #9
    Senior Member
    Join Date
    Mar 2008
    Posts
    168
    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.

  10. #10
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Quote Originally Posted by ADVaughn View Post
    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.

    you also have to put the button on Stage.


    arkitx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center