How to fix duplicate function error when i dont see any duplicate - actionscript-3

i get an error telling me theres a duplicate function in verse 38, 60 and 79. but i see no duplicates there!
verse 38 eight was the last line in this
{
if (keyEvent.keyCode == Keyboard.RIGHT) {
rightpressed1 = true;
linkfacing1 = "right";
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftpressed1 = true;
linkfacing1 = "left";
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downpressed1 = true;
linkfacing1 = "front";
} else if (keyEvent.keyCode == Keyboard.UP) {
uppressed1 = true;
linkfacing1 = "back";
}
verse 60 is the last line in
{
spacepressed1 = false;
}
and verse 79 is the last line in
if (linkMc.x > 200) {
linkMc.x -= 10
} else if (overworldMc.x < 0) {
overworldMc.x += 10;
}

(1) Don't use same function names in different parts of your program.
It's not clear from your posted code whether or not you have put code on different layers, scenes or maybe even different frames. If you spread your code then be sure you did not use a duplicate function name elsewhere (later triggered by your shown code).
(2) Make sure there are no variables & functions with a matching name.
//# if a var has name like...
var myThing: int = 5;
//# then using a matching function name will cause the Duplicate Function Error
function myThing () : void
{
//do stuff here...
}
So for example: Do you have a function spacepressed1()or such that could trigger?
(3) If still having issues, show a complete/testable minimum code required to cause your issue. Reduce your code to a few vars and functions that cause such error. After re-creating, then easier to advise...

Related

How to loop through frame number and if true then ignore that frame number?

Hey guys so I am struggling the best way to approach this situation.
So I have 5 frames in my ovenScreen.mcChar movie clip. Each is a character that the player can unlock. If the player has enough coins then they can go to the prize screen to get a random unlockable character.
Here is how it is setup so far:
private function getPrizeHandler(e:MouseEvent):void
{
//Check if prize is locked or unlocked then unlock item/ loop through random number frames
frameLoop = randomNumber(1, 5);
ovenScreen.mcChar.gotoAndStop(frameLoop);
if (frameLoop == 1 && !sharedObjectCharacters.data.sharedHotDog)
{
sharedHotDog = true;
sharedObjectCharacters.data.sharedHotDog = sharedHotDog;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 2 && !sharedObjectCharacters.data.sharedTaco)
{
sharedTaco = true;
sharedObjectCharacters.data.sharedTaco = sharedTaco;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 3 && !sharedObjectCharacters.data.sharedDonut)
{
sharedDonut = true;
sharedObjectCharacters.data.sharedDonut = sharedDonut;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 4 && !sharedObjectCharacters.data.sharedCoffee)
{
sharedCoffee = true;
sharedObjectCharacters.data.sharedCoffee = sharedCoffee;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 5 && !sharedObjectCharacters.data.sharedPancakes)
{
sharedPancakes = true;
sharedObjectCharacters.data.sharedPancakes = sharedPancakes;
sharedObjectCharacters.flush ();
}
////////////////////////////////////////
ovenScreen.gotoAndPlay(2); //play animations
TweenLite.delayedCall(3.5, prizeConfettie);
ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler);
}
As you can see I have the var frameLoop which is a random number from 1 - 5. So the character unlocked will be random and show the random unlocked character. I use the if statements to check if the random number lands on that certain frame and its not the case that it is unlocked then unlock it and save the data.
Now this all works fine but How could I go about fixing it to where if the Item is already unlocked to sort through a different frame number. So if the frameLoop lands on 2 and that character is already unlocked then repeat the random frame number until it lands on a locked character. I was thinking of setting up an array of numbers maybe that approach might be a logical one but not sure how to go about doing so.
Any help would be appreciated thanks.
Additional Info on the shared object Booleans:
private function allSharedObjectBooleans():void
{
sharedObjectCharacters = SharedObject.getLocal("Characters");
sharedHotDog = sharedObjectCharacters.data.sharedHotDog != null ? sharedObjectCharacters.data.sharedHotDog : false;
sharedTaco = sharedObjectCharacters.data.sharedTaco != null ? sharedObjectCharacters.data.sharedTaco : false;
sharedDonut = sharedObjectCharacters.data.sharedDonut != null ? sharedObjectCharacters.data.sharedDonut : false;
sharedCoffee = sharedObjectCharacters.data.sharedCoffee != null ? sharedObjectCharacters.data.sharedCoffee : false;
sharedPancakes = sharedObjectCharacters.data.sharedPancakes != null ? sharedObjectCharacters.data.sharedPancakes : false;
}
and I create them like so:
//shared Booleans
private var sharedHotDog:Boolean;
private var sharedTaco:Boolean;
private var sharedDonut:Boolean;
private var sharedCoffee:Boolean;
private var sharedPancakes:Boolean;
If the character is already unlocked, you increment the variable. If it's overboard (greater than the number of unlockable entities), set it to 1. If it looped through all characters and they all are already unlocked, do something else. And you already have an array of sorts for this, it's just located in sharedObjectCharacters.data and its indexes are string, not int. But this can be remedied by providing a map from int to string, and using this[string] syntax to check for properties. An example:
const strUnlockables:Array=["Noone","sharedHotDog","sharedTaco","sharedDonut","sharedCoffee","sharedPancakes"];
const unlockables:int=5;
private function getPrizeHandler(e:MouseEvent):void {
var f:int=randomNumber(1,5); //frameLoop
for (var i:int=0;i<unlockables;i++) { // check all unlockables, starting with f'th
if (sharedObjectCharacters.data[strUnlockables[f]]===false) {
// this "f" is the one to unlock
sharedObjectCharacters.data[strUnlockables[f]]=true;
sharedObjectCharacters.flush();
ovenScreen.mcChar.gotoAndStop(f);
ovenScreen.gotoAndPlay(2); //play animations
TweenLite.delayedCall(3.5, prizeConfettie);
break;
}
f++;
if (f>unlockables) f=1; // loop around
}
// if we're here, we either did a break, or have all characters unlocked
ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler); // clean up
}

