Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-06-2009, 03:10 PM   #1
pockydondon
dina
 
Join Date: Feb 2009
Posts: 8
Default Help in hitTestObject

Hello everyone,
i'm a new programmer in AS3 and i making a simple game
it is to have a gun and some arrows go from it and there are moving circle
if the arrow hit the circle so the circle must disappear

its working but with this error

Parameter hitTestObject must be non-null

the game file is attached

thanks in advance
Attached Files
File Type: zip game.zip (9.7 KB, 237 views)
pockydondon is offline   Reply With Quote
Old 02-06-2009, 08:28 PM   #2
pockydondon
dina
 
Join Date: Feb 2009
Posts: 8
Default

oooh
7 views but no any reply
pockydondon is offline   Reply With Quote
Old 02-06-2009, 08:31 PM   #3
box86rowh
FLEX AND FLASH ARE COOL
 
Join Date: Oct 2005
Posts: 794
Default

why not put the piece of code that is executing the hitTest inside a try catch. That will eliminate the error
box86rowh is offline   Reply With Quote
Old 02-07-2009, 02:24 AM   #4
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

The main problem is that you declare your arrow variable at the top of the code like this:
ActionScript Code:
var myarrow:arrow1;
Now you've got a variable called myarrow, but if you were to trace out its value at this point, it would be null, because you haven't instantiated it yet. You don't instantiate it in your code until the point where your user presses the spacebar.

However, you start an ENTER_FRAME function right away that begins hit testing this arrow object against the other object:
ActionScript Code:
if(myobj.hitTestObject(myarrow))
Since the enter frame function starts running right away, but the arrow object hasn't been instantiated yet, flash complains with an error message that it can't hit test a null object:

Parameter hitTestObject must be non-null

To remedy this in your code, you might do something like this:
ActionScript Code:
function hit(e:Event):void {     if(myarrow != null) {         if(myobj.hitTestObject(myarrow)) {             removeChild(myobj);             removeChild(myarrow);             won.text="You Won";         }     } }
The outer if in the above code tests to see if myarrow is not null before it does any hit testing on it. This gets rid of the error. However, if you run the above, when you succeed in hitting the object with an arrow, you will get another error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

What's going on this time? Well, the first if test passes because myarrow is not null. Even though it's been removed from the display list, it still exists in memory. Also, believe it or not, the next if test also passes. Even though the two objects have been removed from the display list, they are still considered to be hitting each other! Since both if's pass the test, the code proceeds to do a removeChild on both of the objects again. That's when the error kicks in, because you can't remove a child from the display list that's not on it. To remedy this error, you can further modify the outer if statement like this:
ActionScript Code:
function hit(e:Event):void {     if(myarrow != null && myobj.stage) {         if(myobj.hitTestObject(myarrow)) {             removeChild(myobj);             removeChild(myarrow);             won.text="You Won";         }     } }
Now the outer if test will fail if the myobj object has been removed from the stage, because its stage property will be null.

This cures the errors you were getting. But there are other problems with it (like keyboard lag, for one), so I rewrote it, and it's attached. The class files are included in there too, but I mostly didn't change those.
Attached Files
File Type: zip game(maz_rewrite).zip (7.9 KB, 231 views)
__________________
My Tutorials * My Website

Last edited by Mazoonist; 02-07-2009 at 02:44 AM. Reason: changed the attachment to also include the class files
Mazoonist is offline   Reply With Quote
Old 02-07-2009, 07:40 AM   #5
pockydondon
dina
 
Join Date: Feb 2009
Posts: 8
Default

very thanks for ur effort
hitTestObject works now well but if u hit the object the timer still running and time out text even if u already hit the object

again very thanks and if u have any suggestion in timer function i will be very glade

thank u

Last edited by pockydondon; 02-07-2009 at 07:49 AM.
pockydondon is offline   Reply With Quote
Old 02-07-2009, 08:31 AM   #6
pockydondon
dina
 
Join Date: Feb 2009
Posts: 8
Default

thank u timer is now working
i just added
mytimer.stop():
in enterframe function
thank u
but steel one matter

when the user win all objects disappears except arrows
still have arrows without gun
pockydondon is offline   Reply With Quote
Old 02-07-2009, 12:30 PM   #7
Mazoonist
Site Contributor
 
Mazoonist's Avatar
 
Join Date: Jun 2006
Posts: 3,160
Send a message via AIM to Mazoonist
Default

You are right. To tell you the truth, when I remade the file, I added in the timer stuff last, and I didn't test it very thoroughly before posting. But there will always be "still one mattter." At some point, it's going to be up to you to fine tune the program once you get the help you wanted (note: the hitTestObject problem is solved! The mechanic is not going to fix everything on your car just because he's replacing a fan belt).

Making the "arrows" stop firing is easy (hmm... they look more like bullets to me), and there are many ways to accomplish it. One way might be to remove the event listeners for key presses when someone has either won or lost the game. Another way is to create a boolean variable called "gameOver" and check for that to be false whenever you fire bullets.

1. put this at the top of all the code:
ActionScript Code:
var gameOver:Boolean;

2. find the place in the keyPressed function where you fire new bullets, and modify it so that it checks first that game is NOT over:
ActionScript Code:
if(!gameOver) {         //shoot an arrow     var myarrow:arrow1 = new arrow1();     myarrow.y = mygun.y;     myarrow.x = mygun.x + mygun.width / 2;     addChild(myarrow);     arrows.push(myarrow); }

3. Then, just set gameOver to true in those places where the user has either won or lost (in the enter frame function where the user has won and in the timer event handler where the user has lost, add this line):
ActionScript Code:
gameOver = true;

Take this piece of advice as well... NEVER again write a class and name it with an initial lowercase letter. It will be a source of confusion to you later because you can't easily tell a class name from a variable name that way. Also, there is no surer way to give yourself away as a novice, as it gives an immediate impression that you don't know what you're doing (this may be one reason that many people look at your file but nobody helps). Naming classes with capital letters may be just a convention, and it works either way, but there are good solid reasons for the convention. One of the best reasons is that you can always use the lowercase version for a variable name:
ActionScript Code:
var foo:Foo = new Foo();
__________________
My Tutorials * My Website

Last edited by Mazoonist; 02-07-2009 at 12:41 PM.
Mazoonist is offline   Reply With Quote
Old 02-07-2009, 02:17 PM   #8
pockydondon
dina
 
Join Date: Feb 2009
Posts: 8
Default

ya sure i mentioned before i write anything that the problem already solved and i thanks u for this then i ask a new question
the method of removing listener don't work because i tried it before in another code
it might be simple for some one expert in AS3 sure but may it be a point of confusion for beginners like me
thx again for ur time and effort
pockydondon is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[AS3] HitTestObject help gufufu Gaming and Game Development 8 10-15-2008 11:32 PM
HittestObject problem alexz ActionScript 3.0 7 09-12-2008 01:07 PM
hitTestObject not working techfreak11 ActionScript 3.0 2 08-30-2008 02:04 AM
hitTestObject and rotation FerHel ActionScript 3.0 0 07-15-2008 01:55 PM
hitTestObject dilemma RickyNilsson ActionScript 3.0 0 09-21-2007 06:35 PM


All times are GMT. The time now is 04:36 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.