Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-27-2010, 04:01 PM   #1
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default [AS3] hittest help

I got another problem.

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

I use this hittest:
Code:
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
teambuddies is offline   Reply With Quote
Old 12-27-2010, 04:24 PM   #2
zyxstand
Platinum Member
 
Join Date: Dec 2010
Posts: 193
Default

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
Code:
 for (var i:int=enemy_arr.length - 1; i >= 0; i--) {
2. when checking conditional for boolean, you wrote:
Code:
if (mybool == true)
//or
if (mybool == false)
and you only need to (and should) write:
Code:
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
zyxstand is offline   Reply With Quote
Old 12-27-2010, 05:00 PM   #3
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

actually for splice it is (start, count) slice is (start, end)

http://livedocs.adobe.com/flash/9.0/...l#splice%28%29



One problem I see is you have
ActionScript Code:
army.attack == true;

that should be just
ActionScript Code:
army.attack = true;
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster is offline   Reply With Quote
Old 12-28-2010, 01:54 AM   #4
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default

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 is offline   Reply With Quote
Old 12-28-2010, 02:24 AM   #5
teambuddies
Registered User
 
Join Date: Dec 2010
Posts: 8
Default

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?
teambuddies is offline   Reply With Quote
Old 12-28-2010, 07:42 PM   #6
tadster
tadster
 
tadster's Avatar
 
Join Date: Feb 2009
Location: Texas
Posts: 2,111
Default

i don't know, but you definitly need to change both cases of
ActionScript Code:
army.attack == true; //and army.attack == false;

to just one equals sign, that is wrong.

I'm talking about these lines here:
ActionScript Code:
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");                                 }
__________________
www.actiontad.com - ActionScript and JavaScript sitting in a tree...
tadster 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


All times are GMT. The time now is 02:21 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.