Why does flash output a TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild()

This is my code that I have so far
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
var leftpressed:Boolean = false;
var rightpressed:Boolean = false;
public function Main()
{
//constructor code
Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnpress);
stage.addEventListener(KeyboardEvent.KEY_DOWN, FlyBullet);
}
function moveToKey (event:Event)
{
if (leftpressed && (Spaceship.x>0))
{
Spaceship.x = -5;
}
if (rightpressed && (Spaceship.x<550))
{
Spaceship.x = +5;
}
}
function setKeyPress(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftpressed = true;
}
if (e.keyCode == 39)
{
rightpressed = true;
}
}
function setKeyUnpress(e:KeyboardEvent): void
{
if (e.keyCode == 37)
{
leftpressed = false;
}
if (e.keyCode == 39)
{
rightpressed = false;
}
}
function FlyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
}
}
With the biggest issue with the final FlyBullet function that outputs an the error listed as
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at Main/FlyBullet()
This is your problem:
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
There are no curly brackets so the if condition only applies to the first line where bulletshot is assigned. In other words, if you press any key other than SPACE it will skip the assignment of bulletshot but still try to addChild(bulletshot), but since bulletshot was not assigned a value it is null and you get errors.
I think you meant this:
if (e.keyCode == Keyboard.SPACE) {
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
PS: Your code is poorly indented in a lot of places. It helps to have correctly indented code. An auto-formatter could help you here.
It is generally considered bad practice to omit brackets in multiline if statements, this is an example why. Whenever e.keyCode != 32 the addChild line will try to run.
Solution would be:
///...in the Main function//
stage.addEventListener(KeyboardEvent.KEY_DOWN, flyBullet);
///...
function flyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
{
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y;
}
}
Some unrelated as3 code quality tips, to avoid possible errors later:
I'm guessing Spaceship isn't a static class, so i suggest using lowercase first letters for variable/function names, and uppercase for classes, but this is just good practice and does not affect your codes correctness.
It is also good practice to define the accessibility of your functions, i.e public, private, internal, protected. If you omit it the default is internal.
And only skip brackets after conditionals when necessary.

AS3 keyboard animated character - Compiler errors all over the place despite following tutorial to the letter

Trying to create a 2D character that responds to keyboard inputs in Flash Professional CC. Every YouTube tutorial my son and I have tried produces Compiler errors and NOTHING works.
import flash.events.KeyboardEvent;
import flash.events.Event;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var playerSpeed:int = 7;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseAKey);
function pressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
function releaseAKey(event.KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(rightKeyIsDown == true)
{
player_mc.x += playerSpeed;
}
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
}
What is wrong with the above code? We have followed this tutorial, which seems very straightforward however the minute we type in a curly bracket it highlights in red, and then when we try to test we receive Compiler Errors.
Scene 1, Layer 'Player', Frame 1, Line 35, Column 27 1084: Syntax error: expecting rightparen before dot.
We are using Flash Professional CC and AS3 - is there a better resource for tutorials for this type of thing as we're being driven mad having tried at least 5 of these now without success.
Would really appreciate some advice on why the above is wrong and also where we might find tutorials that actually work!
Thank you.
NJ & Son! :)
I threw your code into Flash and did not see any issues surrounding mismatching parenthesis; however, I did notice the following errors
First, In the following if control structure:
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
You are attempting to use an operator of -+ but no such operator exists in Flash ActionScript. I'm thinking you meant to do the following instead:
if(leftKeyIsDown == true)
{
player_mc.x -= playerSpeed;
}
Second, the issue is right here:
function releaseAKey(event.KeyboardEvent):void
{
// your code
}
You need a : to specify the datatype. Can't believe I missed that. So it should be:
function releaseAKey(event:KeyboardEvent):void
{
// your code
}
I was able to plug this code into Flash CC and get it to compile with no errors. Cheers!

