Error while using removeChild() and accessing members of array - arrays

I am stuck doing this even though I know it's very simple. Yet, I am getting errors.
What I have:
I have 3 arrays.
1st Array contains objects of UpgradeButton class.
2nd Array contains objects of BuyButtonclass.
3rd Array named newCostlyShops contains Numbers.
BuyButton class and UpgradeButton class, both have a shopCode member which is a number; the number which I'm trying to equate.
What I'm trying to do:
My goal is to first look for BuyButton and UpgradeButton objects in the respective arrays which have shopCodes same as those in newCostlyShops.
After that, I removeChild() that object and splice it out from the array.
My Code:
Array 3:
var newCostlyShops:Array = new Array();
newCostlyShops = Object(root).WorkScreen_mc.returnCostlyShops();
trace(newCostlyShops); // this is tracing out the exact shopCodes I want and is working fine.
Deletion and Splicing codes:
for (looper = 0; looper < upgradeButtonsArray.length; looper++) {
for (var secondLooper: int = 0; secondLooper < newCostlyShops.length; secondLooper++) {
if (upgradeButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
trace(looper);
trace(upgradeButtonsArray[looper]);
removeChild(upgradeButtonsArray[looper]);
upgradeButtonsArray.splice(looper, 1);
}
}
}
for (looper = 0; looper < buyButtonsArray.length; looper++) {
for (secondLooper = 0; secondLooper < newCostlyShops.length; secondLooper++) {
if (buyButtonsArray[looper].shopCode == newCostlyShops[secondLooper]) {
trace(looper);
trace(buyButtonsArray[looper]);
removeChild(buyButtonsArray[looper]);
buyButtonsArray.splice(looper, 1);
}
}
}
What's wrong with this Code:
I keep getting error
TypeError: Error #1010: A term is undefined and has no properties.
This error comes only after the 1st time this code is run and not the first time it is run. When I remove the removeChild and splice , this traces out objects that are not null, ever. Even after this whole function is called 100 times, the error is not shown. Only when I removeChild and use splice this occurs.
Is there something wrong with what I'm doing? How to avoid this error? This is throwing the whole program haywire. If there is any other alternative to this method, I'm open to take those methods as well as long as I don't get errors and my goal is reached.

It might sounds funny, but.... try to decrement looper after splicing.
trace(looper);
trace(upgradeButtonsArray[looper]);
removeChild(upgradeButtonsArray[looper]);
upgradeButtonsArray.splice(looper, 1);
looper--;
I think after splicing the array all item's are being shifted and you're skipping next one.
Also, you should get some more information with this error, like which class/line is throwing it. Maybe you need to enable "permit debugging" or something?
Bonus suggestion:
For newCostlyShops use Dictionary instead of Array so you won't have to nest for inside for...

Related

as3 array of objects - movement with constant speed

