ActionScript 3.0 :: Problem With Hit Detection/looping Through Array
Oct 14, 2011
Hey guys, thanks in advance for any help, I'd be stuck all the time without these forums.
I have a custom class(Ball) and a .FLA. Ignore the custom class, it works fine, having a problem within my main FLA.
I'm getting repeated "Error #1009: Cannot access a property or method of a null object reference. at Environment_fla::MainTimeline/hitTest()".
I understand that this error means I'm asking it to reference something that's no longer there. I just can't figure out why I'm getting it, and how better to hitTest many objects in an array.
My game involves creating a bunch of balls that will fall into buckets. I need to keep track of how many touched the buckets, how many fell off the screen, and remove each accordingly.
Thanks again, very much appreciated.
Here is the code from my main FLA
[as]var ballTimer:Timer = new Timer(200);
var changeTimer:Timer = new Timer(50000);
ballTimer.addEventListener(TimerEvent.TIMER, throwBall, false, 0, true);
ballTimer.start();
changeTimer.addEventListener(TimerEvent.TIMER, changeColor);
changeTimer.start();
//Environment variables
var gravity:int = 1.5;
var friction:Number = .85;
var color:int = 2;
//score variables
var YellowScore:int = 0;
var BlackScore:int = 0;
var tBall:Ball = new Ball(new Point(mouseX, mouseY), new Point(Math.random() + Math.random()*5 + Math.random()*8), gravity, friction);
var ballArray:Array = new Array();
function throwBall(e:TimerEvent):void {
var tBall:Ball = new Ball(new Point(mouseX, mouseY), new Point(Math.random() + Math.random()*5 + Math.random()*8), gravity, friction);
tBall.addEventListener(Event.ENTER_FRAME, hitTest);
tBall.gotoAndStop(color);
addChild(tBall);
ballArray.push(tBall);
}
function changeColor(e:TimerEvent):void {
if (color == 1) {
color = 2;
return;
}
if (color == 2) {
color = 1;
}
}
function hitTest(e:Event) {
trace(ballArray.length);
for (var i = 0; i < ballArray.length - 1 ; i++) {
if ((ballArray[i].hitTestObject(YellowBucket1) && ballArray[i].currentFrame == 2)) {
YellowScore += 1; //trace(YellowScore); trace("Space");
ballArray[i].parent.removeChild(ballArray[i]);
ballArray.shift();
break;
}
else if ((ballArray[i].hitTestObject(YellowBucket1) && ballArray[i].currentFrame == 1)) {
YellowScore -= 1;
}
else if ((ballArray[i].hitTestObject(BlackBucket1) && ballArray[i].currentFrame == 2)) {
BlackScore -= 1;
}
else if ((ballArray[i].hitTestObject(BlackBucket1) && ballArray[i].currentFrame == 1)) {
BlackScore += 1; trace("BlackScore");
}
else if (ballArray[i].y > stage.stageHeight || ballArray[i].x > stage.stageWidth) {
trace("cleanUp");
ballArray[i].parent.removeChild(ballArray[i]);
ballArray.shift();
}
}
updateScore();
}
function updateScore() {
BlackBucket_txt.text = String(BlackScore);
YellowBucket_txt.text = String(YellowScore);
}
[/as]
View 3 Replies
Similar Posts:
Oct 14, 2011
I've attached a zip with a few custom classes and a .FLA. Ignore the custom classes. I'm getting repeated
"Error #1009: Cannot access a property or method of a null object reference. at Environment_fla::MainTimeline/hitTest()".
I understand that this error means I'm asking it to reference something that's no longer there. How better to hitTest many objects in an array. My game involves creating a bunch of balls that will fall into buckets. I need to keep track of how many touched the buckets, how many fell off the screen, and remove each accordingly.
Here is the code from my main FLA
ActionScript Code:
var ballTimer:Timer = new Timer(200);
var changeTimer:Timer = new Timer(50000);
ballTimer.addEventListener(TimerEvent.TIMER, throwBall, false, 0, true);
ballTimer.start();
changeTimer.addEventListener(TimerEvent.TIMER, changeColor);
changeTimer.start();
[Code] .....
View 6 Replies
Mar 31, 2011
i have a snake game, that after time loads sprites in an array to make the snake grow. Just wondering how i would go about adding a hit so when the head of the snake hits the body something happens. Below is the full document class code.
<code>
package {
import flash.display.Sprite;[CODE].....
View 5 Replies
Jan 31, 2011
What's the difference between looping through an array or using array.every() to assign a callback to each array element?
View 3 Replies
Mar 28, 2012
I'm trying to have an enemy's shell hit my player. I have the logic in place that detects a player's bomb hit an enemy, I tried to edit it slightly and apply the same logic to the enemies shell.
Code:
// check for collisionspublic function checkHits(event:Event)
{
[code]......
View 2 Replies
Sep 18, 2011
I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside. Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically. I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not. My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.
View 2 Replies
Jun 23, 2009
I am creating a game where I have two separate arrays of objects that I need hitTested with each other. I found an old tutorial in AS2 that I modified to come up with this code:
ActionScript Code:
for (var w:Number = 0;w < enemyLaserArray.length;w++) {
for (var R:Number = 0;R < barricade1Array.length-1;R++) {
if (enemyLaserArray[w].hitTestObject(barricade1Array[R])) {
[code]....
This code executes like I believe it should, but along with removing the two objects, and splicing them from their arrays, I get this error
Code:
TypeError: Error #1010: A term is undefined and has no properties.on the line hitTestObject line. I have had similar problems in other areas of my project, but this time no matter what I try using trace statements I cannot fathom quite what the problem is.
View 2 Replies
Nov 13, 2009
a little while ago I saw something on this website:[URL]...I decided I wanted to recreate something like it.
They're using javascript, I'm using ActionScript, anyway.I'm trying to create collision detection for an array of movieclips inside a for loop. Right now I have it that upon holding down the mouse, the same movie clip will fall onto a ground object (and bounce up occasionally,) I'm trying to make it to so each ball could stand on top of each other. I've used collision detection before for separate objects, but never for instances of the same object inside a for loop.
View 0 Replies
Apr 2, 2011
[I apologise if this isn't really an in depth question, but I wanted to solve this once and for all]I was trying to get look into quadtrees, but already ran into trouble getting collision detection without any optimization working properly. Did a search and found a pretty neat example:Only some collisions are detected between particular objects. When the objects are not moving it seems to work alot better for some reason. really can't figure out whats the problem, the code is essentially the same as the sample. I did not blindy copy pasted it, I understands whats happening except Here is what I did:Main class
package
{
import com.martino.objects.Square;
[code]....
View 2 Replies
Mar 8, 2012
I am trying to test the collisions between a bullet and an array of enemies in Actionscript 2. However it is not sensing a collision.This is the code in the bullet.
onClipEvent(load)
{
facing = _root.player.facing;
speed = 1;
[code]....
View 1 Replies
Jan 27, 2010
Why doesn't AS2 interpret these variable as it steps through? Everything works if I write it out longhand and do not use a loop, but if I try to condense things by using an array, variables such as _root.myTargets[i]._x are not interpreted properly.
Code:
myTargets = new Array('shoot','shoot2');
for (var i:Number=0; i<myTargets.length; i++) {
if ((_root._xmouse > (_root.myTargets[i]._x - 30)) and
(_root._xmouse < (_root.myTargets[i]._x + 50)) and
[Code]....
View 1 Replies
Feb 1, 2008
I have an XML document that loading into Flash. I have a dynamic text box on the stage that I am writing the XML to. My problem is, I have 6 group names that I am pulling from my XML file and I want to write each one of them to the text box and add a line break to the end. This is my code:
var group= this.firstChild.firstChild.childNodes;
for (var i=2; i<group.length; i++){
divBox['divInfoLinks'].htmlText =
[Code].....
When I trace [i] it lists all 6 names, however my text box only shows the last name in the array.
View 3 Replies
Apr 23, 2011
how to work out the direction of the mouse on stage and say from this:
"if the mouse goes from the left to right (visa-versa) once, add score +1" and "if the mouse speed which is player controlled - slows down - to then display an error graphic"
View 21 Replies
Mar 11, 2012
I am attempting to build an array of arrays that will with the following syntax. My output is definitely not what I am trying to achieve. Here is my code:
[Code]...
My output is coming out as 6 references to "Set3". There is obviously something wrong with my for loops, but I can't figure it out.
View 1 Replies
Oct 8, 2011
So my question is:"how do you stop looping?"
I tried implementing a Counter and every time flash goes to another variable in array, i would increment that Counter and i would see if Counter == Array.length.[code]...
View 2 Replies
Dec 29, 2011
I am trying to download 3 files from a website by defining an array and looping through the urlloader commands I am using urlloader so that the user doesn't have to confirm each download, my code is:
[Code]...
View 1 Replies
Jun 28, 2006
I have two arrays. One is used for button names and the other is used for the links for the buttons. The buttons are generated dynamically with attachMovie and the names are also modified via AS. My problem comes in at looping through the links array and applying them to the buttons.[code]
View 3 Replies
Jan 10, 2008
How can i stop this array from looping. I just want the images to fade in once then stop after theyre done.
import mx.transitions.Tween;
import mx.transitions.easing.*;
var mc_arr = [num_mc1,num_mc2,num_mc3];
var counter = 0;
[code]....
View 5 Replies
Apr 14, 2009
I'm looping through an array and checking the elements to see if an element contains this text. the array is as following
[Code]...
i've narrowed down the problem to being the variable 'current'. If I replace the dynamic part to the string '_A_1' it works. That is why I entered the trace just above to see what its is checking against. I can see that they are both the same, but flash thinks otherwise.
View 2 Replies
Dec 24, 2010
I've got no idea how to phrase this, but is there a simple/efficient way to do something like this that doesn't feel hacky:?
[Code]...
I realize I can just trade the index number in the brackets for a function that returns the corrected index (after it works through the end of beginning of the loop as needed), but what's the best or most efficient way to do this? The damn 0 as the first position in an array really confuses me sometimes when trying to work these things out.
View 11 Replies
Jun 4, 2010
I am trying to loop my function through my array values continuously. I'm only able to get it to loop through one array value as intended. The long term goal here is the create a grid of movie clips that continuously change colors in random order (from preselected values). I'm new to actionscript and I have been racking my brain trying to figure this out. I'm unsure of how to send the function to all items in the array, and loop them all continuously, yet with random color patterns for each.
[Code]...
View 3 Replies
Jul 28, 2010
I would like to repeat a block of code within a function in Flex, which is the best way to do this, I would like to do this without making a new function?
The code currently is:
Code:
for each (var rawPhoto:Object in event.data)
{
logger.info("Photos.getPhotos: Photo[{0}] downloaded", rawPhoto.pid);
[Code].....
View 1 Replies
Jun 16, 2011
Here is my xml structure:
<question>
<q1> Who coined the term "Clinical Psychology"? </q1>
<answer> Lightner Witmer </answer>
<option1> Stanley Hall </option1>
<option2> Lightner Witmer </option2>
<option3> Henry P. David </option3>
</question>
I can loop through fine and pick out the questions and answers, then throw them into separate arrays. The problem I'm having is looping and pulling the options into a multidimensional array like such:
var one:Array = new Array( 3 );
one[0] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[1] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[2] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
View 2 Replies
Jul 25, 2011
this is part of an XML file I retrieve using AS3 E4X:
<links>
<link>
<label>Versions</label>
<href>http://mylink1</href>
[Code].....
I want to retrieve the values of labels, so I write:
document.links.link.label.text();
This returns VersionsConfigurations. I need this as Array ([Versions, Configurations]) but I would like not to use a loop. Is there any other way?
View 2 Replies
Aug 27, 2009
I'm trying to make a next button loop through frame labels on the timeline. Here is my current code and it doesn't seem to be returning anything..
ActionScript Code:
var a:Array = ["a", "b", "c", "d"];
var len:Number = a.length;
[code].....
View 4 Replies
Jun 9, 2005
I am preloading multiple mp3's dynamically using a looping array and it is working very well. However, I would like to display the percetage of the last mp3 loaded by the array.Does anyone know how I would modify the code to do this:
Code:
var my_array:Array = new Array();
my_array[0] = "Audio/audio_1.mp3";
[code].....
View 2 Replies
Jan 22, 2007
I'm working off the XML photo gallery tute found here: xml_flash_photogallery.htm. My wish is to have the array loop forward and backwards, so by clicking next you will eventually get back to the first value, and if you keep clicking previous you will eventually see the last value in the array.
I found some Scotty code (first line in the nextImage function) that enabled this function with the next button, but I can't figure out how to use it for the previous button.Here's the code:
previous_btn.onRelease = function() {
prevImage();
};[code]....
View 5 Replies
Sep 3, 2003
I want to do a for loop that grabs values from different variables. This function works nicely without the eval() statement (using the commented line below it) but not with it.
function saveData ()
{
saveUrl = "savedata.php?";
for (count=1; count<=numberOfVariables; count++)
[Code].....
View 14 Replies
Dec 23, 2009
On the PHP site it was advised to do for loops in a certain way that I have not seen on any recommendations for AS3. I think I found about a 10 times speed increase and I thought I better check that I was not missing something (?) before going through my code and changing it. Very simple and fairly self explanatory (in the faster one the array.length is not calculated every time I think):
Code:
import flash.utils.getTimer;
var time:Number;
var myarray:Array = new Array();
myarray.length=2000000;
time=getTimer();
[Code] .....
View 14 Replies
Jul 29, 2010
Is there any as3 based source code that accomplishes pitch detection or beat detection accurately? Doesn't have to be both, if you know of at least one of those,
View 4 Replies