Implement the same function in AS2 for an Array - actionscript-2

I have an array, and I would like to make a function onRelease for all of the array positions.
The code would be like:
pick = new Array(2,3,4);
var botoes1:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:1*22});
_root.botoes1.gotoAndStop(pick[1]);
var botoes2:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:2*22});
_root.botoes2.gotoAndStop(pick[2]);
var botoes3:MovieClip = lev.attachMovie("block", "block_"+lev.getNextHighestDepth(), lev.getNextHighestDepth(), {_x:550, _y:3*22});
_root.botoes3.gotoAndStop(pick[3]);
for(i=0;i<3;i++){
_root['botoes'+i].onRelease() = Function () {
}
}
but it doesn't work this way...
and if possible, how can I make the MovieClip declaring for all the buttons in an for loop?

Couple syntax errors there, here's what this line should look like:
_root['botoes' + i].onRelease = function()
{
// Function body.
//
}
Your previous code was trying to assign the result of _root['botoes' + i].onRelease() (which would have been undefined) to the result of Function() (which would have been a Function object).

Related

Getting TypeError when recreating Lodash dropWhile() method in Javascript for an assignment

I get the error: "TypeError: arrayslice is not a function" in "const droppedArray = array.slice(n)" when my dropWhile() method calls the drop() method (below).
I'm confident that the dropWhile() method is correct - so no changes needed here.
I've tried problem solving the TypeError in the drop() method and reworking the code - but keep going in circles with this - can you tell me what is going wrong and how to fix this? I'm sure it's something simple...
drop: function(array,n){
if( n ===undefined){
var n = 1;
}
const droppedArray = array.slice(n);
return droppedArray;
},
dropWhile: function(array,predicate){
const dropNumber = array.findIndex(function(element, index){
return !predicate(element, index, array)
});
const dropArray = this.drop(dropNumber);
return dropArray;
}
I think you forgot to pass the array in your drop function:
const dropArray = drop(dropNumber); // <-- missing the array param
Should be:
const dropArray = drop(array, dropNumber);
What happens now is your drop function is trying to call slice on the index found by this row:
const dropNumber = array.findIndex(function(element, index){
Here is a fiddle to illustrate

CreateJS Tick function not updating from external values.

I think this might be quite basic, I'm still learning CreateJS. I won't include all the code as its a large program but basically.
Outside of my tick function I have this code:
var hitOrMiss = 'Mada';
function hit()
{
hitOrMiss = 'Hit';
//alert(hitOrMiss);
}
function miss()
{
hitOrMiss = 'Miss';
//alert(hitOrMiss);
}
When I click a button and call these they are testing ok (alerting out the values).
Inside my tick() function the values are not being picked up.
if(hitOrMiss = 'Mada')
{
var basic = 'basic';
}
else if(hitOrMiss = 'Hit')
{
if(gamePrincessBmpAnimation.x < 1000)
{
gamePrincessBmpAnimation.x += gamePrincessBmpAnimation.vX;
var basic = 'Not basic';
}
}
else if(hitOrMiss = 'Miss')
{
if(gamePrincessBmpAnimation.x > 60)
{
gamePrincessBmpAnimation.x -+ gamePrincessBmpAnimation.vX;
var basic = 'Miss Not basic';
}
}
Do I need to specify a listener, if so how should it be set up?
I have already triggered the below, Does something similar need to be added to the tick function?
createjs.Ticker.addListener(window);
createjs.Ticker.useRAF = true;
createjs.Ticker.setFPS(60);
gameStage.update();
The other if statements within the tick function are all firing, an example of which:
if (bmpAnimation.x >= screen_width - 16) {
// We've reached right side of our screen
// We need to walk left to go back to our initial position
bmpAnimation.direction = -90;
}
Any help would be appreciated! :)
Fixed this one, wasn't a createJS issue, was a silly Javascript issue, the code here: else if(hitOrMiss = 'Hit') should have been else if(hitOrMiss == 'Hit') etc.

How do i remove all Dojo widgets with id starts with xyz

I have scrollableViews with a id name like idname1, idname2, idname88 etc. I want to destroy all widgets with a id name starting with "idname".
I have tried this:
var widgets = dijit.findWidgets("id^=divNodes");
dojo.forEach(widgets, function(w) {
w.destroyRecursive(false);
It seems that I cant use dijit.findWidgets("id^=divNodes") for this.
What will work for this?
From the docs...
registry.findWidgets returns an array of all non-nested widgets inside
the given DOM node.
https://dojotoolkit.org/reference-guide/1.8/dijit/registry.html
You could iterate over the registry yourself
require(["dojo/_base/array", "dijit/registry"], function(array, registry){
var startsWith = function(wholeString, lookFor) {
return wholeString.slice(0, lookFor.length) == lookFor}
};
var toDestroy = array.filter(registry.toArray(),
function(w) { return startsWith(w.id, 'divNodes'); });
array.forEach(toDestroy, function(w) { w.destroyRecursive(false); });
});

Determing if a SQL select returns an empty set asynchronously?

Is this possible easily? It seems the handleResult method is only executed if the result isn't the empty set.
A thought I had was to have handleResult and handleCompletion be member functions of an object and have handleResult update a member variable that handleCompletion can check. If the variable is set, not empty, if variable unset, empty and can act accordingly.
seems to be overly complicated and hoping there's a better solution?
to sketch out a solution (the thought i had above) (edit2: per comment I made below)
function sql() {
this.results = false;
var me = this;
this.handleResult = function(aResultSet) {
for (var row = aResultSet.getNextRow(); row; row = aResultSet.getNextRow()) {
me.results = true;
var value = row.getResultByName("name");
}
};
this.handleError = function(aError) {
.... //deal with error
};
this.handleCompletion = function(aReason) {
if (me.results) {
....//results
} else {
....//no results
}
if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) {
....//handle these
};
};
s = new sql();
statement.executeAsync({
handleResult: s.handleResult,
handleError: s.handleError,
handleCompletion: s.handleCompletion
});
is this considered a good way to solve this problem?
edit1: this doesn't behave in the manner I'd expect (it works, but not 100% sure why). i.e. the this.results variable is undefined (not false), if handleResult never runs. So it appers as if handleResult and handleCompletion are operating on a different set of variables than I'd expect.
any help to understand what I'm doing wrong would be appreciated.

Queuing system for actionscript

Is there an actionscript library providing a queuing system?
This system would have to allow me to pass the object, the function I want to invoke on it and the arguments, something like:
Queue.push(Object, function_to_invoke, array_of_arguments)
Alternatively, is it possible to (de-)serialize a function call? How would I evaluate the 'function_to_invoke' with the given arguments?
Thanks in advance for your help.
There's no specific queue or stack type data structure available in ActionScript 3.0 but you may be able to find a library (CasaLib perhaps) that provides something along those lines.
The following snippet should work for you but you should be aware that since it references the function name by string, you won't get any helpful compiler errors if the reference is incorrect.
The example makes use of the rest parameter which allows you to specify an array of arbitrary length as the arguments for your method.
function test(... args):void
{
trace(args);
}
var queue:Array = [];
queue.push({target: this, func: "test", args: [1, 2, "hello world"] });
queue.push({target: this, func: "test", args: ["apple", "pear", "hello world"] });
for (var i:int = 0; i < queue.length; i ++)
{
var queued:Object = queue[i];
queued.target[queued.func].apply(null, queued.args);
}
Sure, that works similar to JavaScript
const name:String = 'addChild'
, container:Sprite = new Sprite()
, method:Function = container.hasOwnProperty(name) ? container[name] : null
, child:Sprite = new Sprite();
if (method)
method.apply(this, [child]);
So a query method could look like:
function queryFor(name:String, scope:*, args:Array = null):void
{
const method:Function = scope && name && scope.hasOwnProperty(name) ? scope[name] : null
if (method)
method.apply(this, args);
}