Ok, so I have some experience with as3 and some of the basics. But this problem has been stumping me for so long. I tried to do a workaround based on what I currently know about as3. But somehow either i get an error message or it just doesn't do anything at all. Here is the code that i'm trying to solve.
var zombieCount:Array = new Array();
var helltime:Timer = new Timer(1500);
helltime.addEventListener(TimerEvent.TIMER, spawnzombies)
helltime.start();
function spawnzombies(happened:TimerEvent){
var zombie1:Zombie = new Zombie();
zombieCount.push(zombie1);
stage.addChild(zombieCount[zombieCount.length - 1]);
zombie1.x = 135 + (330*Math.random())
zombie1.y = -29
stage.addEventListener(Event.ENTER_FRAME, move_zombie)
function move_zombie(happened:Event){
for(var i:int; i < zombieCount.length; i++){
zombieCount[i].y = zombieCount[i].y + 1;
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.shift();
trace(zombieCount.length);
}
}
}
}
While this may not be inclusive of everything wrong, here are at least a few of the issues I see.
Inline function issue:
Inside your timer tick handler (spawnZombies), you create an inline function called move_zombie and then add an enter frame handler that calls that function.
The issue here, is that every tick of the timer, will then create a whole new copy of that function, and add ANOTHER enter frame handler. This will create huge problems after a few timer ticks.
Break that move_zombie function OUT OF the spawn function:
eg:
helltime.addEventListener(TimerEvent.TIMER, spawnzombies)
helltime.start();
stage.addEventListener(Event.ENTER_FRAME, move_zombie);
function move_zombie(......
function spawnzombies(.....
Iteration issue:
In your for loop:
for(var i:int; i < zombieCount.length; i++){
zombieCount[i].y = zombieCount[i].y + 1;
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.shift();
trace(zombieCount.length);
}
}
You are not initializing your i value. While this will default it to 0, it's still a good idea for readability to initialize it.
So your iterating forward from 0 to the end of the array. However, if your hit test succeeds, you then use the shift method of the array. This removes the first item of the array (irrespective of what value i is at the time). This will remove the wrong item, plus mess up what zombieCount[i] refers to (because the amount of items has now changed after doing shift, so the next iteration zombieCount[i] will be a reference to same item as the previous iteration).
Instead of what you're currently doing, use the splice method to remove, and iterate backwards so your index doesn't get out of whack.
for(var i:int=zombieCount.length-1;i >=0;i--){
zombieCount[i].y += 1; //move it down 1 pixel
if(zombieCount[i].hitTestObject(border)){
stage.removeChild(zombieCount[i]);
zombieCount.splice(i,1); //remove the item at the current index (do this instead of shift)
trace(zombieCount.length);
}
}

Clearing my array leaves blank array positions

So I'm facing a problem with an AS3 class that's not operating the way I need it to. It's a simple problem, but a complex set of methods that cause it.
Firstly, the 'quiz' I'm building has 6 questions loaded as external SWFs inside of a Shell, run by a class. First we declared a var "a_quiz" to hold 6 values pushed from the external SWFs. These 6 values are reduced to a string and then checked against another array that contains the correct answers. The following loadQuiz function is designed to launch one of three random quizes and clear the a_quiz array so it can take new answers:
public function loadQuiz():void {
a_quiz.length=0;
trace("loadQuiz");
n_quizCorrect = n_quizScore = 0;
n_currentQuiz = Math.floor(Math.random() * (n_totalTopics - n_quizStart + 1) + n_quizStart);
loadTopic(n_currentQuiz);
}
Now I've tested the Shell and confirmed that the trace "loadQuiz" fires every time. And the first time you load the quiz, everything behaves as it should. The 6 questions trace correct or incorrect responses and push 6 binary values into the a_quiz array. The output looks like this:
loadQuiz
incorrect
correct
incorrect
incorrect
incorrect
incorrect
0,1,0,0,0,0
you failed
Then I jump back to the main menu and launch again. This deploys the loadQuiz function all over again. The first line of the function:
a_quiz.length=0;
should be emptying the a_quiz array to accept new answers to mark against. But when I complete the quiz I get this:
loadQuiz
correct
correct
correct
correct
correct
correct
,,,,,,1,1,1,1,1,1
you failed
For some reason beyond my understanding, the push values are stacking on top of empty positions, so when the strings compare...they won't match. What is going on here?
The push function:
function handleClick(evt:MouseEvent):void{
var tempCORRECT = a_answerSheet.toString();
var tempSELECTION = a_selected.toString();
//
if(tempSELECTION == tempCORRECT){
trace('correct');
parentObj1.a_quiz[ parentObj1.n_currentQuestion - 1 ] = 1;
}else{
trace('incorrect');
parentObj1.a_quiz[ parentObj1.n_currentQuestion - 1 ] = 0;
}
parentObj1.n_currentQuestion ++;
// GOTO NEXT SLIDE
parentObj1.LOADNEXT('up');
}
The problem originated in the loading of the Quiz itself.
as you can see on the handleClick function:
parentObj.n_currentQuestion++
was increasing an integer variable on the parentObj's timeline called n_currentQuestion. The problem then, originated in how the quiz was logging n_currentQuestion when the quiz loads with this function:
public function loadQuiz():void {
a_quiz= [];
trace("loadQuiz");
n_quizCorrect = n_quizScore = 0;
n_currentQuiz = Math.floor(Math.random() * (n_totalTopics - n_quizStart + 1) + n_quizStart);
loadTopic(n_currentQuiz);
}
So to correct the error, I needed to add a line to the loadQuiz, so that it would always load with n_currentQuestion set to 1.
public function loadQuiz():void {
a_quiz= [];
n_currentQuestion = 1;
trace("loadQuiz");
n_quizCorrect = n_quizScore = 0;
n_currentQuiz = Math.floor(Math.random() * (n_totalTopics - n_quizStart + 1) + n_quizStart);
loadTopic(n_currentQuiz);
}

Looping and Removing Children stored in Array

I have an Array that holds a few MCs... There is a reset in my app that clears the array and removes the MCs from the stage. It works fine if I test one at a time like this:
if (myArray.length > 0)
{
removeChild(myArray[0]);
}
However if I try to iterate through the Array somehow as to remove all the objects (pr even just one in this case) it doesn't seem to know what the parent object is and therefore can't remove it. I've tried explicitly tell it myArray[0].parent.(removeChild(...) but It throws the same error. Currently I'm trying this:
while (myArray.length > 0)
{
removeChild(myArray[0]); // this line 'must be a child of the caller'
}
Other kinds of loops return the same error. If I trace the objects parent it prints the correct object too... So I'm at a loss. Am I missing something obvious or is there a better way to do this?
Your code will always try to remove the first element in myArray. So first time you could remove the element, and the element isn't the child of his parent anymore.When in the second time, you are trying to remove the first element again and as the element has no parent(it was removed last time), it will give you an error.
So you should remove each element in the array, not same element.
Try this
while (myArray.length > 0)
{
var mc = myArray.shift();
removeChild(mc);
}
If you don't want to remove element in myArray, you can use a count to save if you have remove all elements.Or just use for each like you did.
var count:int = 0;
while (count < myArray.length)
{
var mc = myArray[count];
removeChild(mc);
count++;
}

AS3 adding EventListener to array of movieClips but getting error #2007 :Parameter listener must be non-null

I am trying to add an event listener to ALL of my buttons in the buttons array. I can make them buttons within the loop but when I try and add the event listener is give me this error:
TypeError: Error #2007: Parameter listener must be non-null.
at flash.events::EventDispatcher/addEventListener()
at Main()
I can add this event to another array but just not this one. Ive placed these buttons on the stage and gave them instance names which I'm referring to in my as file. I am learning AS3 in school so this is probably a very obvious problem but Im not qualified to debug my code yet :S Thanks for all your help.
//array of buttons and making them buttons
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
Check the name of the "clickMe" function both in definition and parameters-section, make sure that exactly the same characters are used (a character can sometimes be mistaken for the character from another code table). The error occurs because "clickMe" expression is null at the moment when the loop is being executed.
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];
for(var b:int = 0; b<buttons.length; b++){
buttons[b].buttonMode = true;
// what is the output of the following expression?
trace(clickMe) // should be "function Function() {}"
buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}
function clickMe(e:MouseEvent):void{
trace("hello");
}
In your addEventListener line clickMe is null.
I suspect that we aren't seeing all the code here. Is that code together in the same file ? Or did you cut/paste just parts you thought were important ?

