PDA

View Full Version : Order of commands being executed


compsci
12-26-2008, 04:53 PM
Ok, I call a function (A) which in turn calls another function (B). So B completes than the rest of A is completed, this is how it works right?

//prepares the camera..
function testme():void{
//create a new transformation matrix
var scaleMatrix:Matrix = new Matrix();
//scale the matrix
scaleMatrix.scale(4,4);
...
urlLoader.load(myRequest);
urlLoader.addEventListener(Event.COMPLETE, onLoaded);
}
//#########START HERE########button is clicked, it calls two functions
captureBtn.addEventListener(Event.CHANGE, changeHandler);

function changeHandler(event:Event):void {
pleaseWait();
testme();
}

function pleaseWait(){
loadit.load(new URLRequest('loading2.swf'));//test loading
myMovie.addChild(loadit);
addChild(myMovie);
}



The thing is, my pleasewait() function happens after my testme function()! My preloader comes after everything has happened?!

How do I troubleshoot?!

Thanks for any help. :)

CyanBlue
12-26-2008, 05:10 PM
Well... Flash is not going to wait until the pleasewait() function is done done... The changeHandler() will call pleaseWait() and then call testme() at one, anc your pleaseWait() has a routine that will probably take a bit longer than other function because it is loading an external file...

compsci
12-26-2008, 05:45 PM
take a bit longer than other function because it is loading an external file...That's what I though too at once, but then I added a COMPLETE event. Here is my full script, what could it possible be!
//prepares the camera..
function testme():void {
//COULD IT BE THIS FUNCTION IS DOING A LOT OF WORK and it effects the rest??
}

captureBtn.addEventListener(Event.CHANGE, changeHandler);

function changeHandler(event:Event):void {
pleaseWait();
}

myMovie = new MovieClip();
loadit.load(new URLRequest('loading2.swf'));//test loading
loadit.contentLoaderInfo.addEventListener(Event.CO MPLETE,onComplete);
var completeImage:Boolean = false;

function pleaseWait() {
myMovie.x = 8;
myMovie.y = 108;
myMovie.addChild(loadit);
addChild(myMovie);
if(completeImage){
testme();
}
}

function onComplete(ev:Event):void {
trace('Done');
completeImage = true;
}
:confused:

CyanBlue
12-26-2008, 06:12 PM
Your if statement is already run by the time your onComplete handler fires...
The onComplete should be the one that triggers the testme() function...
//
//prepares the camera..
function testme(e:Event):void {
//COULD IT BE THIS FUNCTION IS DOING A LOT OF WORK and it effects the rest??
}

captureBtn.addEventListener(Event.CHANGE, changeHandler);

function changeHandler(event:Event):void {
pleaseWait();
}

myMovie = new MovieClip();
loadit.load(new URLRequest('loading2.swf'));//test loading
loadit.contentLoaderInfo.addEventListener(Event.CO MPLETE,onComplete);

function pleaseWait() {
myMovie.x = 8;
myMovie.y = 108;
myMovie.addChild(loadit);
addChild(myMovie);

myMovie.addEventListener("runTestMe", testme, false, 0, true);
}

function onComplete(ev:Event):void {
trace('Done');
dispatchEvent(new Event("runTestMe", true));
}

compsci
12-26-2008, 06:30 PM
The onComplete should be the one that triggers the testme() function...Really sorry for being a noob :) - but i am trying to get the testme() function to run when a user clicks a button.

I am not sure if I got carried away with solving this problem. But the pleasewait() function should be executed by a user clicking a button which in turn will call the testme() function [somehow].

From the code we have so far this isn't happening right? Because I made the changes you made and what happens is the pleasewait() function is called perfectly and the loading.swf comes up great. But the test me function isn't run at all!

CyanBlue
12-26-2008, 11:49 PM
Maybe I am not really understanding this... Let's simplify this abit...

Create a new FLA and add a new button and name it captureBtn, and paste this code...
//
import flash.events.*;
import flash.display.Loader;

//prepares the camera..
function testme():void {
//COULD IT BE THIS FUNCTION IS DOING A LOT OF WORK and it effects the rest??
trace("Function testme");
}

