A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Trying to learn AS3 event handling (I know AS2)

  1. #1
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110

    Trying to learn AS3 event handling (I know AS2)

    Hi, I've tried searching and checking out tutorials but it seems that all of those that would be of help to me are all no longer online, and I can't seem to find any basic info about this.

    Basically I need to know how to change this type of stuff into AS3, I'm not very advanced with this and the whole putting code onto different MCs is very ingrained in me so I don't get how to put it all in one frame in AS3. Some guidance or help would be majorly appreciated.


    This is on a button, but how do I go about getting it off the button and on to the timeline, (I know this is basic stuff but I'm having trouble finding any BASIC stuff in search and adobe docs)

    on (release) {_root.gotoAndStop(2);
    _root.title.gotoAndPlay(2)
    _root.bus.gotoAndPlay(2);
    }

    And this type of stuff on MCs

    onClipEvent (enterFrame) {
    if(Key.isDown(Key.RIGHT)){

    if(_root.game.bus_BG._x<540){
    _root.BG.BG_2.crack._x+=14
    _root.BG.BG_2.akira._x+=14
    _root.game.bus_BG._x+=3

    }}}


    Any help or links would really alleviate my stress right now.

    THANKS!!
    Use soap! Charisma + 5 bonus

  2. #2

  3. #3
    Member
    Join Date
    May 2008
    Location
    Ohio, USA
    Posts
    63
    You might also want to check out http://www.focusonflash.com/ The tutorials don't get into Keyboard Events, but they'll get you started with simple Mouse and Timer Events. This tutorial - http://www.newgrounds.com/bbs/topic/773868 specifically talks about Keyboard Events and has a couple of links to custom Key classes.

    AS3 takes some getting used to if you're migrating from AS2. I actually just started learning AS3, but so far it seems to be a lot more flexible and intuitive in most cases. You're going to have to basically relearn ActionScript, so don't expect to understand everything right away.

    More references:
    http://www.senocular.com/flash/tutor...3withflashcs3/
    http://www.kirupa.com/developer/flash/index.htm
    Last edited by DoomOfTheLiving; 11-03-2008 at 11:25 PM.

  4. #4
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    I've referred this book to friends and strangers alike. Most don't bother, but those who actually do spend the $30 to buy it always thank me for recommending it... it is the BEST book to own for AS3 novices http://www.amazon.com/Learning-Actio...5821042&sr=8-1
    Follow me on Twitter: http://twitter.com/jasondefra

  5. #5
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110
    Thanks a lot guys!

    This helps, it is very hard for me to relearn AS, I mean right now I can't even figure out how to do _root.gotoAndStop(3), in AS2 you just put that at the last frame of a MC and it'll move the main timeline, but AS3 don't let you use it that way and it's very frustrating...my problem is I made a small driving game, but I need to put it in AS3, and its past due at work so I'm stressing.

    So I'm just going piece by piece and trying to learn the new syntax for each part. So thanks again and I'm sure I'll have more problems.
    Use soap! Charisma + 5 bonus

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Grant Skinner has you covered: http://gskinner.com/talks/as3workshop/

  7. #7
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110
    Sweet, that helps a lot!!
    Use soap! Charisma + 5 bonus

  8. #8
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110

    Another Problem

    Here's a question I can't seem to find an answer for.

    Is it possible that every little thing takes an immense amount of code in AS3, if that's the case let me know so I can accept my fate...

    for example, on the timeline of thisMC at frame 30 I simply put

    _root.otherMC.gotoAndStop(3)

    and then that other movie clip on the stage will play what's in frame 3.

    perfect!! it all works out for me.

    But in AS3 how do you do something like this, all I can do is get error messages, when I try to use functions and addEventListener(Event.ENTER_FRAME, whatever); and all this crazy AS3 stuff it takes so much what seems to me like backwards ass coding to do a very simple gotoAndStop request.

    Am I missing some major thing here, I appreciate the links and I'm going through them but it seems every single thing I'm trying to do is a hassle.

    Thanks if anyone's got some ideas.
    Use soap! Charisma + 5 bonus

  9. #9
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    AS3 does not have a _root. It is a completely different structure so you have to really think of it as a different language. Don't have the mentality that just because it works in AS 2.0 it will work this way in AS 3.0.

    That was my biggest road block while moving away from AS 2.0. But trust me, once you learn 3.0 you will never want to go back!

  10. #10
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    stage.otherMC.gotoAndStop(3)
    Follow me on Twitter: http://twitter.com/jasondefra

  11. #11
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110
    Is there an easy way to tell a MC_1 to gotoAndStop(3) once MC_2 reaches a certain frame, to me it's the most basic thing to do using actionscript to animate, but maybe the technology has moved beyond me too fast...

    I can't even find this simple stuff in the tutorials.

    It just used to be so simple, I'm no coder and not very logical even but with AS2 I could do so much so easily and this upgrade is so jarring. It really is learning a whole new language, but it feels like the simple things are out of reach now...
    Use soap! Charisma + 5 bonus

  12. #12
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Like I said before, if you're a novice and having problems with the basic stuff, buy this book: http://www.amazon.com/Learning-Actio...5821042&sr=8-1
    Trust me, after reading and going through the exercises in that book you'll be a more competent AS3 developer.

    One way you could it do is:

    PHP Code:
    //on the Actions layer in the main timeline
    addEventListener(Event.ENTER_FRAME,checkFrame,false,0,true);

    function 
    checkFrame(e:Event):void{
      if (
    MC_2.currentFrame==30){
        
    MC_1.gotoAndStop(3);
        
    removeEventListener(Event.ENTER_FRAME,checkFrame);
      }

    Follow me on Twitter: http://twitter.com/jasondefra

  13. #13
    Submit Modifications
    Join Date
    Jun 2003
    Location
    Chicago
    Posts
    110
    Sorry, I made that last post without seeing your one just before it. But still none of that is working, bunch of errors I don't understand.

    Guess I gotta just learn to code from the ground up now.

    It just sucks cause I've made a Flash game for work in AS2 only to just now discover it needs to be AS3( which the 'programmers' here don't know how to explain either), so I fear I'm gonna lose my job for spending 2 weeks on a project that won't actually work...

    mc1.gotoAndStop(3)

    or...

    addEventListener(Event.ENTER_FRAME,checkFrame,fals e,0,true);

    function checkFrame(e:Event):void{
    if (MC_2.currentFrame==30){
    MC_1.gotoAndStop(3);
    removeEventListener(Event.ENTER_FRAME,checkFrame);
    }
    }

    I wish it was still 2002...
    Use soap! Charisma + 5 bonus

  14. #14

  15. #15
    Teacosy is coming ranoka's Avatar
    Join Date
    Jun 2003
    Location
    UK
    Posts
    123
    You could do (root as MovieClip).otherMC.gotoAndPlay(2);

    I think nobody told you to encourage you to learn better practices. But sometimes you've just got to use what you know/understand at the time, and improve as you go.

    In AS2 everything can reference the stage. In AS3 only Display Objects attached to the stage can reference the stage.

    Event handlers are completely different in AS3.
    This is really worth reading, by Senocular:
    http://www.adobe.com/devnet/actionsc...ng_as3_03.html

    Hope that helps, and that you get through your job anxiety. I'm sure if you keep at it you can get your head around it

  16. #16
    Senior Member Gohloum's Avatar
    Join Date
    Aug 2002
    Location
    Florida
    Posts
    185
    But in AS3 how do you do something like this, all I can do is get error messages, when I try to use functions and addEventListener(Event.ENTER_FRAME, whatever); and all this crazy AS3 stuff it takes so much what seems to me like backwards ass coding to do a very simple gotoAndStop request.
    As mentioned above by someone else, best practices in how to write your code will solve alot of your problems conceptually. Because you don't understand certain aspects of programming structures vs quick scripting, you are having trouble conceptualizing what's going on in the flash engine.

    Also you need to understand how the event flow works on the Display. The amount of code is circumstantial, according to what you are trying to accomplish, but let me make an analogy for you that may help.

    In AS2 If a the movieclip _root.northAmerica.florida.orlando wanted to call a frame on _root.northAmerica.canada.montreal, then you would need these long paths in order to reference them. However, if orlando is a movieClip or Sprite, etc... then it can simply dispatch a custom event. or even more simply, montreal can just register to listen for MouseEvent.CLICK and use an if conditional:

    Code:
    if (event.target == orlando)
    {
        // perform gotoAndStop here
    }
    To take that even further, you could do all your listener registrations on a specific timeline or class so you dont have a bunch of code snippets spread all about the place. Why? Because all Events on the display propagate on the base of the display. Then they filter down through every item attached to the display. And, if it's a bubbling event, then it will bubble back through to the base again (Round trip event).


    So say you want to build a website with bunches of buttons. You could have 1 click handler on the main timeline or class and just filter the target like so:

    Code:
    // on main timeline or main class
    
    this.addEventListener(MouseEvent.CLICK, myClickHandler);
    
    function myClickHandler(e.MouseEvent)
    {
        switch(e.target)
        {
            case button1:
                // do button actions here
                break;
    
            case button2:
                // do button actions here
                break;
    
            case button3:
                // do button actions here
                break;
        }
    }
    We are able to catch all MouseEvents on the main timeline / class because this is the base of the display and all events will start here. So, depending on how you build your .fla and it's assets, with a little planning, you can code a monster amount of functionality in 1 place (or a few classes if you wish) with less code, less chances for error and type-o's etc.

    Something else to consider - the above filtering process reduces the amount of registered listeners and thus increases performance. In something like a game application, this could come in extremely handy.

    That's alot easier than having on(clipevent) scripts and the like scattered all through your .fla.

    Along with the book recommended above, Essential ActionScript 3.0 by Colin Moock will greatly help. Chapters 1 - 7 will get you understanding classes, functions, and how to use them. Chapter 20 will get you up to speed on the Display Heirchy, and Chapter 21 goes specifically into Event Flow on the display.
    The Early Bird may get the worm, but the second mouse to the trap gets the cheese...

  17. #17
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    With all due respect to Moock (he definitely is among the elite), his book is not made for novices. I know because I started on his book as a novice, and after getting 200 pages in realized I wasn't taking much out of it. I revisited his book several months ago after reading a few different books and building an array of different applications and it helped improve my overall understanding of OOP, but I feel the problem with his book is that you run through his tutorials and rarely get to see the script you've written in action, simply because there is always a 'little more' that needs to be scripted before anything will function properly. I'm sure Essential Actionscript 3 is beneficial to a lot of people, but as someone migrating from an AS2 environment to AS3 (such as me a year ago), this book is garbage. I wish Learning Actionscript 3.0 was out when I was making the transition, because this book is THE BEST book for folks that are in the boat I was in.
    Follow me on Twitter: http://twitter.com/jasondefra

  18. #18
    Senior Member Gohloum's Avatar
    Join Date
    Aug 2002
    Location
    Florida
    Posts
    185
    Then I would have to say your knowledge of AS2 isn't that vast either. If you read Essential ActionScript 2, you will find the first 7 chapters are almost like the first 7 of AS3 book. So your problem goes beyond the language barrier but into understanding how to implement classes and structures (which is what the first several chapters do in both books).

    It's like learning to tune a car or work with any kind of complex mechanism. If you don't have an understanding of what's going on under the hood, you won't be able to effectively tune the car (or whatever the case may be).

    Your question was how to learn event handling in AS3 and you said you know AS2. If you are programming in true AS2 practices (well formed code, classes, datatyping variables, etc), then the hurdle to AS3 really comes down to the Display Heirchy, and the Event Model (unless you are dealing with alot of url and server calls, then you will find that stuff to be quite different as well).

    However, if the Moock book is giving you that much trouble in the first 7 chapters (which are the ones teaching about simple classes, structures, etc), then I would suggest you find a way to learn how to write applications in this manner because not only are you fighting with syntax, but you are fighting with theory, implementation and architecture all at the same time.

    On the flip side, why would you even want to code something in AS3 if you are not using OOP practices? If it's scripting quick and dirty you want to do, then by all means do it in AS2. It's more forgiving.

    I can't make a the call on what you should do, I don't know your skill level. What I can tell you is before I was coding AS3, I was coding well formed AS2, so when I finally did my first project in AS3, it was a bit rough, but it took me about a month before I was good and comfortable with it. Is that fast or slow for a transition? I couldn't tell you because I don't have any metrics to measure it by. What I can tell you is that once I learned how the display worked and understood the new Event model and how it works, then everything became pretty easy.

    There was a post some time ago on AS2 vs AS3. It got rather long and obnoxious, but it appeared that the OOP coders and application developers preferred AS3 where the designers and scripters preferred AS2 (in most cases anyway).

    It may come down to AS2 being better for you. There is certainly nothing wrong with that. Why should one go through all the trouble to get out an extension cord, plug in the compressor, hook up a nail gun(AS3) and then pull the trigger to drive 1 silly nail in.... Just grab the freakin' hammer (AS2), give it 3 whacks and done!

    It's more about the right tool for the right job. Don't frustrate yourself to do something in AS3 if there are no real benefits for your project.
    The Early Bird may get the worm, but the second mouse to the trap gets the cheese...

  19. #19
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Moock's book, Essential Actionscript 3, sucks for novices. There's no point in trying to argue otherwise because I'm right.

    You weren't a programming novice when you jumped into AS3. I was. The book sucks.

    edit: HOWEVER, it is a great resource for learning the OOP side of things once a person has the ball rolling in AS3.
    Follow me on Twitter: http://twitter.com/jasondefra

  20. #20
    Senior Member Gohloum's Avatar
    Join Date
    Aug 2002
    Location
    Florida
    Posts
    185
    Your statements are conjecture and not grounded in any sense of logic:

    (I know AS2)
    You should have said "I am familiar with ActionScript". This would better describe your level of skill although somewhat ambiguous. To say you KNOW AS2 implies to most programmers in the community that you are at least in practice of using classes and datatyping your methods and variables - Especially when you inquire in your heading about understanding events in AS3, which is a tricky topic in itself and I definately wanted to help out if I could.

    The book sucks... Essential Actionscript 3, sucks for novices... this book is garbage
    Dude, is it really necessary for you to make such charged and untrue statements? I don't know Colin from Adam... Never met or spoken with any actionscript author, but I don't go around insulting their work either, regardless of what I think of it.

    To be honest with you, I took time out of my productive hours of my work day to write you a somewhat detailed response on Events. Then when you responded, I sensed your frustration and tried to give you some rational reasons for staying in AS2. This was not to be insulting or degrading, but as I explained - the right tool for the right job.

    There's no point in trying to argue otherwise because I'm right.
    Excuse me but is known as a Fallacy of Equivication.

    You are a novice
    Moock's book didn't work for you
    Therefore Moock's book is not for novices

    That one shure won't stand up before a jury...

    So by now, I am sure that you (as well as a few others reading this post) are wondering why I am going through all this explanation? Well it's pretty simple:

    At some point in our careers, we were all novices. And as many do now, we all have used the forums to ask questions, get help, etc. So knowing that your are posting to your peers who are more skilled, it's common courtesy to be respectful to others (book authors included) and therefore if you are frustrated or emotionally charged in some way, make sure you read your posts and think about what you type before you submit it.

    So I could have taken the position of "Hey, I'm actually spending my time trying to help you and you respond to me as a novice know-it-all?!"

    No, I didn't, did I? What I am actually saying to you is "Hey, I don't mind spending my time helping and would like to assist you, so in the future would you please be so kind as to be respectful, logical, and as factual as possible in your posts?"

    Understand?
    The Early Bird may get the worm, but the second mouse to the trap gets the cheese...

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