TypeError: Error #1009: Cannot access a property or method of a null object reference - Event.ENTER_FRAME

I am aware there are tons of answers to this problem, but after searching for a while. I could not find one. I know it has to do with my Event.ENTER_SCENE.
Here's my code:
stage.addEventListener(TouchEvent.TOUCH_BEGIN, touchBegin);
stage.addEventListener(TouchEvent.TOUCH_MOVE, touchMove);
stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);
function touchBegin(e:TouchEvent){
if(oCharUse == true){
if(Math.abs(e.stageX - oChar.x) < 100 && Math.abs(e.stageY - oChar.y) < 100){
oMove = true;
} } }
function touchMove(e:TouchEvent){
if(oCharUse == true){
if(oMove == true){
oChar.x = e.stageX;
oChar.y = e.stageY - 100;
} } }
function touchEnd(e:TouchEvent){
if(oCharUse == true){
oMove = false;
} }
stage.addEventListener(Event.ENTER_FRAME, loop)
function loop (e:Event){
if(_collisionTest.complex(x2x, oChar)){
trace("collision")
}
}
I was told to add more info: The error refers to this line: if(_collisionTest.complex(x2x2, oChar)). _collisionTest, is a collisiondetectionkit I got off the internet. And it only appears twice in the code you see above. Not on any other frame.
This error means that the object what you're referencing is null, not defined at that point, provide the line number, because without that, we can't really help. We don't know what _collisionTest is, don't know where was it initiated. Provide more information please.
You need to initialize the variable.
var _collisionTest:CollisionTest = new CollisionTest();

How can I get rid of non-null hitTestObject error with already instantiated objects?

