AS HitTest Help?

Started by: DNA | Replies: 7 | Views: 733

DNA
2

Posts: 441
Joined: Aug 2006
Rep: 10

View Profile
Jun 15, 2008 9:59 AM #157160
Okay, So I'm making a game which will be a reactions/top down shooter/driving game.
For the top down shooter part, I can't get the AS right.
The enemy MC has 5 parts seperated by stop();
Following, Attacking, Hit 1,Hit2,Death.
I want it so when the bullet MC hits the Enemy MC while its in following, it goes to Hit 1. It stays at the end of hit 1, and the enemy begins following again. Then, when the enemy MC is on frame 30 or something (The end of hit one) I want it so when he gets shot again it goes to hit2. And at the end of hit2 , he starts following you again. But it stays at the end of hit2. Then, when he's shot again, it goes to Death. And the enemy MC dies.
That's what I want to happen. I did it like that because I don't know any other way to give it "Health" and, since it's a zombie game, when the enemy attacks his health is replenished.

Actions in the "Actions" frame,

stop();
Num = 0;
hero.onEnterFrame = function() {
if(Key.isDown(Key.LEFT) || Key.isDown(65)){
this._x-=2;
}
if(Key.isDown(Key.RIGHT) || Key.isDown(68)){
this._x+=2;
}
if(Key.isDown(Key.UP) || Key.isDown(87)){
this._y-=2;
}
if(Key.isDown(Key.DOWN) || Key.isDown(83)){
this._y+=2;
}
var x:Number = _xmouse-this._x;
var y:Number = _ymouse-this._y;
var angleRad:Number = Math.atan2(y, x);
var angleDeg:Number = angleRad/Math.PI*180;
this._rotation = angleDeg;
};
Shootbullet = function () {
var bullet = _root.attachMovie("bullet", "bullet"+Num, _root.getNextHighestDepth());
var point = {x:this.hero.gunTip._x,y:this.hero.gunTip._y};
this.hero.localToGlobal(point);
bullet._x = point.x;
bullet._y = point.y;
bullet._rotation = _root.hero._rotation;
bullet.onEnterFrame = function() {
bullet._x += Math.cos(bullet._rotation*(Math.PI/180))*150;
bullet._y += Math.sin(bullet._rotation*(Math.PI/180))*150;
};
Num++;
};
hero.onMouseDown = function() {
Shootbullet();
};
stop();

_root.hero.stop();

onMouseDown = function() {
_root.hero.play();
}
Enemy Script

onClipEvent (enterFrame) {
if (_root.hero._y<_y) {
this._y -= 1;
}
if (_root.hero._y>_y) {
this._y += 1;
}
if (_root.hero._x>_x) {
this._x += 1;
}
if (_root.hero._x<_x) {
this._x -= 1;
}
}
Lixu

Posts: 3,859
Joined: Aug 2007
Rep: 10

View Profile
Jun 15, 2008 10:05 AM #157163
You should pm Rather Cheesy
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Jun 15, 2008 10:07 AM #157164
Here, there may be mistakes with the hit(no) = something in the if functions. I set out the frames as you did so they should be fine, guesed the instance names so you should fix them aswell. This code is ONLY for when he gets hit, it has no movement or return to following ect so add it to your code you already have.

onClipEvent (load) {
hit1 = 0
hit2 = 0
}

onClipEvent (enterFrame) {
if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 0) {
this.gotoandstop (3)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1) {
this.gotoandstop (4)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1 && hit2 = 1) {
this.gotoandstop (5)
}
}
Lixu

Posts: 3,859
Joined: Aug 2007
Rep: 10

View Profile
Jun 15, 2008 10:15 AM #157166
/Thread

It works for me
DNA
2

Posts: 441
Joined: Aug 2006
Rep: 10

View Profile
Jun 15, 2008 10:17 AM #157167
Quote from Scorpioxxx
Here, there may be mistakes with the hit(no) = something in the if functions. I set out the frames as you did so they should be fine, guesed the instance names so you should fix them aswell. This code is ONLY for when he gets hit, it has no movement or return to following ect so add it to your code you already have.

onClipEvent (load) {
hit1 = 0
hit2 = 0
}

onClipEvent (enterFrame) {
if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 0) {
this.gotoandstop (3)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1) {
this.gotoandstop (4)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1 && hit2 = 1) {
this.gotoandstop (5)
}
}


Thanks for the help, but it still doesn't work.
For a super detailed analysis, I'll now edit my first post with more detail.
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Jun 15, 2008 10:34 AM #157176
Similar code but changed for being in the frame.

hit1 = 0
hit2 = 0

if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 0) {
PLAYER.gotoandstop (3)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1) {
PLAYER.gotoandstop (4)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1 && hit2 = 1) {
_root.PLAYER.gotoandstop (5)
}
DNA
2

Posts: 441
Joined: Aug 2006
Rep: 10

View Profile
Jun 15, 2008 11:10 AM #157180
Quote from Scorpioxxx
Similar code but changed for being in the frame.

hit1 = 0
hit2 = 0

if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 0) {
PLAYER.gotoandstop (3)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1) {
PLAYER.gotoandstop (4)
}
else if (_root.BULLET.hitTest(_root.PLAYER) && hit1 = 1 && hit2 = 1) {
_root.PLAYER.gotoandstop (5)
}

I really appreciate your help, but It still doesn't work, I may have mine scripted in a different way, I'll post all the AS I'm using.
Scorpioxxx
2

Posts: 1,454
Joined: Apr 2006
Rep: 10

View Profile
Jun 15, 2008 2:54 PM #157249
I think I may have it now. Since oyu were spawning the bullet and adding depth ect my script was mssing a bit.


onClipEvent (load) {
hit1 = 0
hit2 = 0
Num = 0
}

onClipEvent (enterFrame) {
for (Num = 0; Num <= 'some value I don't know'; Num++) {
if (_root.BULLET.hitTest(_root.["PLAYER" + Num]) && hit1 = 0) {
this.gotoandstop (3)
hit1 == 1
}
else if (_root.BULLET.hitTest(_root.["PLAYER" + Num]) && hit1 = 1) {
this.gotoandstop (4)
hit2 == 1
}
else if (_root.BULLET.hitTest(_root.["PLAYER" + Num]) && hit1 = 1 && hit2 = 1) {
this.gotoandstop (5)
}
}
}


Again PLAYER should be the enemy and BULLET should be your flying thing that kills stuff. If this doesn't work your problem is beyond what I know.