Be a Supporter!

Need a little help with code (AS3)

  • 159 Views
  • 1 Reply
New Topic
amartainment
amartainment
  • Member since: Mar. 8, 2009
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Need a little help with code (AS3) 2013-06-05 04:38:14

Hey all
I just needed a little help with some code. Its the first time Im coding AS3 so yeah , Im pretty much clueless as to whats going wrong.

Okay what I'm trying to do is to make the enemy mc shoot whenever the hero mc is within range. So yeah, the problem is, the distance code just doesnt seem to work. I'm programming them to be on the same level so I am not checking Y values. Heres the code :

public function enemyRange(event:Event):void {
			var dbhe:Number = enemy.x - hero.x;
			var distanceBetweenHeroAndEnemy:Number = Math.sqrt(dbhe*dbhe);
			var inrange:Boolean = false;
			
			// fix this code, distance detection not working
			if(  distanceBetweenHeroAndEnemy < 50 && distanceBetweenHeroAndEnemy > 0) {
				inrange = true;
				
			}else {
				inrange = false;
				}
				if(inrange) {
					trace("inrange!");
				}
		}
	}

as you can guess hero and enemy are the mcs. Right now, I'm just having one enemy on stage to check the code so yeah. The problem is, the trace doesnt get triggered when the hero is moving as shown :
as in when the hero/enemy is approaching one another, there is no trace being triggered :S
<link> http://imageupper.com/i/?S0200010070011B1370421184782856 </link>

BUT, when the hero crosses the enemy
<link> http://imageupper.com/i/?S0200010070021B1370421184782856 </link>
either from right to left or left to right, the trace is triggered.

So why does the code work in only one case? Im taking the absolute value of (hero.x - enemy.x) so isn't it supposed to work both ways?

Any help would be appreciated
Thanks

Kirk-Cocaine
Kirk-Cocaine
  • Member since: Aug. 17, 2003
  • Offline.
Forum Stats
Moderator
Level 38
Programmer
Response to Need a little help with code (AS3) 2013-06-05 05:19:40

Hi there!

Questions about ActionScript belong in the Flash Forum. Please read the forum descriptions before posting.

What I would suggest though is tracing out distanceBetweenHeroAndEnemy to see what the distance actually is.


The water in Majorca don't taste like what it oughta.

| AS3: Main | AS2: Main | Flash Tutorials |

BBS Signature