ActionScript 3.0 :: HitTest - Object Detection / Looping Through Array

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


Similar Posts:


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

ActionScript 3.0 :: Collision Detection Array Object?

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

ActionScript 3.0 :: Hittest Object Within An Object Array?

Mar 15, 2011

I have a function that checks if 2 objects are touching OB1 and OB2, if they do touch it runs the rest of the collision code where OB1 is the character and OB2 is a tile that the player cannot pass through.

I came across an issue where i wanted sprites to be separate from the hitboxes but my collision code works by checking the height and width of an object (you guys know the drill).

So i have a Tile class with 2 parts to it, a bitmap and a movieClip.

So i have this setup(pseudocode):

Code:
loop
{
function collision (ob1 , ob2[loop].movieclip)
}

This should work right? But it ONLY works for the first object in the loop!

View 4 Replies

ActionScript 3.0 :: Multiple Array Object Collision Detection?

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

ActionScript 3.0 :: HitTest On Array Object - Using RemoveEventListener?

Apr 6, 2010

I was using hitTest to see when sunny (mc) would hit one of five objects I have in an array. But I had to use removeEventListener otherwise it would keep occurring. But now I can't "hit" the next object. Why doesn't the addeventListener work again?

stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds ALONG NYPOTENUSEvar v:Number=5;
var varObject:String;
var i:Number;
var arrayObjects:Array;
[Code] .....

View 4 Replies

ActionScript 3.0 :: Apply Hittest Object Between Two Array?

Dec 5, 2009

adding multiple childs to an array these childs are added using timer after a time interval.actually there two classes and child of both classes are added to stage one through timer as i described and other through keyboard event. now is it possible to do this if yes than how?? and is it possible to apply hittest object between these two array.

View 6 Replies

ActionScript 3.0 :: Flash Hittest Only Works On Last Object In Array

Mar 24, 2012

For days now I have been trying to get this hit test to work and which ever way I try it, it always comes back to the same result. I get a hit on the last item I pushed into my array but none on the previous items I pushed in. Also if I put a .sort() on the array it only gives a hit on the last item of the array, no matter which one I push in.

The idea is that I want to drag and drop objects onto the stage, they may not overlap so I need a hit test on the objects so I can make them undroppable when the hit test is true.

This is the part I have for dragging the objects to the stage, and adding them to arrays for the hit test.

