ReferenceError: Error #1069: Property 0 not found on String and there is no default value - actionscript

He,everyone. I'm new in ActionScript 3 and I don't understand where is my mistake.
for (var i:int = 0; i < 26; i++) {
button = new Sprite();
button.name = String.fromCharCode(65 + i);
button.graphics.lineStyle(2, 0x000000);
button.graphics.beginFill(0x00FF00);
button.graphics.drawRect( Keyboard_Container.x + i * ButtonSize,Keyboard_Container.y,ButtonSize,ButtonSize );
button.graphics.endFill();
button.addEventListener(MouseEvent.CLICK,InitLetter);
Keyboard_Container.addChild(button);
}
And the InitLetter function from event listener is
private function InitLetter(event:MouseEvent):void {
this.letter = event.target.name;
}
The error that I received is "ReferenceError: Error #1069: Property 0 not found on String and there is no default value."
Why I cannot gain access name of button trough event.target. Why? Thank you very much.

Your problem is in your main Hangman class, in the CheckLetter() function :
for (var i:int = 0; i < word.length; i++)
{
if (word[i] == currentLetter) // word is a String and not an Array
{
// ...
}
}
Here you should know that to get a char from a String, we use string.charAt(i), if you want to use an Array object, you can do :
word:Array = "word".split(""); // gives : ["w", "o", "r", "d"]
Also, I don't know why you are using this : setInterval(PlayGame,100);, normally you can check the word just when the user put a new letter and not every 100 ms ! and even you have to use it ( in another case ), it's better to use a Timer or just Event.ENTER_FRAME ...
Hope that can help.

Related

Why is my binary search implementation returning -1?

This is my main.dart:
import 'edgecases.dart';
main () {
var card = edgecases(0)['input']['cards'];
var query = edgecases(0)['input']['query'];
var result = locate_card(edgecases(0)['input']['cards'], edgecases(0)['input']['query']);
var output = edgecases(0)['output'];
print("Cards:- $card");
print("Query:- $query");
print("Output:- $result");
print("Actual answer:- $output");
}
And this is my edgecases.dart:
edgecases ([edgecasenumber = null]) { //You may make it required, I provided a null as default to check if my syntax is going right.
List tests = [];
var edge1 = {'input': {
'cards': [13, 11, 10, 7, 4, 3, 1, 0],
'query': 1
}, 'output': 6};
tests.addAll([edge1]);
if (edgecasenumber == null){ // This if is useless here so you may
return 'Null type object coud not be found.';
} else {
return tests.elementAt(edgecasenumber); // Indexing in dart also starts with 0.
}
}
locate_card (List cards, int query){
int lo = 0;
int hi = cards.length - 1;
print('$lo $hi');
while (lo <= hi) {
//print('hello'); Uncomment to see if it is entering the loop
var mid = (lo + hi) ~/ 2;
var mid_number = cards[mid];
print("lo:$lo ,hi:$hi, mid:$mid, mid_number:$mid_number");
if (mid_number == query){
return mid;
} else if (mid_number < query) {
hi = mid - 1;
} else if (mid_number > query) {
lo = mid + 1;
};
return -1; //taking about this line
};
}
[I have cut short the code here so you may find some things as unnecessary so just ignore it XD]
Actually I am trying to implement binary search here(I have previously successfully implemented it in python, I am implementing in dart to learn the language.)
On testing it with first edge case(that is on running the command dart main.dart), I found that it is returning the value -1 which was wrong, so I tried commenting the return -1; line in edgecases.dart file to see what happens as it was made to handle another edge case(edgecase if the list is empty, here I have removed that for simplicity). I am not able to understand why it is returning -1 if it gives the right value on commenting that line. Any possible explainations and solutions?
Thanks in advance!
You almost did it right. Just place the return -1; after the while loop's closing brace at the very end of locate_card.

GetElementById and A tags

Getting error on this:
for (i = 1; i <= 18; i++) {
oAllKits = myNode.getElementById('node' + i).getElementsByTagName('a');
}
I have a series of IDs on the html document called: node1, node2,... node18. I am trying to target the A tags on these IDs since these A tags are the only elements inside these ids. The console is giving me this message: # has no method 'getElementById'.
I am doing a for loop because I want the variable oAllKits to hold an all those A tags inside the Ids. Thank you advance for your help.
That can be done easily. Find it here
or you can see the code
var avar = document.getElementById('div');
var bvar = div.getElementsByTagName('a');
var cvar = children.length;
for (var i=0;i < len;i++) {
document.getElementById('aclass').innerHTML +='<br> ' + children[i].href;
}
getElementById exists on the document. There should be only 1 of any particular ID so the selector is fast.
var div = document.getElementById('id1');
var children = div.getElementsByTagName('a');
var len = children.length;
for (var i=0;i < len;i++) {
document.getElementById('found').innerHTML += '<br> ' + children[i].href;
}
http://jsfiddle.net/UhT2W/1/

JQuery : How can we filter int from variable

