calling a function from inside another function - actionscript-3

I need a little help. I'm trying to create a loop of three functions.
I created the first loop but I can not get to the first function after the
3rd one is done.
I think the best solution is to call the first one inside the third one,
but I'm not sure this is the best way.
Anyway this is my code:
function LOADALL(event:MouseEvent):void{
if (ConditionC == "NotPlaying"){
ConditionC = "Playing";
var urlRequest:URLRequest = new URLRequest(Sounds_Array[i]);
var wav:URLLoader = new URLLoader();
wav.dataFormat = 'binary';
wav.load(urlRequest);
wav.addEventListener(Event.COMPLETE, playWav);
}
}
function playWav(e:Event):void{
var tts:WavSound = new WavSound(e.target.data as ByteArray);
var channel:WavSoundChannel = tts.play();
channel.addEventListener(Event.SOUND_COMPLETE, completeHandler)
}
function completeHandler(e:Event):void{
ConditionC = "NotPlaying";
trace ("hello");
LOADALL();
}
The error is in the 2nd from the end line ( LOADALL(); )
Any help?

You are trying to call the LOADALL function without any arguments, when in fact it requires one argument: event - which is probably why you are getting an error. Since you are not using event in LOADALL, just pass null, ie:
LOADALL(null);

Related

Call a random function from an array

I have three functions that I have listed in an array. Now I need a random function of the three to be called when pressing a button. However, when I press the button it calls all three functions and I'm not quite sure where I've gone wrong. It looks like this right now:
function Arm1function1(){
this.parent.parent.parent.Armfront1.visible = true;
this.parent.parent.parent.Armback1.visible = false;
}
function Arm1function2(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = true;
}
function Arm1function3(){
this.parent.parent.parent.Armfront1.visible = false;
this.parent.parent.parent.Armback1.visible = false;
}
function getRandomElementOf(Armbuttonarray1:Array):Object {
var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];
var idx:int=Math.floor(Math.random() * Armbuttonarray1.length);
return Armbuttonarray1[idx];
}
Randombutton1part1.addEventListener(MouseEvent.CLICK, Randombutton1part1Click);
function Randombutton1part1Click(e:MouseEvent):void
{
getRandomElementOf(null);
}
Any clue of where I've gone wrong?
Your issue is this line:
var Armbuttonarray1:Array = [Arm1function1(), Arm1function2(), Arm1function3()];
When populating that array, you are actually populating it with the results of the functions.
Should be:
var Armbuttonarray1:Array = [Arm1function1, Arm1function2, Arm1function3];
Notice the lack of parenthesis ().
You want to actually execute the function on the click handler, so you'll need to tweak that a bit too:
getRandomElementOf(null)();
or
getRandomElementOf(null).call();
As an aside, your getRandomElementOf function should probably look more like this:
function getRandomElementOf(array:Array):Object {
return array[Math.floor(Math.random() * array.length)];
}
Then do:
getRandomElementOf([Arm1function1, Arm1function2, Arm1function3])();

AS3 Referencing an object or variable via string etc

I have found answers 'similar' to the one I'm looking for. I really hope I didn't overlook an already answered problem.
code:
var Randy:Object = {age:32, gender:1};
var Joey:Object = {age:35, gender:1};
var slot_0 = Randy;
var slot_1 = Joey;
myFunction();
function myFunction():void{
for(var i = 0; i < 2; i++){
var thisObject = ("Slot_" + i);
trace(thisObject); // example 1
trace(thisObject.age); //example 2
}
}
it will trace in //example 1
slot_0
slot_1
*if I 'trace(thisObject)' the 'name of the Objects' ("slot_0" ; "slot_1") trace out.*
but in //example 2 I get:
Error #1069: Property age not found on String and there is no default value.
*How do I get it to understand that I want it to reference the properties of the object itself? e.g. 'trace(thisObject.age) means slot_0.age witch means Randy.age etc...*
Without a for-loop, I have to write a lot of redundant script, so I need to know this!
Thank You in advance for the help!
var thisObject = this["slot_"+i];
That's how you can do a string reference. Of course, if the var is located elsewhere, use the proper parent object.

