zaterdag 19 december 2015

Stencyl / HaXe : Load sound dynamically

For: Stencyl
Version: 3

The code stencyl uses to run the OGG depends on a streaming setting.
When not streaming you can influence the data, but if you have streaming set (music setting in Stencyl) then it will load the asset instead of your own changed code!

So in order to get the dynamic nature working these are the steps:

Create one Stencyl Sound object. (Only the first sound will be replaced, but you might need extra code to check for the one that you want to change)

  • Ogg import a sample. This will be changed later so it doesn’t really matter, but you have to have one!
  • Set the style to “Sound Effect”
  • In my tests it was on the main atlas

So we have a sound now. (In my example below I created a sound called ‘abc’)

This sound object is the one that we can change in our code.

In stencyl create : [Custom Import block ]

[code]

import nme.media.Sound;

import nme.media.Sound;
import nme.events.Event;
import nme.net.URLRequest;
import nme.net.URLLoader;
import nme.net.URLLoaderDataFormat;
import flash.display.LoaderInfo;
import nme.utils.ByteArray;

[/code]

Custom Code Block

[code]

public var mySound:nme.media.Sound;
public var loader:nme.net.URLLoader;

public function onComplete(e:Event){
trace("In on complete!");

for(a in com.stencyl.Data.get().getResourcesOfType(com.stencyl.models.Sound)){
           trace("Found sound");
           trace("The id : "+a.ID);
         var thesound=com.stencyl.Data.get().getResourcesOfType(com.stencyl.models.Sound)[0];
         trace("type: "+Type.getClass(e.target.data));
trace("Type of theSound: "+Type.getClass(thesound));
trace("Type of theSound.src: "+Type.getClass(thesound.src));
var ba : ByteArray = e.target.data;
trace("Length of target:"+e.target.data.length);
trace("length of ba: "+ba.length);
trace("Type of the original data: "+thesound.src+" length: "+thesound.src.length);
var abcd=new flash.media.Sound();
abcd.loadCompressedDataFromByteArray(ba,ba.length);
thesound.src=cast(abcd);
com.stencyl.Data.get().resources.set(a.ID, cast(thesound,com.stencyl.models.Sound));
}
// tell Stencyl we are done with it!
setGameAttribute("DoneIt",true);

}


[/code]


The code is containing a call-back function that is called when the load of the data is done.
In the following code block I am using an actor value that contains the URL string with the dynamic OGG. This code is place in a mouse event on another actor to test the load.

[code]

// example string
//var f:String="https://dl.dropboxusercontent.com/u/107982821/abc/sound.ogg";
var url:String=actorOfType.getActorValue("Value");

loader=new nme.net.URLLoader();
// tell the loader we are going to load binary !!!
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE,onComplete);
loader.load(new URLRequest(url));


[/code]


The code uses a game attribute (DoneIt) to tell Stencyl to play the sound:

do every 0.1 seconds
<if [‘value of game attribute with name [DoneIt]>
set game attribute with name [DoneIt] to <false>
Play [abc]

Geen opmerkingen:

Een reactie posten