Is it possible to filter numbers from the variable.
I can show you one example here from the link http://jsfiddle.net/sweetmaanu/82r5v/6/
I need to get only numbers from the alert message
Simply replace the box string out of it.
DEMO
for (var i = 0; i < order.length; i++) {
order[i] = order[i].replace('box', '');
}
So instead of box1, box2, box3, box4 you want to see 1,2,3,4
You can use a regular expression like this:
var order = $("#boxes").sortable("toArray") + "";
alert(order.replace(/[^0-9,]/g, ''));
I also had to append an empty string to order because it wasn't being recognized as a string object even though the jQuery documentation says it should be when you call sortable("toArray").
change var order = $("#boxes").sortable("toArray");
to var order = $("#boxes").sortable("toArray").join(',').replace(/[a-zA-Z]/gi, "");
Demo: http://jsfiddle.net/82r5v/13/
// Remove all non-digits from the string
'box1'.replace(/\D/g, ''); // => '1'
// Same, but try to make the string a number
Number('box1'.replace(/\D/g, '')); // => 1
// Shorthand for making an object a number (+o is the same as Number(o))
+'box1'.replace(/\D/g, ''); // => 1
// parseInt(s) works if the number is at the beginning
parseInt('1box'); // => 1
// but not if it occurs later
parseInt('box1'); // => NaN
Maybe using regular expressions something like this:
`alert(order.join(',').match(/\d/g));`
To return the array as numbers.
(\d matches all digits, g signifies a global match wildcard)
One way to do it by using regular expressions - http://jsfiddle.net/holodoc/82r5v/14/
$(document).ready(function() {
var arrValuesForOrder = ["2", "1", "3", "4"];
var ul = $("#boxes"),
items = $("#boxes li.con");
for (var i = arrValuesForOrder[arrValuesForOrder.length - 1]; i >= 0; i--) {
// arrValuesForOrder[i] element to move
// i = index to move element at
ul.prepend(items.get(arrValuesForOrder[i] - 1));
}
$("#boxes").sortable({
handle : '.drag',
update: function() {
var order = $("#boxes").sortable("toArray");
var sorted = [];
$.each(order, function(index, value){
sorted.push(value.match(/box(\d+)/)[1]);
})
alert(sorted);
}
});
});

Can I iterate through each of my SharedObjects in ActionScript 3?

Is it possible to execute a "foreach" through each of my SharedObjects?
Something like this . . .
_so = SharedObject.getLocal("test","/");
foreach (var item:Object in _so)
{
// print key name and value
}
I'm not sure I understand.
1.Do you want to loop through all your Shared Objects ?
2.Do you want to loop through all your objects inside the data property of a SharedObject ?
1.Assuming sharedObjects is an array containing SharedObject instances
var sharedObjects:Array = ['test','fred','louie'];
var sharedObjectsData:Array = [];
for(var i:int = 0 ; i < sharedObjects.length ; i++){
sharedObjectsData.push(SharedObject.getLocal(sharedObjects[i],"/").data);
}
//or something like that
2.Try using an old school for in loop.
for(var i in _so.data){
trace('property: ' + i + ' value: ' + _so.data[i]);
}
Hope it helps!

Actionscript 2 functions

I'm an experienced programmer but just starting out with Flash/Actionscript. I'm working on a project that for certain reasons requires me to use Actionscript 2 rather than 3.
When I run the following (I just put it in frame one of a new flash project), the output is a 3 rather than a 1 ? I need it to be a 1.
Why does the scope of the 'ii' variable continue between loops?
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
fs[i] = function(){
trace(ii);
}
}
fs[0]();
Unfortunately, AS2 is not that kind of language; it doesn't have that kind of closure. Functions aren't exactly first-class citizens in AS2, and one of the results of that is that a function doesn't retain its own scope, it has to be associated with some scope when it's called (usually the same scope where the function itself is defined, unless you use a function's call or apply methods).
Then when the function is executed, the scope of variables inside it is just the scope of wherever it happened to be called - in your case, the scope outside your loop. This is also why you can do things like this:
function foo() {
trace( this.value );
}
objA = { value:"A" };
objB = { value:"B" };
foo.apply( objA ); // A
foo.apply( objB ); // B
objA.foo = foo;
objB.foo = foo;
objA.foo(); // A
objB.foo(); // B
If you're used to true OO languages that looks very strange, and the reason is that AS2 is ultimately a prototyped language. Everything that looks object-oriented is just a coincidence. ;D
Unfortunately Actionscript 2.0 does not have a strong scope... especially on the time line.
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
fs[i] = function(){
trace(ii);
}
}
fs[0]();
trace("out of scope: " + ii + "... but still works");
I came up with a kind of strage solution to my own problem:
var fs:Array = new Array();
for (var i = 0; i < 3; i++){
var ii = i + 1;
f = function(j){
return function(){
trace(j);
};
};
fs[i] = f(ii);
}
fs[0](); //1
fs[1](); //2
fs[2](); //3

Resources