A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [F8] How to write a method that can be altered depending on circumstance at run time

  1. #1
    Member
    Join Date
    May 2007
    Posts
    46

    [F8] How to write a method that can be altered depending on circumstance at run time

    Hiya people!

    I have an issue with something, i have been getting into Flash Lite and basically i eventually found a way to navigate with the keypad in a class...

    var array:Array = new Array ("Start","Options","Help","About");
    var selected:Number = 0;

    //keypress up =
    selected --;

    //keypress down =
    selected ++;

    //keypress select =
    array[selected].onSelect();


    So basically, you can see how this works, its simple and it works for the main part of my program but i am having an issue with the Main Menu because each button does something completely different.

    The MovieClip class contains an onMouseDown... or something like that... im not infront of flash as i write this, but

    onMouseDown = function (){}

    works.

    How can i create a public method that all buttons use but that i can set on a per button basis

    var button1:MyButton = new MyButton();
    button1.onSelected = function (): Void
    {
    //code for button 1... say it draws a circle
    }

    var button2:MyButton = new MyButton();
    button2.onSelected = function (): Void
    {
    //code for button2... say it draws a rectangle
    }

    Im unsure how to implement this in code. Can someone give me helping hand?

    Cheers!

  2. #2
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    I don't understand what you're asking. The first half of your post (using arrow keys to cycle through an array) seems to have absolutely nothing to do with the second (button functions). Also, "onSelected" isn't a method of the button class or the movieclip class.

    What is the big picture of what you're trying to do here?

  3. #3
    Member
    Join Date
    May 2007
    Posts
    46
    Ahh thanks for reasding.

    Ok lets try this

    You have a flash application that has no mouse input so you need to use the keyboard, you have no choice.

    With this in mind, you have 4 buttons b1,b2,b3,b4

    When the application starts, b1 is in the selected state b1.highlighted();

    IF you pres the down arrow, then b2 is next and hence b2.highlighted();

    IF you press the down arrow once more thwen b3 is next and hence b3.highlighted();

    Each of these 4 buttons do not follow a common pattern, for example, When i press the Select button (or space button in our pc example) then depending on the highlighted button.... a specific function occurs.

    Lets say b1 has a textfield so the user knows what to do "Start Application"
    and also b2 has a textfield so the user knows what to do "Options"

    Each of these options requires comepletely different behaviour, pressing select on b1 will load up the application. where as highlighting options and pressing select should load up a options screen......

    In order for this to occur, each instance of the MyButton class needs to be sure to contain an public onSelected() method much like the MovieClip class has a public OnEnterFrame method.

    In my world i want to be able to say

    b1.onSelected() = function (): Void
    {
    //specifics go here
    }

    the movie clip class has

    mc.onEnterFrame = function(): Void
    {
    //specifics go here
    }

    See i want my MyButton class to have access to a method which i can change.

    The reason my main application works fine is because all AppButton classes function the same, all the data loaded is the same, all that changes is the image and the text but in the case of MyButton (for the main menu) each onSelected() has a completely different function when pressed....

    Does this make any sense?

  4. #4
    Member
    Join Date
    May 2007
    Posts
    46
    Just to make it really clear, the only other option i can think of is to have 4 classes:

    MyStartAppButton.as
    MyOptionsButton.as
    MyHelpButton.as
    MyAboutButton.as

    Clearly seeing as all of these buttons hold pretty much the same functionality apart from 1 function that differs (onSelected()), then it seems too much effort (and not correct) to do it this way and im really just looking for ideas as how else i can do what im trying to do.

  5. #5
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    Thanks for the clarification
    To me it seems more intuitive to think of this from the keyboard's perspective. You already have a function that listens to the keyboard's input, and iterates through an array, and assigns things based on this, etc. Plus the event is happening at the key level. So instead of adding methods to classes, why not make one function/class/whatever that incorporates all the shared functionality, and a function or class for each individual button that the keyboard listener, which you already must have, can simply call. You certainly don't need to make separate AS files, but obviously you do have to separate the statements apart from button to button, so however you choose to do this part is up to you (though i don't see what's 'incorrect' about having different functions/classes in different files). so maybe something like:

    PHP Code:
    function b1Actions(){
      
    //do this
    }

    function 
    b2Actions(){
      
    //do that
    }

    function 
    sharedActions(){
      
    //do these

    then you can just call the appropriate combo based on the logic you already have in place from your first post. Always call the sharedActions(), and then, based on what the user has keyed to - b1, b2, b3, or b4 - call the associated bNActions()

    PHP Code:
    yourKeyboard Listner.onKeyDown = function(){
      
    //logic for sensing appropriate 'select' key
      //detect what button is 'selected'
      //execute associated action
      //execute sharedActions() regardless of key

    _b

  6. #6
    Member
    Join Date
    May 2007
    Posts
    46
    Hey dude!

    Sorry i was so slow to reply, i had to travel pretty urgently out of the blue.

    While i was on the train today i managed to get to get to the bottom of the problem in a different way to yours (although by no means better).

    I just put all the functinality in a class called MainButton and created 4 subclasses, for each subclass i would override the onSelected() method to do some other action.

    However either way it still raises that question.

    What is MovieClip.onEnterFrame? I mean how is it defined in the MovieClip class because of the way it is used.

    To me it looks like a property... i dont know what im trying to ask.

    I thought you were not meant to access properties of a class directly? Is that what onEnterFrame is?

    class MovieClip {

    public var onEnterFrame:Function;

    }

    or is it a Method

    class MovieClip {

    public function onEnterFrame(): Void {//no code yet}

    }

    Thanks! Im going to compare your method to mine and see the difference between how they work.

    Thanks again for takin the time to write matey.

  7. #7
    Getting There! bitsk308's Avatar
    Join Date
    Jul 2000
    Location
    Phoenix, AZ
    Posts
    427
    It's neither, it's an event handler. The movieClip class naturally goes forward on frame at a time (unless told to stop or go to a specific frame or whatever). When ever the movieclip object 'enters' it next frame naturally (without being told to gotoAndStop and a particular frame) that event is picked up by this handler, and code will execute.

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