access a movie clip on a certain frame within an array as3

I have movie clip in an array (newStep) that is being added to the stage dynamically. It's randomly choosing a frame to go to every time an instance is added. There is a nested movie clip (stepLine) that i need to change the alpha of. This code actually works for adding a string to to the dynamic text box (pointsDText) but when i try to access the nested movie clip (stepLine) it gives me 1009 null object reference error. The funny thing is the code actually works and does change the alpha of the movie clip but i still get that error, and i think it's making my game more glitchy. I've tried using if(contains(steps[r].stepLine)) too but it doesn't work. Is there a better way to access this movie clip without getting the error?
if(newStep != null){
for(var r:int = 0; r<steps.length;r++){
if(steps[r].currentLabel == "points"){
steps[r].pointsDText.text = String(hPoints);
}
if(steps[r].currentLabel == "special"){
steps[r].stepLine.alpha = sStepAlpha;
}
if(steps[r].currentLabel == "life"){
steps[r].stepLine.alpha = hStepAlpha;
}
}
}
This is so difficult to explain but i hope you understand.
Thanks so much.
A null reference error happens when you try to access properties of a variable that doesn't point to any object -- a null reference. You're effectively trying to access an object that doesn't exist. For example, perhaps stepLine doesn't exist in one of those instances, so stepLine.alpha is causing the error. (How do you set the alpha of a non-existent clip?) Maybe the steps[r] clip is on a frame where there is not yet any stepLine MovieClip.
You should run the movie in debug mode by pressing Ctrl+Shift+Enter in the Flash IDE. This should show you the exact line that causes the error, and it will let you inspect the values of any variables at that point. This should help you track down the problem. Similarly, you can use trace statements to aid in debugging. For example, you could trace(steps[r].stepLine); to check for null values, or even simply if(!steps[r].stepLine) trace("ERROR");. Additionally, if you wrap your accesses in if statements, you can avoid the null reference errors, even though this doesn't really address the underlying problem:
if(newStep != null){
for(var r:int = 0; r<steps.length;r++){
// only touch things if the movieclip actually exists
if(steps[r] && steps[r].stepLine){
if(steps[r].currentLabel == "points"){
steps[r].pointsDText.text = String(hPoints);
}
if(steps[r].currentLabel == "special"){
steps[r].stepLine.alpha = sStepAlpha;
}
if(steps[r].currentLabel == "life"){
steps[r].stepLine.alpha = hStepAlpha;
}
}
}
}

Resources