[TUT] Adding dynamic music to the AS3 client/server

Posts 111 of 11 · Page 1 of 1
[TUT] Adding dynamic music to the AS3 client/server
Dynamic music is something I made a long time ago in phoenix that I expect about every server on here has. However, if you started messing with the recently released AS3 client source, you may have noticed that it isn't there. No need to worry, as I already have the AS3 code for all of it in Seraphs' Dominion.

Note that you must be using the same AS3 client AS WELL AS the same server source uploaded by Kithio. If you don't want to use that server source, I'm not helping you.

PART ONE: THE CLIENT




Before I say anything, it's important to know that you can change the URL to your sfx folder which contains your music folder in com/company/assembleegameclient/parameters/Parameters.as.


Step 1 You want to open up the AS3 client and navigate to _vf/_gs.as. You'll want to replace the entire file with this code:
Code:
// Decompiled by AS3 Sorcerer 1.99
// http://www.as3sorcerer.com/

//_vf._gs

package _vf {
    import flash.media.Sound;
    import flash.media.SoundChannel;
    import flash.net.URLRequest;

    import com.company.assembleegameclient.parameters.Parameters;

    import flash.media.SoundTransform;

    public class _gs {
        public static const menuMusic:Vector.<String> = new <String>["sorc", "Menu", "Menu2"];
        public static var fadeIn:Number = 0.65;
        public static var fadeOut:Number = 0;
        public static var music_:String = "Menu";
        private static var currentSoundChannel:SoundChannel = null;
        private static var currentSoundTransform:SoundTransform = null;
        private static var newSound:Sound = null;
        private static var newSoundChannel:SoundChannel = null;
        private static var newSoundTransform:SoundTransform = null;

        public static function load():void {
            var currentSound:Sound = new Sound();
            currentSound.load(new URLRequest((("http://" + Parameters.musicUrl_) + "/sfx/music/" + randomMenu() + ".mp3")));
            currentSoundTransform = new SoundTransform(Parameters.data_.playMusic ? 0.65 : 0);
            currentSoundChannel = currentSound.play(0, int.MAX_VALUE, currentSoundTransform);
        }

        public static function _continue(_arg1:Boolean):void {
            Parameters.data_.playMusic = _arg1;
            Parameters.save();
            currentSoundChannel.soundTransform = new SoundTransform(((Parameters.data_.playMusic) ? 0.65 : 0));
        }

        public static function randomMenu():String {
            return menuMusic[Math(dot)round(Math.random() * (menuMusic.length - 1))];
        }

        public static function reload(name:String):void {
            if (music_ == name) return;
            music_ = name;
            try {
                if (fadeIn < 0.65 && newSoundChannel != null) {
                    endFade();
                }
                newSound = new Sound();
                newSound.load(new URLRequest("http://" + Parameters.musicUrl_ + "/sfx/music/" + (name == "Menu" ? randomMenu() : name) + ".mp3"));
                newSoundTransform = new SoundTransform(Parameters.data_.playMusic ? 0.65 : 0);
                newSoundChannel = newSound.play(0, int.MAX_VALUE, newSoundTransform);
            } catch (e:Error) {
                trace(e);
            }
            //if (fadeIn < 0.65) endFade();
            fadeIn = 0;
            fadeOut = 0.65;
        }

        public static function endFade():void {
            try {
                currentSoundChannel.stop();
            } catch (er:Error)
            {
                trace(er);
            }
            currentSoundChannel = newSoundChannel;
            currentSoundTransform = newSoundTransform;

            currentSoundTransform.volume = Parameters.data_.playMusic ? 0.65 : 0;
            fadeIn = 0.65;
            fadeOut = 0;
        }

        public static function updateFade():void {
            if (fadeIn >= 0.65) return;
            if (!Parameters.data_.playMusic) {
                endFade();
                return;
            }
            try {
                fadeIn += 0.0065;
                fadeOut -= 0.0065;
                if(fadeOut < 0) {
                    fadeOut = 0;
                }
                currentSoundTransform.volume = fadeOut;
                newSoundTransform.volume = fadeIn;
                currentSoundChannel.soundTransform = currentSoundTransform;
                newSoundChannel.soundTransform = newSoundTransform;
            } catch (e:Error) {
                trace(e);
            }
            if (fadeIn >= 0.65) endFade();
        }

    }
}//package _vf
Then you replace the (dot) with a period because MPGH is retarded and censors Math(dot)round

I should also note that this line of the code:
Code:
public static const menuMusic:Vector.<String> = new <String>["sorc", "Menu", "Menu2"];
is the random array of menu music that can play. You can change that to whatever you have.

Step 2 Navigate to _02t/_pM_.as, find the onEnterFrame function, and paste this code at the start of it:
Code:
if (_vf._gs.music_ != "Death" || _vf._gs.music_ != "Menu") _vf._gs.reload("Menu");
_vf._gs.updateFade();
You'll also have to import _vf._gs

Step 3 Navigate to com/company/assembleegameclient/game/GameSprite.as, find the onEnterFrame function, and paste this code at the start of it:
Code:
_gs.updateFade();
You'll also have to import _vf._gs

Step 4 Navigate to com/company/assembleegameclient/net/_1f.as, find the _038 function, and paste this code at the start of it:
Code:
_vf._gs.reload("Death");
You'll also have to import _vf._gs

