PDA

View Full Version : [AS3] hittest help


teambuddies
12-27-2010, 04:01 PM
I got another problem.

In my game, my army walk then attack the enemy.

I use this hittest:
var hitenemy:Boolean = hitenemycheck(army);
if((hitenemy == true) && (army.attack == false))
{

army.xspeed = 0;
army.attack == true;
army.gotoAndPlay ("attack");
} else if ((hitenemy == false) && (army.attack == true)){
army.attack == false;
army.xspeed = 3;
Army.gotoAndPlay ("walk");


}

function hitenemycheck(objek:MovieClip):Boolean
{
for each (var enemy:MovieClip in enemy_arr){
if(objek.hitTestObject(enemy))
{
if (objek is efeksword1){
enemy.hp -= 1;

}
deletemc();
return true;
}

}
return false;
}

function deletemc():void{
for (var i:int=0;i<enemy_arr.length;i++){
if (enemy_arr[i].hp <=0){
removeChild(enemy_arr[i]);
enemy_arr.splice (i,1);

}

}


When the enemy die. my army start walk again and my problem is, my army won't attack the next enemy, my army just walk pass the enemy.

But when i try to make the enemy just walk pass my army (so the enemy don't die). my army will attack the next enemy (not pass it again).

what is the problem? is there any idea for the hittest?

Thanks

zyxstand
12-27-2010, 04:24 PM
here's the problem:
you're using splice wrong! it's enemy_arr.splice(start, end), not (start, count)
so you should put splice(i, i)!! for the first enemy, what you have would delete the first enemy arr(0), and the next enemy arr(1)!

2 things:
1. in your deletemc function, if you remove an element from an array as you're cycling through it, make sure to write i-- to not skip the next enemy (as the next enemy will immediately get the deleted element's index and you skip over it on next loop). or you can do
for (var i:int=enemy_arr.length - 1; i >= 0; i--) {

2. when checking conditional for boolean, you wrote:
if (mybool == true)
//or
if (mybool == false)
and you only need to (and should) write:
if (mybool)
// or
if (!mybool)
to the computer, what you wrote looks like if (true == true) and that will be compared and the result will look like if (true). so it's kinda an unnecessary step

good luck ;)

tadster
12-27-2010, 05:00 PM
actually for splice it is (start, count) slice is (start, end)

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html#splice%28%29



One problem I see is you have

army.attack == true;


that should be just

army.attack = true;

teambuddies
12-28-2010, 01:54 AM
Sorry guys, it's still not working.

zyxstand, thank you for the bool problem, but when i changed the splice, it give new error (error #2007).

and for tadster the code won't run if i changed == to =.

But thank you for trying.

teambuddies
12-28-2010, 02:24 AM
I got new news.

It's really confusing.

When my army finished attacking one enemy and the enemy die.
Sometimes my army attack next enemy, and sometimes not (just pass it).

What is the problem?

tadster
12-28-2010, 07:42 PM
i don't know, but you definitly need to change both cases of

army.attack == true;
//and
army.attack == false;


to just one equals sign, that is wrong.

I'm talking about these lines here:

if((hitenemy == true) && (army.attack == false))
{

army.xspeed = 0;
army.attack == true; //here
army.gotoAndPlay ("attack");
} else if ((hitenemy == false) && (army.attack == true)){
army.attack == false; //and here
army.xspeed = 3;
Army.gotoAndPlay ("walk");


}