i get Error #1010 when i use this while loop:
while (pos.length>0)
{
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
(pos_array[index]).x = coo.x;
(pos_array[index]).y = coo.y;
index++;
}
The error says: A term is undefined and has no properties.
What is wrong with my loop because I used the same loop for other programms and i got no such error.
Thank you for your attention.
Your while loop is breaking.
pos.length will never change and eventually pos_array[index] will be out of bounds.
When when you are out of bounds it is undefined.
So basically you are doing.
undefined.x = coo.x;
And just like the error says undefined has has no properties.
I can't see how this loop ever worked.
Try this instead much cleaner
var savedX:Number = 0
for each( var obj:Object in pos_array ){
coo = new MovieClip()
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
obj.x = savedX;
obj.y = 0;
savedX += coo.width;
}
Without knowing what the collection contains, I'm assuming it is filled with DisplayObjects or an object that has x and y properties?
Cast the reference so the compiler understands what the collection contains. For example:
DisplayObject(pos_array[index]).x = coo.x;
DisplayObject(pos_array[index]).y = coo.y;
...or whatever type your collection contains.
Perhaps pos.length and pos_array.length are not equal when the loop starts.
Try this:
while (pos.length>0)
{
coo = pos.splice(Math.floor(Math.random() * pos.length),1)[0];
if (pos_array[index])
{
(pos_array[index]).x = coo.x;
(pos_array[index]).y = coo.y;
}
index++;
}
Related
I'm struggling to concatenate a reference to a variable from a XML document. I'm trying to get:
chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2, chat_History.Msg.chatMessage3
It's instead over-riding the reference and turning into the value '0', '1', '2'. My code:
public function onReceivedChatData(Event:LoaderEvent)
{
var raw_user_info = LoaderMax.getContent("chatHistory");
var chat_History:XML = XML(raw_user_info);
if (chat_History.Msg)
{
trace("ReceivedChatData");
trace(chat_History);
for (var i:int = 0; i < int(chat_History.chatLength); i++)
{
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
shopchatbox.shop_chat_window.text = shopchatbox.shop_chat_window.text + "\n" + chatString;
shopchatwidebox.shop_chat_window.text = shopchatwidebox.shop_chat_window.text + "\n" + chatString;
}
}
else
{
trace("chat_History XML Does Not Exist!!! Noooo :( ");
trace(chat_History);
}
}
The chatLength is 3, and it's calling the for statement 3 times correctly, however chatString isn't referencing it's variable (a string) correctly and only appears as '0', '1', '2'. I'm guessing I'm not concatenating this right and that's the problem, but I'm not sure how to do this?
Thanks!
chatString += i.toString();
That's going to give you the indexes 0, 1, 2. i just stores increments for iteration. Not the values from chat_History.Msg.chatMessage So you're adding i onto the content of chatString, not setting the variable name.
var chatString:String = chat_History.Msg.chatMessage;
chatString += i.toString();
Your code here says, take chatString, and set it to the value chat_History.Msg.chatMessage, then concatenate i as a String onto the content of chatString
If you wanted to access your variables by a variable name, I believe you'd do something like this;
var chatString:String = chat_History.Msg["chatMessage"+ String(i+1)];
So i+1 = 1, 2, 3. Which should mean you're accessing chat_History.Msg.chatMessage1, chat_History.Msg.chatMessage2 and chat_History.Msg.chatMessage3
Remove the line;
chatString += i.toString();
I'm afraid I can't test this as I don't have the XML code, but hopefully that gives you a general idea. I'm not 100% sure I've got calling a variable by a variable name correct, but it is possible to call a variable by a string name.
Edit:
After doing a little testing, the syntax appears to be correct, so that should get you the values from chat_History.Msg
I'm trying to make a turn based strategy game on a grid map. To move your characcters around, I'm using click detection to see which direction the player intends. The code I'm using removes the old object, I presume, the adds the new one in one space to the right. The problem is that when I remove my object the image of the object remains where it began, while the actual objects reappears in the correct position. Then, when I try again, flash throws a 2025 error. Here is the code.
if(s == 0){
sarray[s] = [main, maincharacter, mc, 3]
enemyiden = "gwr"
}
else if(s == 1){
sarray[s] = [mcaxe, mainaxe, mcaxeman, 3]
enemyiden = "gar"
}
else if(s == 2){
sarray[s] = [mchorse, maincav, mccavalry, 5]
enemyiden = "gcr"
}
for(var g:int = 0; g < tilepositions.xcoords.length; g++){
if(tilepositions.xcoords[g] == Allies[s][0] - 12.5){
break
}
}
for(var h:int = 0; h < tilepositions.ycoords.length; h++){
if(tilepositions.ycoords[h] == Allies[s][1] - 12.5){
break
}
}
if(mouseX < Allies[s][0] && Math.abs(mouseX - Allies[s][0]) < 37.5 && Math.abs(mouseY - Allies[s][1]) < 12.5 ){
trace("test1")
this.removeChild(sarray[s][0])
sarray[s][0] = new Sprite
this.addChild(sarray[s][0])
trace(this.contains(sarray[s][0]))
sarray[s][1] = new sarray[s][2]
sarray[s][0].addChild(sarray[s][1])
sarray[s][0].x = tilepositions.xcoords[g-1]
sarray[s][0].y = tilepositions.ycoords[h]
Allies[s][0] = sarray[s][0].x + 12.5
movecurr[s] += 1
break
}
"s" is a for loop variable that counts upward. Any help would be much appreciated.
The problem is within this line: this.removeChild(sarray[s][0]). And the error says it all: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller, which means that sarray[s][0] is not child of this. There's no magical way to tell you why, as we don't know, but if you fix this the problem will be resolved :)
In this example I'm trying to create an Array of length 5 where each ellement contains the number of times .3 can be summed without exceeding 1. i.e. 3 times. So each element should contain the number 3. Here is my code:
Array[(
workingCount = 0;
workingSum = 0;
done = false;
While[! done,
workingSum = workingSum + .3;
If[workingSum > 1, done = true; workingCount, workingCount++]
])
, 5]
In the 3rd to last line there I have workingCount without a ; after it because it seems like in Mathematica omitting the ; causes the value a statement resolves to to be returned.
Instead I get this:
{Null[1], Null[2], Null[3], Null[4], Null[5]}
Why does this happen? How can I get my program to do what I want it to do? i.e. In the context of the function passed to Array to initialize it's elements, how to I use complicated multi-line functions?
Thanks in advance.
Two things:
First, one way to be able to do that in Mathematica is
Array[
Catch[
workingCount = 0;
workingSum = 0;
done = False;
While[! done,
workingSum = workingSum + .3;
If[workingSum > 1,
done = True; Throw#workingCount,
workingCount++]]] &,
5]
Second, and most important: you never should do that in Mathematica! Really.
Please visit for example the Stack Exchange site for Mathematica, and read the questions an answers there to get some grip on the programming style.
Your problem comes from the fact that you are trying to initialize your array, but are trying to do so without an explicit function call - which is what you need to do.
See here for documentation on Arrays in Mathematica:
http://reference.wolfram.com/mathematica/ref/Array.html
That aside, and minor errors (True and False have to be capitalized), this is what you want to do:
f[x_] :=
(
workingCount = 0;
workingSum = 0;
done = False;
While[done != True, workingSum = workingSum + 0.3;
If[workingSum > 1, done = True, workingCount++]
];
Return[workingCount];
);
Array[f, 5] (* The array here is generating 5 values of the return value of f[x_] *)
I'm looking at the as3delaunay library and most of the code is clear to me. This part is not, however (note the line that I put preceded with an arrow):
public function circles():Vector.<Circle>
{
var circles:Vector.<Circle> = new Vector.<Circle>();
for each (var site:Site in _sites)
{
var radius:Number = 0;
var nearestEdge:Edge = site.nearestEdge();
=======>> !nearestEdge.isPartOfConvexHull() && (radius = nearestEdge.sitesDistance() * 0.5);
circles.push(new Circle(site.x, site.y, radius));
}
return circles;
}
For reference, isPartOfConvexHull() is found in Edge.as and looks like this:
internal function isPartOfConvexHull():Boolean
{
return (_leftVertex == null || _rightVertex == null);
}
What does !nearestEdge.isPartOfConvexHull() do? Does that mean that the radius = nearestEdge.sitesDistance() * 0.5 only executes if false is returned from the call to isPartOfConvexHull()? Does that stop execution of any other code?
It is equivalent to:
if (!nearestEdge.isPartOfConvexHull()) {
radius = nearestEdge.sitesDistance() * 0.5;
}
In the following line:
var b:Boolean = expression1 && expression2;
expression2 will not be evaluated if expression1 is false because we already know the final result: b = false.
Now in the following line:
expression1 && expression2;
The same thing happens except the fact that we are not assigning the result to a variable.
And this is exactly what happens in the line you are asking about where !nearestEdge.isPartOfConvexHull() is the first expression and (radius = nearestEdge.sitesDistance() * 0.5) is the second expression.
To extends #sch answer with some explanations (I didn't knew if editing answer to almost double it was ok).
This is based on lazy execution of the interpreter. If (!nearestEdge.isPartOfConvexHull()) is False then there's no need to execute the second part of the AND statement to know it'll be False, then it's left unexecuted. If it's true the evaluation of the complete statement is needed (and then done) to tell wether or not this boolean is True. So this is equivalent to an if statement.
TMHO this is bad code since it's to much condensed and hard to understand.
Hello I've started to learning AS3 from one book and found something I don't understand.
Ellipse(_board[row][column]).fill = getFill(row, column);
_board is two dimensional array of Ellipse type, so I just dont understand why is Ellipse(Ellipse object) used when it apparently works without it, or I haven't seen any changes when I omitted it.
Ellipse(_board[row][column]) is a type cast Type(object)
Because you can push anything into an Array the compiler doesn't know what kind of objects are stored within the Array. So it is good style to cast objects if you retrive them from an Array to their correct type.
This has several benefits:
if such an object is not of the type you expect or null, you will know when you typecast instead of getting an error later somewhere far away
the code executes a bit faster if you are explicit about the types
if you use a good IDE it can provide you with autocompletion when it knows the types
_board is a multidimensional array filled first with Arrays.
In the BoardDisplay.mxml
(Hello! Flex 4 : Chapter 3. Hello Spark: primitives, comp... > FXG and MXML graphics—building a game.. - Pg. 80) ,
<Graphic version="1.0" viewHeight="601" viewWidth="701"
xmlns=" library://ns. adobe. com/flex/spark"
xmlns:fx=" http://ns. adobe. com/mxml/2009"
xmlns:mx=" library://ns. adobe. com/flex/halo"
initialize="createBoard()"
click=" clickHandler(event)">
initialize calls createBoard().
private function createBoard():void {
newGame();
_board = new Array(6);
for (var row:int = 0; row < 6; row++) {
_board[row] = new Array(7);
for (var col:int = 0; col < 7; col++) {
_board[row][col] = addEllipse(row, col); //Magic Happens!
}
}
}
addEllipse returns Ellipse to each entry in _board
private function addEllipse(row:int, col:int):Ellipse {
var ellipse:Ellipse = new Ellipse();
ellipse.x = 4 + col*100;
ellipse.y = 5 + row*100;
ellipse.width = 90;
ellipse.height = 90;
ellipse.fill = getFill(row,col); // Magic Found !
ellipse.stroke = new SolidColorStroke(0x000000, 1, 1.0, false,"normal", null, "miter", 4);
boardGroup.addElement(ellipse);
return ellipse;
}
The author casted it as maxmx said but did not really need to as all the entries were of type Ellipse so
Ellipse(_board[row][column]).fill = getFill(row, column);
can work as
_board[row][column].fill = getFill(row, column);