Step 5 Here's where it gets difficult. Navigate to com/company/assembleegameclient/map/_X_l.as.
First, you'll want to add a new string variable called music_. It looks like this:
Code:
public var music_:String;
Place it where the other variables are at the top of the class.
Then, you'll want to replace this function:
Code:
        public function _ti(_arg1:int, _arg2:int, _arg3:String, _arg4:int, _arg5:Boolean, _arg6:Boolean):void{
            this.width_ = _arg1;
            this.height_ = _arg2;
            this.name_ = _arg3;
            this._vv = _arg4;
            this.allowPlayerTeleport_ = _arg5;
            this.showDisplays_ = _arg6;
        }
with this one:
Code:
        public function _ti(_arg1:int, _arg2:int, _arg3:String, _arg4:int, _arg5:Boolean, _arg6:Boolean, _arg7:String):void{
            this.width_ = _arg1;
            this.height_ = _arg2;
            this.name_ = _arg3;
            this._vv = _arg4;
            this.allowPlayerTeleport_ = _arg5;
            this.showDisplays_ = _arg6;
            this.music_ = _arg7;
            _vf._gs.reload(this.music_);
        }
You'll also have to import _vf._gs

Step 6 Next, you'll want to fix the usages of the function you just changed.
First, navigate back to _02t/_pM_.as, and replace this line of code:
Code:
_local3._ti((_sl.x_ + (2 * BORDER)), _sl.y_, "Background Map", Background._0H_W_, false, false);
with this:
Code:
_local3._ti((_sl.x_ + (2 * BORDER)), _sl.y_, "Background Map", Background._0H_W_, false, false, "Menu");
Then, go to _K_D_/_p0.as and replace this line of code:
Code:
_local3._ti(_local2["width"], _local2["height"], _local2["name"], _local2["back"], false, false);
with this:
Code:
_local3._ti(_local2["width"], _local2["height"], _local2["name"], _local2["back"], false, false, "Menu");
Finally, go back to com/company/assembleegameclient/game/GameSprite.as, and replace this line of code:
Code:
this.map._ti(_arg1.width_, _arg1.height_, _arg1.name_, _arg1.background_, _arg1.allowPlayerTeleport_, _arg1.showDisplays_);
with this one:
Code:
this.map._ti(_arg1.width_, _arg1.height_, _arg1.name_, _arg1.background_, _arg1.allowPlayerTeleport_, _arg1.showDisplays_, _arg1.music_);
Note that this will still give an error, that'll be fixed here shortly.

Step 7 You will be modifying the MapInfo packet in here. This means that your client will not correctly recieve the MapInfo packet and will break if you don't modify the server to send out the correct information. That will be covered later in the tutorial. I'll be using screenshots for this part. Add the lines that are outlined in red.
Navigate to _011/_wx.as.







PART TWO: THE SERVER




Step 1 Navigate to wServer/realm/World.cs and make these basic changes:





Afterwards, put these functions in the same class:
Code:
        public void SetMusic(params string[] music)
        {
            Music = music;
        }

        public string GetMusic()
        {
            if (Music.Length == 0)
                return "null";
            var rand = new wRandom();
            return Music[rand.Next(0, Music.Length)];
        }
Step 2 Navigate to wServer/networking/svrPackets/MapInfoPacket.cs and add these:







Step 3 Navigate to wServer/networking/handlers/HelloPacketHandler.cs and add this one line:



Step 4 You're done! Now you just go into the world files you want and add their music!

Holy a lot of steps. Thanks for the tut though.
Quote Originally Posted by sacredmike View Post
Holy a lot of steps. Thanks for the tut though.
Holy a lot of steps is right. I've been working on this for 3 hours and I already had all the code. I'm probably just slow at writing tutorials though.
Quote Originally Posted by Travoos View Post
Holy a lot of steps is right. I've been working on this for 3 hours and I already had all the code. I'm probably just slow at writing tutorials though.
You aren't the only one.
Very good job, thank you for the tutorial
@Travoos

Is this normal ? File: _1f.as

Quote Originally Posted by ~V~ View Post
@Travoos

Is this normal ? File: _1f.as

Yeah, I noticed that when I was re-adding this to my client. Very strange.
Just replace that line with this:
Code:
var _local2:_j_ = new _j_(new Server(_arg1.name_, ((_arg1.host_)!="") ? _arg1.host_ : this.server_._09v, ((_arg1.host_)!="") ? _arg1.port_ : this.server_.port_), _arg1.gameId_, this._96, this.charId_, _arg1.keyTime_, _arg1.key_);
I don't know why that error happens.
Quote Originally Posted by Travoos View Post
Yeah, I noticed that when I was re-adding this to my client. Very strange.
Just replace that line with this:
Code:
var _local2:_j_ = new _j_(new Server(_arg1.name_, ((_arg1.host_)!="") ? _arg1.host_ : this.server_._09v, ((_arg1.host_)!="") ? _arg1.port_ : this.server_.port_), _arg1.gameId_, this._96, this.charId_, _arg1.keyTime_, _arg1.key_);
I don't know why that error happens.

Even if its work without errors client still giving me Loading error,retrying...
But thanks anyway
Could you please tell me what you mean with "You'll also have to import _vf._gs"

Thanks!
Quote Originally Posted by Tjeerdo View Post
Could you please tell me what you mean with "You'll also have to import _vf._gs"

Thanks!
Put this at the top import _vf._gs;
Posts 111 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?