Code:
private function onClickDragBuilding1ToGameField(e:MouseEvent):void{
building1Placeholder.x = mouseX-25;
building1Placeholder.y = mouseY-25;

[Code]...

Now if I run this I can add the object to the stage just fine no errors or other issues but the hit test only works on the last object I added to the stage/array and not the previous object I added to the stage/array.

Sso I drag the first object to the stage. (no other object are available yet so nothing happens, this is as it should be) Then I drag a second object to the stage, this reacts to the first object and returns the hit test as true. (Also great and working as it should.) Then I drag a 3th object to the stage, this reacts to the 2nd object I dragged just fine and return a true value, but it does not react to the 1 object I dragged onto the stage. Then when I drag a 4th object to the stage it will react to the 3th object I added, but not the 1st ad 2nd object.

View 4 Replies

ActionScript 3.0 :: Adding Child To An Array And Applying Hittest Object?

Mar 20, 2012

regarding adding multiple childs to an array these childs are added using timer after a time interval.actually there two classes and child of both classes are added to stage one through timer as i described and other through keyboard event. now is it possible to do this if yes than how?? and is it possible to apply hittest object between these two array.

View 6 Replies

ActionScript 3.0 :: Hittest/Collision Detection?

Apr 18, 2012

I'm trying to create an animated 'LED style' display. I want to draw/tween/type shapes or text and have the 'LED dots' detect the animation and turn on the relevant dots.'ve been testing the hittests but it slows down pretty quickly and the problem with the hitTestObject is that it obviously just detects the bounding box, NOT the vectors.

View 3 Replies

Actionscript 3 :: Collision Detection Using Bitmap.hittest

Aug 26, 2010

I have two png files, a circle and a maze. Basically the maze is a square with some paths carved through it. I want to draw these images, move the circle to the mouse coordinates, and have a text say 'hit' when the circle intersects a wall of the maze and 'miss' when it doesn't. Now I want to do these using the bitmapdata.hittest method. The circle is a 32x32 image and the maze is a 256*256 image. I've gotten everything set up and everything draws on the screen correctly but I can't get the actual collision detection part of it to work, that is it always displays 'miss' rather than 'hit' even when the circle clearly intersects the maze.[code]

View 1 Replies

ActionScript 2.0 :: [AS2] Looping Function - Using HitTest(); With Movieclips Within Arrays

Apr 28, 2007

[Code]...

NOTE: The following text is my problem and observations, if you afraid to read it all, my problem is easily guessable within the code go ahead and crack at it without my explanation :-p I'm having some trouble with using hitTest(); with movieclips within arrays. I wanted to build a function that checks the hitTest of Movieclips in arrays because the array is dynamic and it would run more efficient and faster this way.

As you may notice it doesn't check just the movieClip in the array, but a movieclip 2 scopes within it. I feel that this is my problem. I noticed in my debugger that that mc doesn't show up, but i put an onLoad event on it to trace its existence when loaded and it shows up fine. I wonder what I'm doing wrong.

View 2 Replies

ActionScript 2.0 :: Hittest With Multiple Array - Without The Rest Of The Array Numbers Being Reset

Mar 20, 2009

The Setup: For each movieclip the hittests a set of "target" movieclips an array is given a value. If mcIcon1 is dropped onto mcTarget1 the first number in the vacant array is given the value of one. The Issue: If I remove mcIcon1, for example, from the mcTarget1 movieclip I can't find a way of just removing the 1 from that array without the rest of the array numbers being reset.

[Code]...

View 9 Replies

ActionScript 2.0 :: Use ShapeFlag HitTest For An Object Inside An Object?

Jan 7, 2009

ok so if i have an obect called square1 and an object called square2 and inside square2 i have hitArea... i want to use shapeflag to detect an absolute true hitTest on just the area of square1 not the bounding box.... i tried this code and it didnt work...

actionscript 2.0

ActionScript Code:
if (_root.square1.hitTest(square2.hitArea._x, square2.hitArea._y, true)) {

View 2 Replies

ActionScript 3.0 :: Hit Detection On Array?

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

Actionscript 3 :: What's The Difference Between Looping Through An Array And Using Array.every()

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

ActionScript 3.0 :: Using Hit Detection - Object Doesnt Get Very Close The The Second Object But The Game "thinks" It Does

Nov 20, 2010

i'm currently using hitTestObject and it kinda sucks, my object doesnt get very close the the second object but the game "thinks" it does. is there a better hit detection?

View 7 Replies

ActionScript 3.0 :: Object Detection At A Certain Point?

Nov 27, 2009

I need to know how to have an object be recognized when it passes over a point on my stage. Then once that has happened an event happens such as a new screen appares.

View 3 Replies

ActionScript 2.0 :: CS3 To HitTest Within An Array?

Jul 22, 2011

I have this thing where orbs are supposed to stick together, so I put them all in an arrayand told them to stick together when they encounter another member of that array.However, they only stick in groups of up to four, and then the orbs begin to overlap and such.

View 2 Replies

Flash :: Collision And Bounce Detection From Array Of Points

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

ActionScript 3.0 :: Collision Detection For Array Objects In A For Loop?

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

ActionScript 3.0 :: Collision Detection Between Curve And Object?

Feb 7, 2009

I need to detect collision between my bezier curve (drawn using curveTo method) and a object, the curve is dyanamic and hence the equation changes every frame. I am looking for a efficient method to detect collision between curve and the object.

View 1 Replies

ActionScript 2.0 :: Multiple Object Collision Detection?

Feb 19, 2008

I read the tutorial about Multiple Object Collision Detection. What I want to do is have there be 3 circles and 3 squares. the circles are moving randomly while the squares are stationary. The actionscript would need to be checking to see whenever the moving circles collide with the stationary squares. However, it doesn't seem to be working. I followed the logic of the tutorial pretty closely, by making a separate list for the instance names. Here is the code for the collision detection.

[Code]...

View 1 Replies

Actionscript 3 :: Drag An Object With Collision Detection?

Nov 11, 2011

I want to create a function in which I can drag a MovieClip which will bump into other MovieClips on the stage without overlapping them. (i.e. the object cannot be dragged over or through other MovieClips).

function dragHolder01(event:Event):void{
if (mouseDownHolder01 == true) {
for(var m:int = 0;m<blockHolder.numChildren; m++){

[code].....

View 3 Replies

ActionScript 3.0 :: Flash Camera With Object Detection?

Sep 19, 2010

I want to make a cam application. When user's show the objects (sample is in the attach) Flash will play a video. How can I do this?

View 1 Replies

ActionScript 2.0 :: Hittest Array - Allow Gravity

Oct 15, 2010

I'm making a game were parts of the level are randomly generated and move across the screen and while I have made it so all that works correctly I am having trouble running a hittest on them to allow for gravity. Below is the piece of code attached to the player movieclip that I think is causing the issue (shortened for sake of space)

[Code]...

View 3 Replies

ActionScript 2.0 :: Call An Array From A HitTest?

Aug 10, 2003

I'm trying to make two duplicated movie clips check for any of the others ie bullet239 hitting badguy92

View 12 Replies

ActionScript 3.0 :: Hittest Will Not Work In Array?

Feb 18, 2009

I am trying to use the following code to simplify multiple Hittests using an Array, but the code won't work. Can anyone tell me why?

var blockArray:Array = new Array("block1", "block2", "block3");
player.addEventListener(Event.ENTER_FRAME, blockTest);
function blockTest(evt:Event):void {

[Code].....

View 4 Replies

Actionscript 3 :: Collision Detection For Loop Only One Array Item Tested?

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

Flash :: AS3: HitTest Any Object

Apr 6, 2011

I am working on an application where an image serves as cursor. Now i would like to know at any time over which object the cursor is hovering. Sort of like a HitTestObject(*) where i can then see what object the * represents. how i could accomplish this? (and using the mouse is not an option)

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved