TwelvestoneFlash

AS3 - code structure help - use random class


Sign in

  • Waiting for Godot ( 720 k posts )
    Just conversation.
  • Thunder Dome ( 23 k posts )
    Photoshop Tennis and Collabs.
  • Photography ( 4.8 k posts )
    For all you shutterbugs, sh...
  • Flash ( 18 k posts )
    ActionScripting to tweens, ...
  • Front End ( 5.8 k posts )
    general front end design an...
  • Back End ( 9.6 k posts )
    serverside scripting, progr...
  • Projects and Theory ( 12 k posts )
    This forum is for discussio...
  • FAQ ( 269 posts )
    All those nagging questions...
  • Design ( 17 k posts )
    graphics & all aspects of g...
  • Purgatory ( 3.6 k posts )
    12stone Jail, feel free to ...
DontBogartMe
 
2008-05-29

Can someone help me clean this up a bit?

    private function playRandomAppleSound():void {
        var snd:Sound;
        var rnd:uint = Math.ceil(Math.random() * 7);
        switch(rnd){
            case 1:
                snd = new Banjo_1();
                break;
            case 2:
                snd = new Banjo_2();
                break;
            case 3:
                snd = new Banjo_3();
                break;
            case 4:
                snd = new Banjo_4();
                break;
            case 5:
                snd = new Banjo_5();
                break;
            case 6:
                snd = new Banjo_6();
                break;
            case 7:
                snd = new Banjo_7();
                break;
        }

        var chnl:SoundChannel;

        chnl = snd.play();
    }

Those Banjo1, Banjo_2 classes are for library sound clips of notes played on a banjo. In AS2 I could've done this neater by dynamically creating the linkage ID I wanted, e.g. "Banjo" + rnd.

Is there a way to do similar in AS3?

JLM
 
2008-05-29

Not tried it but a really bad one liner may work?

(new ( getDefinitionByName( 'Banjo_'+String(Math.ceil(Math.random() * 7)) ) as Class )).play();

DontBogartMe
 
2008-05-29

awesome thanks JLM:

    private function playRandomAppleSound():void {
        var snd:Sound;
        var rnd:uint = Math.ceil(Math.random() * 7);

        var RandomBanjoClass:Class = getDefinitionByName("Banjo_" + rnd) as Class;
        snd = new RandomBanjoClass();

        var chnl:SoundChannel;          
        chnl = snd.play();
    }

works a charm k

Arsis
 
2008-05-30

Originally posted by: JLM

(new ( getDefinitionByName( 'Banjo_'+String(Math.ceil(Math.random() * 7)) ) as Class )).play();

lol.. I have a frustrating client at the moment and I think I might do that to every line of AS before hand over the source k

JLM
 
2008-05-30

there is now a haxebundle for a haxe and it compiles from textmate I managed to get the hello worlds to work last night, why not port it (there is a as3 generator if they then hassle you). I am sure that will fox them ps I think there maybe an error in the line above.

Sorry, you must be a member to post to a conversation. Either log in or sign up to get involved.
TwelvestoneFlash

AS3 - code structure help - use random class