//captureBtn.addEventListener(Event.CHANGE, changeHandler);
captureBtn.addEventListener(MouseEvent.CLICK, changeHandler);

function changeHandler(event:Event):void {
trace("Function changeHandler");
pleaseWait();
}

var myMovie:MovieClip = new MovieClip();

var loadit:Loader = new Loader();
loadit.load(new URLRequest('http://farm1.static.flickr.com/140/343920462_4376ff7d60.jpg?v=0'));//test loading
loadit.contentLoaderInfo.addEventListener(Event.CO MPLETE,onComplete);
var completeImage:Boolean = false;

function pleaseWait() {
trace("Function pleaseWait");
myMovie.x = 8;
myMovie.y = 108;
myMovie.addChild(loadit);
addChild(myMovie);

if (completeImage){
testme();
}
}

function onComplete(ev:Event):void {
trace('Done');
completeImage = true;
}
This code triggers the testme() function as long as you click on the button right after you see the 'Done' message, and it executes testme() function with no problem...

So, what am I missing, and what are you trying to do here???

compsci
12-27-2008, 04:54 PM
Create a new FLA and add a new button and name it captureBtn, and paste this code...It works perfectly and this is how I understood it.

So, what am I missing, and what are you trying to do here???If I do the same thing and incorporate your code into mine. I get the following:

stop();

import flash.events.MouseEvent;
import flash.media.*;
import fl.controls.Button;

var captureBtn:Button = new Button();
var lastImage:String;
var myRequest:URLRequest;
var urlLoader:URLLoader;
var myMovie:MovieClip;
var loadit:Loader = new Loader();
var previewMovie:MovieClip;
var imageLoader = new Loader();
var imageName:String;

var cam:Camera = Camera.getCamera();// our camera object.
if (cam==null) {
if (this.parent.parent != null) {
var parentObj:Object = this.parent.parent as Object;
parentObj.noCamera();
}
}
var iFps = cam.fps;
cam.setMode(640, 480, iFps);
cam.setQuality(0,100);

myvideoobject.attachCamera(cam);

function testme():void {
trace('Function testme');
//create a new transformation matrix
var scaleMatrix:Matrix = new Matrix();
//scale the matrix
scaleMatrix.scale(4,4);

var foo:BitmapData = new BitmapData(640,480);
foo.draw(myvideoobject, scaleMatrix);// this will capture the camera-data to this BitmapData-array

var myEncoder:JPEGEncoder = new JPEGEncoder(100);
var myCapStream:ByteArray = myEncoder.encode(foo);// were encoding it right in flash to JPEG..

urlLoader = new URLLoader();

var header:URLRequestHeader = new URLRequestHeader ("Content-Type", "image/jpeg");
myRequest = new URLRequest ("http://www.mydomain.com/screen.php");// dont forgett the crossdomain.xml
myRequest.requestHeaders.push(header);
myRequest.method = URLRequestMethod.POST;
myRequest.data = myCapStream;

urlLoader.load(myRequest);
urlLoader.addEventListener(Event.COMPLETE, onLoaded);
}

function onLoaded(evt:Event):void {
imageName = urlLoader.data;
trace(imageName); trace("Taken!");
removePleaseWait();
}

captureBtn.addEventListener(Event.CHANGE, changeHandler);
addChild(captureBtn);

function changeHandler(event:Event):void {
trace('Function ChangeHandler');
pleaseWait();
var myBtn:Button = event.currentTarget as Button;
ifs.color = 0xFF0000;
captureBtn.setStyle("textFormat", ifs);
myBtn.label = "Re-Capture";//is this needed?!
savebtn.enabled = true;
savebtn.setStyle("icon", BulletCheck);
preview.enabled = true;
}

myMovie = new MovieClip();
loadit.load(new URLRequest('loading2.swf'));//test loading
loadit.contentLoaderInfo.addEventListener(Event.CO MPLETE,onComplete);
var completeImage:Boolean = false;

function pleaseWait() {
trace('Function Pleasewait');
myMovie.x = 8;
myMovie.y = 108;
myMovie.addChild(loadit);
addChild(myMovie);
//myMovie.addEventListener('runTestMe', testme, false, 0, true);
if (completeImage){
testme();
}
}

