Copy link to clipboard
Copied
I am creating a game wherein there are 4 buttons. The 4 buttons are baskets with labels of solid, liquid, gas and fire. When the button is clicked, the basket that the catcher/character is holding will change depending on what the button is clicked. The changing of the basket is okay now. I placed the changing of basket animation inside the character/catcher movieClip and what I do is that when I clicked the desired basket, I will call the gotoAndPlay function. Now, my problem is regarding my hitTestObject. I want the solid objects to be catching the solid objects only and so on. But whenever I clicked on the button, the basket will change but the catcher/character won't move because of this error. What should I do? >.<
Here is my code in hitTestObject:
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
if (objects.hitTestObject(catcher.mouth_mc_2)) {
if (objects.typestr == "solid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_2.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_2.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if (objects.hitTestObject(catcher.mouth_mc_3)) {
if (objects.typestr == "liquid") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_3.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_3.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if (objects.hitTestObject(catcher.mouth_mc_4)) {
if (objects.typestr == "gas") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_4.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_4.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if (objects.hitTestObject(catcher.mouth_mc_5)) {
if (objects.typestr == "fire") {
score += 5;
soundfx_1.play();
catcher.mouth_mc_5.gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher.mouth_mc_5.gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
else if (objects.hitTestObject(boundary)) {
removeChild(objects);
objects.splice(i, 1);
}
catcher.x = mouseX;
checkStageBorder();
}
}
test if the objects exist before trying to use them in your hittest. for example you can use the following to check for all your hittests except the boundary. how you should handle the boundary hit depends on whether more than one catcher.mouth_mc an exist at any one time:
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
for(var j:int=2;j<=5;j++){
if(catcher["mouth_mc_"+j]){
if (objects.hitTestObject(catcher["mouth_mc_"
...Copy link to clipboard
Copied
test if the objects exist before trying to use them in your hittest. for example you can use the following to check for all your hittests except the boundary. how you should handle the boundary hit depends on whether more than one catcher.mouth_mc an exist at any one time:
public function moveObjects(e:Event) {
for (var i: int = objects.length -1; i >= 0; i--) {
objects.y += speed;
addChild(objects);
for(var j:int=2;j<=5;j++){
if(catcher["mouth_mc_"+j]){
if (objects.hitTestObject(catcher["mouth_mc_"+j])) {
if (objects.typestr == "solid") {
score += 5;
soundfx_1.play();
catcher["mouth_mc_"+j].gotoAndPlay(9);
}
else
{
score -= 5;
soundfx_2.play();
catcher["mouth_mc_"+j].gotoAndPlay(2);
}
if (score < 0) score = 0;
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
}
}
Copy link to clipboard
Copied
I tried the code you gave and the objects exist and they only catches the solid objects. Can you please give me an idea about the correct way of hitTesting each mouth_mc movieclips? Thank you in advance.
Copy link to clipboard
Copied
what do you mean by, "they only catches the solid object"?
Copy link to clipboard
Copied
When I tried the code you gave me, the basket now change depending on the button and the error doesn't occur now. But all the baskets only adds a score when they catch solid objects and I think it's because of this "if (objects.typestr == "solid")". I'm sorry if I made you confuse with that statement.
Copy link to clipboard
Copied
well, why do you have that code in place if you don't want it? what is the typestr property? why don't you remove that code?
and what objects are not "caught"?
Copy link to clipboard
Copied
The type str property represents the arrays for the falling objects. I have 4 arrays. Solid, liquid, gas and Fire. The mouth_mc/default basket must not caught anything, mouth_mc_2 or the second basket should only be caughting the solid objects only, the mouth_mc_3/third basket should only be caughting the liquid object, mouth_mc_4/fourth basket should only be caughting the gas object, the mouth_mc_5/fifth basket should only be caughting fire object. When I try hitTesting this different mouth_mc movieclip, I get this error. >.<
Copy link to clipboard
Copied
kglad,
It finally works! Thank you for your help. I just didn't use the code you gave me properly that is why I am getting error! Thank you very much.
Copy link to clipboard
Copied
Whenever you splice an object in a for loop, I recommend using 'continue;' after it. Change the lines after
if( score < 0 )
to:
ScoreDisplay.text = String(score);
removeChild(objects);
objects.splice(i, 1);
continue;
Look for anywhere else you use 'objects.splice(i,1);' and type 'continue;' after it. I think the reason you are getting a null error is because the code is trying to check for an element in the array that isn't there anymore because you have removed it from the array. Also, I'd recommend typing 'objects = null' before removing it from the array to make sure you don't cause a leak in memory
Copy link to clipboard
Copied
The error still appears even though I have placed the continue function.
Copy link to clipboard
Copied
Spring Missile,
Thank you for you help. It is finally working!