I am a beginner/intermediate AS3 "programmer" trying to complete a skeeball-like flash-based game for a university assessment, and I'm doing myself serious mental damage trying to get even basic object (ball) collisions working with scoring targets on my stage. I'm using a power bar type variable to determine the force of the ball roll which is translated into a tween to create a smooth movement up my "lane" and into the scoring area (this is overhead perspective). The targets are instances of movie clips inside a larger movie clip that is made up of all the components of the game table. Even though my game table and scoring components have already been instantiated when I release the ball, I am getting the typical non-null errors:
ballSpeed is 552
targetArray value is [object upperScoringAreaCTarget_mc]
TypeError: Error #2007: Parameter hitTestObject must be non-null.
at flash.display::DisplayObject/_hitTest()
at flash.display::DisplayObject/hitTestObject()
at SkeeBlast_7_fla::MainTimeline/ballTargetScore()
at SkeeBlast_7_fla::MainTimeline/rollBall()
at SkeeBlast_7_fla::MainTimeline/releaseBall()
Here is my ball release function:
function releaseBall(event:MouseEvent):void
{
rollBall();
gameElements.removeEventListener(MouseEvent.MOUSE_MOVE, moveBall);
}
function rollBall():void
{
ballSpeed = rollPower * 12;
trace("ballSpeed is " + ballSpeed);
ballFriction();
ballGravity();
//ball.y -= ballSpeed;
//var myBallTween:Tween = new Tween(ball,"y",Strong.easeOut,ball.y,ball.y - ballSpeed,3,true);
myBallTween = new Tween(ball,"y",Strong.easeOut,ball.y,ball.y - ballSpeed,3,true);
myBallTween.start();
ballTargetScore();
}
and my collision detection and scoring function:
//match targets to scoring values which should be tied to determineScore()
function ballTargetScore():void
{
var targetValue:String;
var targetArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaCTarget,
gameTable.upperScoringArea.upperScoringAreaLtTarget,
gameTable.upperScoringArea.upperScoringAreaRtTarget,
gameTable.middleScoringArea.middleScoringAreaTargetTop,
gameTable.middleScoringArea.middleScoringAreaTargetMiddle,
gameTable.middleScoringArea.middleScoringAreaTargetLower,
gameTable.lowerScoringArea.lowerScoringAreaTarget);
if (ball.hitTestObject(gameTable.tableDisplay))
{
myBallTween.stop();
}
else
{
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaCTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaC_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaLtTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaLt_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.upperScoringAreaRtTarget))
{
targetValue = gameTable.upperScoringArea.upperScoringAreaRt_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetTop))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaU_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetMiddle))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaM_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.middleScoringAreaTargetLower))
{
targetValue = gameTable.middleScoringArea.middleScoringAreaL_text.text;
}
if (ball.hitTestObject(gameTable.upperScoringArea.lowerScoringAreaTarget))
{
targetValue = gameTable.lowerScoringArea.lowerSA_text.text;
}
else
{
trace("no hit");
}
}
}
}
//gameElements.removeEventListener(Event.ENTER_FRAME, ballTargetScore);
determineScore(targetValue);
//return targetValue;
}
It's all still a bit messy as I tinker trying to find the right combination.
I need to first get a basic ball<->target or boundary collision working, but ultimately, I'd like to try to figure out how to control the collisions conditionally since many of the targets are aligned vertically like a skeeball table. Will I be able to measure the speed of the ball as it is tweening/rolling? Will I be able to apply gravity correctly so that the ball falls and if it catches one of the arcs beneath the scoring target, it will roll along the arc until it 'falls into the hole'? I think I will have issues with the bounding box of my objects on that one.
Thanks for any help,
Alan
Here's the response to Vesper with a cleaned up function (thanks) and new error.
function ballTargetScore():void
{
var targetValue:String;
var targetArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaCTarget,
gameTable.upperScoringArea.upperScoringAreaLtTarget,
gameTable.upperScoringArea.upperScoringAreaRtTarget,
gameTable.middleScoringArea.middleScoringAreaTargetTop,
gameTable.middleScoringArea.middleScoringAreaTargetMiddle,
gameTable.middleScoringArea.middleScoringAreaTargetLower,
gameTable.lowerScoringArea.lowerScoringAreaTarget);
var targetTextArray:Array = new Array(gameTable.upperScoringArea.upperScoringAreaC_text.text,
gameTable.upperScoringArea.upperScoringAreaLt_text.text,
gameTable.upperScoringArea.upperScoringAreaRt_text.text,
gameTable.middleScoringArea.middleScoringAreaU_text.text,
gameTable.middleScoringArea.middleScoringAreaM_text.text,
gameTable.middleScoringArea.middleScoringAreaL_text.text,
gameTable.lowerScoringArea.lowerSA_text.text);
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(targetArray[i]))
{
targetValue = targetTextArray[i];
trace('targetValue becomes',targetValue);
}
}
}
determineScore(targetValue);
}
and the error:
ballSpeed is 432
targetArray value is [object upperScoringAreaCTarget_mc]
targetArray value is [object upperScoringAreaLtTarget_mc]
targetArray value is [object upperScoringAreaRtTarget_mc]
targetArray value is [object middleScoringAreaTargetTop_mc]
targetArray value is [object middleScoringAreaTargetMiddle_mc]
targetArray value is [object middleScoringAreaTargetLower_mc]
targetArray value is [object lowerScoringAreaTarget_mc]
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at SkeeBlast_7_fla::MainTimeline/determineScore()
at SkeeBlast_7_fla::MainTimeline/ballTargetScore()
at SkeeBlast_7_fla::MainTimeline/rollBall()
at SkeeBlast_7_fla::MainTimeline/releaseBall()
Thanks for the assistance.
The rules, which I'm sure are necessary, are making me turn this into a really long question. :)
Anyway, I sort of figured out the error about the DisplayObject, but the program still isn't working correctly.
I fixed the error by adding my gameElements MC to my removeChild call in the determineScore function, which is below; however, the ball still isn't removed, even with the error gone, and the targetValue and the score never update (or show up in the trace from above).
function determineScore(scoreEvent:String):void
{
if ( scoreEvent == "D-O" || scoreEvent == "2XB" || scoreEvent == "?")
{
if (scoreEvent == "D-O")
{
ballCount += 1;
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
else if (scoreEvent == "2XB")
{
ballCount += 2;
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
else
{
determineScore(allRandomScore(allScoresArray));
}
}
else
{
scoreTotal += Number(scoreEvent);
ballScore = Number(scoreEvent);
gameTable.tableDisplay.Score_text.text = scoreTotal;
gameTable.tableDisplay.Score_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
}
Thanks for looking at this. I feel like I'm finally making a little bit of progress.
Ok, continuing along with yet another update.
gameElements is the primary empty MC that I add all the other MC, like the game table and the ball, to so that when I go back to the main menu, I can remove everything at once.
var gameElements:MovieClip = new MovieClip();
var gameTable:MovieClip = new table_mc();
var ball:MovieClip = new blastBall();
and from my drawGame function:
...
stage.addChildAt(gameElements, 1);
gameElements.addChild(gameTable);
initBall();
...
and initBall:
function initBall():void
{
//resize ball
ball.height = 18;
ball.width = 18;
//place ball on table in correct location
ball.x = gameTable.x;
ball.y = gameTable.height - 20;
gameElements.addChild(ball);
//reduce number of remaining balls;
ballCount -= 1;
//hide the mouse and connect its movement to ball
Mouse.hide();
gameElements.addEventListener(MouseEvent.MOUSE_MOVE, moveBall);
}
Hope there's no limit to this entry. :)
Here's the little addition I put in determineScore to pick up the "results" from ballTargetScore (or lack thereof, really):
if (scoreEvent == null)
{
trace("scoreEvent is null");
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
gameElements.removeChild(ball);
if (ballCount > 0)
{
initBall();
}
else
{
drawEnd();
}
}
(haven't cleaned up anything else yet) Still trying to get that first collision to work. When I started catching the null value, initBall and drawEnd sort of started to work (ok, it still doesn't really do what I want but at least there was a response).
Well, you have an array of your targets, and the error you receive means that some of its elements are null. But, when you iterate through the array, you for some reason check against the entire set of targets, not checking if any of them are null. I don't understand why. Seeing that you need a text variable to be assigned if a hit is detected, I advise you making another array with corresponding indexes that will contain all the texts you want to assign.
var targetTextArray:Array=(gameTable.upperScoringArea.upperScoringAreaC_text.text,
gameTable.upperScoringArea.upperScoringAreaLt_text.text,
gameTable.upperScoringArea.upperScoringAreaRt_text.text,
gameTable.middleScoringArea.middleScoringAreaU_text.text,
gameTable.middleScoringArea.middleScoringAreaM_text.text,
gameTable.middleScoringArea.middleScoringAreaL_text.text,
gameTable.lowerScoringArea.lowerSA_text.text);
Then, when you iterate through targetArray, you no longer need to check what was that target in order to retrieve the correct text. You do:
for (var i:uint; i < targetArray.length; i++)
{
if (targetArray[i] != null)
{
trace("targetArray value is " + targetArray[i]);
if (ball.hitTestObject(targetArray[i])) {
targetValue=targetTextArray[i];
trace('targetValue becomes',targetValue);
}
}
}
Update: Okay, let's clean up your determineScore() function. It looks like you are correctly adding and removing your ball, but it's possible you do something of this twice. Removing the ball twice without adding it back will net you this error. Currently I can't get if gameElements.removeChild(ball); gets called twice within that function.
function determineScore(scoreEvent:String):void
{
var reBall:Boolean=false; // should we make another ball roll, given the score
var reDisplay:Boolean=false; // should we set new text
if ((scoreEvent==null)||(scoreEvent=="")) return; // we haven't scored
if ( scoreEvent == "D-O" || scoreEvent == "2XB" || scoreEvent == "?")
{
if (scoreEvent == "D-O")
{
ballCount += 1;
reBall=true;
reDisplay=true;
}
else if (scoreEvent == "2XB")
{
ballCount += 2;
reBall=true;
reDisplay=true;
}
else
{
determineScore(allRandomScore(allScoresArray));
// hey, what are we doing here? We seemingly receive a "?" as an input,
// and we roll a random value out of a set given elsewhere. Okay
return; // as good measure, because we don't have to do anything more
}
}
else
{ // if we're here, we assume we have a numeric score passed inside, right?
scoreTotal += Number(scoreEvent);
ballScore = Number(scoreEvent);
reBall=true;
reDisplay=true;
}
// Now look, we always have to launch a new ball or display end, and
// we always have to redraw score textfield. But look, you are always calling
// determineScore with some value, while you should do it only if you hit something!
// So I added starting clause of "if null or empty string".
if (reDisplay) {
// we have to update text
gameTable.tableDisplay.BR_text.text = ballCount - 1;
gameTable.tableDisplay.BR_text.embedFonts = false;
}
if (reBall) {
// we have to remove ball and launch another, if we have any left
gameElements.removeChild(ball);
if (ballCount > 0 )
{
initBall();
}
else
{
drawEnd();
}
}
}