simplifying variable.handler = function {}

I'm brushing up on my JS, and I don't know how to ask this, exactly.
I have this:
text_area.onkeyup = function() {
var text_length = this.value.length;
var text_remaining = text_max - text_length;
feed_back.innerHTML = "<strong>" + text_remaining + "</strong> charactres remaining";
}
It works. However, should I be able to take the function and pull it out to something like this?
function countDown() {
var text_length = this.value.length;
var text_remaining = text_max - text_length;
feed_back.innerHTML = "<strong>" + text_remaining + "</strong> charactres remaining";
}
and just call the function by its name?
text_area.onkeyup = countDown();
This has never worked for me, across multiple projects.
Should it? Why doesn't it?
You'll have to use:
text_area.onkeyup = countDown;
Without the parenthesis.
countDown is a reference to the actual function object countDown. It's simply a reference, and the function won't be called or executed.
countDown() actually calls the function, and evaluates to whatever it returns.
It doesn't work because () actually calls the function -- effectively setting text_area.onkeyup to the return value of countDown rather than the function itself.
Try this instead:
text_area.onkeyup = countDown;
The problem is that you're executing the function during the assignment, so the return value from the function becomes attached to the event. Instead you should attach a reference:
text_area.onkeyup = countDown
Or if countdown has parameters you want to pass with it then you can use a function to make sure they don't get lost. Something like this:
text_area.onkeyup = function(){ countDown(paremeter1, paremeter2); }
It shouldn't because you're assigning the result of a function call instead of a function reference. Should be:
text_area.onkeyup = countDown;

Clear html5 canvas, then re-initialize it

I'm working on an activity where a user would draw a line chart and check to see how close it is to an actual chart. I've got it working, but need help on 'resetting' the canvas. I can get it to clear, but I need to reinitialize it after it's cleared so the user can try again.
here is the reset code I have so far:
var clearCanvas = false;
function onFrame(event) {
if (clearCanvas && project.activeLayer.hasChildren()) {
project.activeLayer.removeChildren();
clearCanvas = false;
}
}
$('.clear').on('click', function() {
clearCanvas = true;
return false
})
Here is a jsfiddle
I've kinda hacked this code together, as I'm not a coder, so go easy if it's not the best way of doing things. If anyone has a cleaner/more efficient way of doing this I'm all ears!
Thanks in advance.
S
First, your jsfiddle is getting a 404 when it tries to use "http://paperjs.org/static/js/paper.js". Switching that to "https://raw.github.com/paperjs/paper.js/master/dist/paper.js" works for me.
Second, if you want the user to try again, they need to be able to place 7 (or however many) new points after the reset, so in your onFrame function you need to set maxPoints to 0 when clearing the canvas.
Third, the path that connects the users points will need to be re-initialized. That can be done by calling path.removeSegments() and then re-adding path to the activeLayer.
Fourth, once you have removed the children from the activeLayer, fullChart (the "answer") is no longer on the activeLayer. You will have to re-add it and re-set its opacity to 0 (if you want it hidden again).
Putting those together, this onFrame seems to do what you are attempting:
function onFrame(event) {
if (clearCanvas && project.activeLayer.hasChildren()) {
project.activeLayer.removeChildren();
fullChart.opacity = 0;
maxPoints = 0;
path.removeSegments();
project.activeLayer.addChildren([fullChart, path]);
clearCanvas = false;
}
}
Here's a jsfiddle.
Other notes:
I don't believe you have to separate your code like that; you could just clear the canvas in your button's callback instead of waiting for the onFrame. So all of the code in onFrame could go in the callback instead (just without the clearCanvas variable).
I would also recommend using the same Point objects for your myCircle and myPath instead of making new ones with the same coordinates. For example:
var point1 = new Point(72, 214);
var point2 = new Point(205, 177);
...
var point7 = new Point(868, 177);
var myCircle1 = new Path.Circle(point1, 10)
var myCircle2 = new Path.Circle(point2, 10)
....
var myCircle7 = new Path.Circle(point7, 10)
var myPath = new Path([point1, point2, ..., point7]);

How to use a variable as part of an URL

I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.