What is wrong with this as3 piece of code? - actionscript-3

I'm an as3 newbie and I'm trying to send a message through netstream to a function that will handle it.
Here is the line that sends
public function _attachLocalVideoAndAudio(muteCam:Boolean = false, muteMic:Boolean =
false):void
{
...
_outgoingStream.send("flagVideo",true);
...
}
public function flagV():Boolean
{
var client:Object;
client.flagVideo=function(flag:Boolean):void{
check=flag;
}
return check;
}
*check is defined
I'm using Monster Debugger to debug and it seems that there is a problem with the inner function that handles the flag. There isn't much tutorials on the web on netstream.send() but I wrote this code based on something I saw online.

Related

Parsley Command Decoupled Result Handlers and Observers

I posted a question last night that after reading back sounded awful, so I deleted it and have come back to try again, this time properly.
I have a Flex Mobile App, that uses Parsley, everything works as expected but I am trying to do use a decoupled result handler in my controller, but it is not firing when I expect it to, so would like a pointer as to why.
The command look like this:
public function execute():void
{
var asyncToken:AsyncToken = Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
asyncToken.addResponder(new Responder(result, error));
}
public function result(result:ResultEvent):void
{
callback(result.result);
}
public function error(event:FaultEvent):void
{
callback(event.fault);
}
Which works as expected, the command is executed and the result handler handles the result, the problem comes when I try to put a message handler in the controller for the view.
[CommandResult]
public function handleResult(result:AsyncToken):void
{
trace("result in the controller");
}
[CommandError]
public function handleError(fault:AsyncToken):void
{
trace('error: ' + fault.fault.faultDetail);
}
Neither of these listeners fire when a result arrives, so I did the obvious thing and changed the code to:
[CommandResult]
public function handleResult():void
{
trace("result in the controller");
}
[CommandError]
public function handleError():void
{
trace('fault in controller);
}
Now it fires, but I have no data handle.
I did think of changing the commands execute method to
public function execute():AsyncToken
{
return Db.Instance.ViewChildren(mainModel.loggedInUser.userId);
}
as after all it does return an AsyncToken, but then the command doesn't fire at all (it is part of a 2 command sequence that is mapped to an event called ChildEvent, this is the second and last event in the chain.
So in summary, I want the above to work, but want to be able to manage the result in the decoupled result handler, but I can't work out how, the parsley manual is great for getting to this point (http://www.spicefactory.org/parsley/docs/3.0/manual/?page=commands&section=intro), but the finer details are a little sketchy.
Thanks
With a small tweak to the Controller code, we end up with this:
[CommandResult(type="view.user.UserEvent", selector="loadUser")]
public function handleResult(result:Object):void
{
trace("this is the command result");
}
OR
[CommandResult(selector="loadUser")]
public function handleResult(result:Object, trigger:UserEvent):void
{
trace("this is the command result");
}
Now this fires, I get an Object with my data in, resolved.
It would be useful to note that the manual for Parsley 3.0 misses out the section that explains how this actually works. I eventually found it in the Parsley 2.2 manual (the equivalent section in the 3.0 manual has been removed!) But if you ever need it http://www.spicefactory.org/parsley/docs/2.2/manual/messaging.php#command_methods
Thanks everyone!

AS3: Call function does not work in browser

Here I have AS3 to for uploading recorded sound file to server. When I test it in Flash it works properly (record sound and upload it and goes to next frame) , but in browser it doesn't work. It seems can't call myUpload but I don't why? Is it should be mouse event? Thanks.
function VOCWordToYourMp3()
{
setTimeout(startRecording,3000);
recorder.addEventListener(RecordingEvent.RECORDING, onRecording);
recorder.addEventListener(Event.COMPLETE, onRecordComplete);
}
function startRecording()
{
if (! recording)
{
recorder.record();
}
else if (recording)
{
recorder.stop();
recording = false;
}
}
function onRecording(e:RecordingEvent)
{
//
}
function onRecordComplete(e:Event):void
{
//
}
function renderWav(src, convertToMp3 = false)
{
//
function handleRenderTimer(e:TimerEvent)
{
//
}
function finishRender()
{
//
}
}
function makeIntoMp3(wav)
{
wav.position = 0;
mp3Encoder = new ShineMP3Encoder(wav);
mp3Encoder.addEventListener(Event.COMPLETE, mp3EncodeComplete);
mp3Encoder.addEventListener(ProgressEvent.PROGRESS, mp3EncodeProgress);
mp3Encoder.start();
function mp3EncodeProgress(e:ProgressEvent):void
{
//
}
function mp3EncodeComplete(e: Event):void
{
myUpload('sound1',mp3Encoder.mp3Data);
}
}
function myUpload(namefile:String,sba: ByteArray):void
{
//upload code
}
Update:
In Flash Player 10 and Actionscript 3.0, all the calls to URLLoader must be in the same callstack.
http://helpx.adobe.com/flash-player/kb/user-interaction-required-upload-download.html
What is same callstack mean?
I recommend using something like Vizzy: https://code.google.com/p/flash-tracer/ to debug your swf in the browser. You can put trace statements into the onRecordComplete() and myUpload() functions, etc, to see how far the code is getting, and to see if you are getting any new errors. You may have some kind of security sandbox error because you are running in the browser. Being able to see what that error is will help you figure out what to do next.
To use Vizzy, you need to have the debug player running in your browser, and to configure the right path to your log file. This is sometimes a little bit tricky, so I'll give you some tips:
Add a file to your home directory called mm.cfg, and populate it with these settings:
ErrorReportingEnable=1
AS3Verbose=0
TraceOutputFileEnable=1
AS3Trace=0
TraceOutputBuffered=0
AS3StaticProfile=0
Then in Vizzy you have to set up the path to the log. On my windows 7 machine it is here:
C:\Users\MyUserName\AppData\Roaming\Macromedia\Flash Player\Logs\flashlog.txt
It may be different depending on your operating system.
Good luck, and report back if you Vizzy gives you any new information.

Calling functions in Actionscript: "Term is undefined and has no properties"

I'm writing a genetic fitness program, and I'm currently writing some code that will calculate the 'fitness' value of each organism.
I'm trying to call a function that initializes each genotype;
function random_genotype_initialisation():void
{
//stuff
}
By using the typical method-calling I'm used to in C# and Java;
random_genotyoe_initialisation();
However this returns the error: "TypeError: Error #1010: A term is undefined and has no properties."
I've looked elsewhere for help, and I've found suggestions such as declaring a variable and 'calling' that.
var rep = replicate_new_generation();
rep.call();
Any suggestions?
I assume there's an undefined value inside your random_genotype_initialisation() function. You are correct, you call a function in as3 same as in java/c/c++/c#/js/etc.
The snippet is a bit wrong because you store the result of the replcate_new_generation into rep, but that is a void function, so rep will be void and therefore not have call():
var rep = replicate_new_generation();//rep = void at this point
rep.call();//call does not exist for rep
Do you mean ?
var rep:Function = replicate_new_generation;
rep.call();
Which is same as: replicate_new_generation();
If the error is generating within the function perhaps you should post the body
of the function as well ?
C# is VERY similar to AS3, I have no idea what you are trying to do or why your code did not work since you didn't provide a very good example. But you can call functions directly as long as it is accessible just in any normal way.
say, inside a class you have:
class Boo {
private function foo():void {
trace("bar");
}
public function foobar():void {
foo();
}
}
class FoobarCaller {
public function FoobarCaller (){
var asdf:Boo = new Boo();
asdf.foobar();
}
}
that works, just as it would in C# or any other "standard coding language. However, without a better question it's impossible to say what it is you have done wrong

LIBGDX: How can i tell when a sound has finished playing?

The Sound API seems to be missing a function to indicate that a sound is finished playing. Is there some other way of finding out if the sound is done?
Not without submitting a patch to libgdx as far as I know the underlying Sound Backends for both OpenAL and Android don't even track the information internally, though the Music API has an isPlaying() function and getPosition() function as per the documentation.
just set this
sound.setLooping(false);
this way it will not run again and again.
and to check whether sound is playing or
not do this.
make a boolean variable
boolean soundplaying;
in render method do this
if(sound.isPlaying()){
soundplaying =true
}
and make a log
gdx.app.log("","sound"+soundplaying);
You can track this by storing the sound instance id that e.g. play() and loop() return. Example code:
private Sound sound;
private Long soundId;
...
public void startSound() {
if (soundId != null) {
return;
}
soundId = sound.loop(); // or sound.play()
}
public void stopSound() {
sound.stop(soundId);
soundId = null;
}
If you didn't want to have to call stopSound() from client code, you could just call it from startSound() instead of the return, to ensure any previous sound is stopped.

flexunit addAsync chaining

For some reason, addAsync chaining in a flexunit test as described in this article utterly fails to work when I try to do it.
public function testWhatever():void {
var cont:EventDispatcher = new EventDispatcher();
cont.addEventListener("continue", addAsync(verifyFirst, 1000));
cont.dispatchEvent(new Event("continue"));
}
private function verifyFirst(e:Event):void {
var cont:EventDispatcher = new EventDispatcher();
cont.addEventListener("continue", addAsync(verifySecond, 1000));
cont.dispatchEvent(new Event("continue"));
}
private function verifySecond(e:Event):void {
assertTrue(true);
}
If I run this test, verifyFirst gets called but verifySecond does not. I'm assuming this is a bug in flexunit ... is there a workaround?
I did some more research and found that this is indeed a bug in flexunit, which looks to be fixed in the next release. The workaround I found was to instead use Application.application.callLater to dispatch the second event.
private function verifyFirst(e:Event):void {
var cont:EventDispatcher = new EventDispatcher();
cont.addEventListener("continue", addAsync(verifySecond, 1000));
Application.application.callLater(cont.dispatchEvent,
[new Event("continue")]);
}
This question was inspired by an attempt to inspect the state of an object after everything in Flash's event queue had been processed. I discovered a simpler way to accomplish this without messing with EventDispatchers.
Make the following call the end of the first part of the test when you want the event queue to be processed.
Application.application.callLater(addAsync(phaseTwo, 1000, [args...]), [null]);
With the phaseTwo function having the following signature.
private function phaseTwo(e:Event, args:Array):void
e will be passed a null object. This is necessary to be compatible with addAsync.