SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Getting Number from Textfield

    Hi,

    Im new to actionscript 3.0 and am having problems with a simple tutorial i am following. The tutorial just shows how to setup an input textfield, a submit button and then a background area to print the result.

    Here is the actionscript 3.0 code:

    Code Actionscript:
    var body_txt:Textfield = new Textfield();
    var yourName:String;
     
    body_text.x = 98;
    body_txt.y = 66;
    body_txt.autoSize = TextFieldAutoSize.LEFT;
     
    submit_btn.addEventListener(MouseEvent.CLICK, onClick);
     
    function onClick(event:MouseEvent):void
    {
    	addChild(body_txt);
    	yourName = name_txt.text;
    	board_txt.text = yourName;
    }

    This all works fine i.e. i test movie, insert text into the input field, press teh submit button and then it prints what i typed into the board_txt field.

    Ok, here's the problem. I want to be able to type in numbers and then do a calculation and return the result. i have tried changing the second line to:

    var yourName:Number;

    but i get errors. I have also tried changing the bottom lines to:

    yourName = name_txt.int;
    board_txt.int = yourName;

    and

    yourName = name_txt.number;
    board_txt.number = yourName;

    but still getting errors.

    Can someone tell me where im going wrong when trying to work with integers?

    Thanks

  2. #2
    SitePoint Wizard jim_morrison3's Avatar
    Join Date
    Apr 2004
    Location
    UK
    Posts
    2,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try yourName = Number(name_txt.text);
    Good Luck!
    There are things known and Things unknown
    And in between are the Doors.
    I love hand Coding.

  3. #3
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi Jim,

    Nope, tried that but unfortunatly its still giving me errors. The error is "Implicit coercion of value type Number to an unrelated type String" for this line:

    yourName = Number(name_txt.text);

  4. #4
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're still going to want board_txt.text as you need set the text of the TextField.

    To get yourName as an integer, try first declaring it at the top as a Number (var yourName:Number) and then when you set it, try: yourName = parseInt(name_txt.text);

    None of this is tested mind you, it's all just off the top of my head.

  5. #5
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, finally figured it out. For anyone who's interested the error was coming up as you cant insert a "Number" into a dynamic textfield. You have to first convert it back to a string to diplay it. WOW thats a stupid way of having to do things so maybe im missing something here but at least its working for me now.

    Code Actionscript:
    var body_txt:Textfield = new Textfield();
    var yourName:Number;
     
    body_text.x = 98;
    body_txt.y = 66;
    body_txt.autoSize = TextFieldAutoSize.LEFT;
     
    submit_btn.addEventListener(MouseEvent.CLICK, onClick);
     
    function onClick(event:MouseEvent):void
    {
        addChild(body_txt);
        yourName = Number(name_txt.text);
        yourName = yourName + 10;
        board_txt.text = (String(yourName));
    }

  6. #6
    SitePoint Wizard jim_morrison3's Avatar
    Join Date
    Apr 2004
    Location
    UK
    Posts
    2,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Banana Man View Post
    Ok, finally figured it out. For anyone who's interested the error was coming up as you cant insert a "Number" into a dynamic textfield. You have to first convert it back to a string to diplay it. WOW thats a stupid way of having to do things so maybe im missing something here but at least its working for me now.
    Buddy it is called Type Casting and is one of the most integral part of OOPS paradigm.

    board_txt.text = (String(yourName));
    Instead of doing this you can also do this board_txt.text = yourName.toString();
    Good Luck!
    There are things known and Things unknown
    And in between are the Doors.
    I love hand Coding.

  7. #7
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, problem 1 sorted... now on to problem 2

    Scenes!

    I was planning on using Scenes for this project but everywhere i look online people say dont use scenes especially if you are using actionscirpt also.

    I am creating a flash version of a book more a less and was going to setup each new page as a seperate scene. I would start with a menu scene where you can click each menu item and go to that scene and then on each page i would have a forward and back button for navigation from page to pag. I think scenes are handy for a project like this because when i have 90 pages done it would be messy having it all in a timeline.

    Do you think scenes are ok for what i want to do or is there some other method that would work better?

    Thanks again!

  8. #8
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem with scenes in ActionScript is that they can't communicate with each other - any variables that are set in one scene are wiped when you move to another scene.

    A better way to do it may be to have separate SWFs in each instance where you were going to have a scene. You can then call these child SWFs into a parent SWF to tie it all together. And better yet, variables from parent and child SWFs are accessible.

    I've written a quick tutorial on how to call in external SWFs that may help you: http://fcOnTheWeb.com/articles/external_swfs/

    Hope that helps.

  9. #9
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ferrari_chris View Post
    The problem with scenes in ActionScript is that they can't communicate with each other - any variables that are set in one scene are wiped when you move to another scene.

    A better way to do it may be to have separate SWFs in each instance where you were going to have a scene. You can then call these child SWFs into a parent SWF to tie it all together. And better yet, variables from parent and child SWFs are accessible.

    I've written a quick tutorial on how to call in external SWFs that may help you: http://fcOnTheWeb.com/articles/external_swfs/

    Hope that helps.
    Thanks, i presumed variables would have been available across scenes so they are definetly ruled out for me now. Thanks for the tutorial also, i'll give it a bash tomorrow.

    One last thing. I will be using the same background image for most pages so will have to have it in the parent library and then in every child library also. Will it be possible for this to be taken from cached memory or would it be seen as a different image in each child and hence cause a big filesize?

  10. #10
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe you could play around with transparency on the child SWFs so the background from the parent shows through.

    Or you could try dynamically adding the background image and see if it caches on the second and subsequent inclusions?

  11. #11
    SitePoint Wizard jim_morrison3's Avatar
    Join Date
    Apr 2004
    Location
    UK
    Posts
    2,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Once you load any image dynamically in Flash, it goes to the system cache, and if you have not set any color to the child swf then the parent's background image will show through. Also if you dont wanna try using separate swf then you can also use frames instead of scenes (though i would prefer loading child swfs), what you can do with Scenes can be achieved using Frames too.
    Good Luck!
    There are things known and Things unknown
    And in between are the Doors.
    I love hand Coding.

  12. #12
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The child .swf's are probably the way to go then if i can have the background image in the parent .swf.

    Ferrari, i have been using your sample code and works great. Thanks! I'm having problems though when i want to load another .swf into the parent. How exactly do i go about it? I tried setting up a new keyframe at frame 2 and inserted the same code as before but changed the child swf filename it was calling in to something different but this just gives me conflict errors.

  13. #13
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some of that code can be reused again for another SWF, some of it will have to be rewritten but you'll need to change the instance names, etc.

    I'm not 100% sure off the top of my head - I think you can use the MovieClipLoader again (assuming you're using AS2) but you'll need to create a new container_mc. I'll have a look over the weekend for you and hopefully get back to you with an answer.

  14. #14
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ferrari_chris View Post
    Some of that code can be reused again for another SWF, some of it will have to be rewritten but you'll need to change the instance names, etc.

    I'm not 100% sure off the top of my head - I think you can use the MovieClipLoader again (assuming you're using AS2) but you'll need to create a new container_mc. I'll have a look over the weekend for you and hopefully get back to you with an answer.
    Nice, thanks for all the help with it. Im using AS3 by the way... from my limited experience im guessing that will probably cause issues

  15. #15
    SitePoint Addict Banana Man's Avatar
    Join Date
    Dec 2005
    Posts
    389
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, i figured out how to insert multiple swf's now. I just changed all variable numes to something unique. Im starting to wonder if this method is going to be much cleaner than just plonking everything directly into individual frames. The only benefit is that i am using 9 layers so it will be messy if i need to move things later on.

    Anyway, i have added the actionscript code in frame 1 of the parent to bring in the first swf and then in frame 2 i have loaded a second swf. Each swf has navigation buttons so when creating them i tried adding a gotoAndStop(2) function hoping that when the first swf is played through the parent it would bring the user to frame 2 of the parent and then start playing the second swf. Unfortunatly this isint working. Anyone know if this is possible?

  16. #16
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would have thought that would have worked. It would probably work that way if you didn't want it to!

    Anyway, try this._parent.gotoAndPlay(2) as that should reference the parent SWF. Depending on where the ActionScript is in the child SWF, you may need to try seeveral levels of _parent (this._parent._parent._parent etc.) to get back to the parent.

    Good luck!


Bookmarks

Posting Permissions

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