function onComplete(ev:Event):void {
trace('Done Loading Image');
//dispatchEvent(new Event('runTestMe', true));
completeImage = true;
}

function onCompletePreview(ev:Event):void {
var imageLoader:LoaderInfo = LoaderInfo(ev.target);
imageLoader.content.width = 380;
imageLoader.content.height = 280;
}

function removePleaseWait() {
removeChild(myMovie);
}Please note I have removed unneccesaary stuff like other buttons and my preview function.

I get this this output:Done Loading Image
***There is a pause here, the loading image doesn't appear. Its like its running the testme function since it freezes the camera to take a picture.***
Function ChangeHandler
Function Pleasewait
Function testme ***Pause should be here!***
image1230400111.jpg
Taken!

What could be causing the delay! I don't mind the delay to be honest but it is happening at the wrong time.

CyanBlue
12-27-2008, 05:51 PM
Sorry... I honestly having problem understanding this and I have no clue... :(

compsci
12-27-2008, 06:16 PM
Sorry... I honestly having problem understanding this and I have no clue... :(Thats the result when a noob tries to explain something. :P

TEST APPLICATION: http://www.timelessthinking.com/cyanblue.php

CyanBlue
12-27-2008, 07:14 PM
Sorry... That link does not really help much on why there is a pause there, and I don't even have a web cam which does not help either... :(

compsci
12-27-2008, 08:44 PM
I don't even have a web cam which does not help either... :(Awww.

If I don't call me testme() then everything is fine, if I do there is a delay. So I know where the problem is but what about it?! Thats the frustrating part!! :mad:

I have a class called JPEGEncoder that is called by testme() and when I fire up the app (Before I even press the capture button) I get these errors: "Warning: 3596: Duplicate variable definition." Lots of these.

Its generated by the line in the testme() function:var myEncoder:JPEGEncoder = new JPEGEncoder(100);I am grasping at straws but can this be the problem?? Its just a warning.

CyanBlue
12-27-2008, 08:53 PM
No, that's just a warning you can ignore if you choose to...

The JPEGEncoder is not really slow one... You are drawing big size bitmap, 640x480, with that, but it should not be taking that long... You could try lowering that number to 300x200 or something just to see if there is any difference...

The URLRequest call might be taking abit longer than that... You are indeed passing large data with it, and there could be some lag between your call and the PHP server you have which might explain the delay...

Lastly, it would take some time, not long though, to activate the camera from the user's computer...

Not sure if any of those can help, but try those and see if you get any improvement...

compsci
12-27-2008, 09:29 PM
You are drawing big size bitmap, 640x480, with that, but it should not be taking that long... You could try lowering that number to 300x200 or something just to see if there is any difference...This did indeed speed up the delay. I am sure all the other things would make it much quicker too.

But the thing is, whats up with Flash's ordering of function calls?! I don't mind if took a minute to create the image so long as the loader showed up first. I call this function first and then create function after, but there is a delay straight away!

I have a half baked solution:var myTimer:Timer = new Timer(100);
myTimer.addEventListener("timer", testme);

As you can see, this isn't a great idea. I am introducing a deliberate pause in my application (making it slow on purpose). There are also other problems that could happen especially from not knowing whats going on and providing a fix for it anyway! :eek:

Hmm...is it the case that function calls are really quick yet addChild(myMovie) is slower?

CyanBlue
12-28-2008, 02:21 AM
I can only guess that somehow your Flash Player is stalling the whole process while you are creating large bitmap data which in fact affect the testme() function call... Other than that, the pause idea works, but you'll need to fiddle with the second to make sure you get the right pause time...

compsci
12-29-2008, 03:36 PM
I can only guess that somehow your Flash Player is stalling the whole process while you are creating large bitmap data which in fact affect the testme() function call..Yeh, I think this is the case too but if only the timing of things were done in the way I thought it would happen. :)Other than that, the pause idea works, but you'll need to fiddle with the second to make sure you get the right pause time...I hope something doesn't go wrong with this. At worst case, the timing will only be screwed, meaning one thing will happen before the other!

As always, thanks for your guidance Cyanblue :)

CyanBlue
12-29-2008, 05:22 PM
Yeah... There's gotta be some sort of notification mechanism builtin to trigger the next function, but I cannot think of one that will work on your case... :(