PDA

View Full Version : hit test on top of enemy



rondog
June 3rd, 2007, 05:27 PM
Im trying to make a mario like game where if you jump ontop of the enemy it gets smashed. Right now I have it so when you hit anywhere of the enemy you die. I want it so if you are on top of the enemies head it dies rather than you. How would I do that kind of hitTest?

Dark Viper
June 3rd, 2007, 06:15 PM
When you do the hittest you should be able to check if your characters y coordinate is above the y coordinate of the enemy.

LordArkain
June 3rd, 2007, 07:03 PM
Just make it check to see if the hero is falling and hitTest with the enemy. Then do whatever for an action script. Here is a script from a game I made that was just like mario.


//defined variables on load
onClipEvent(load){
PLAYERX=_root.mcPLAYER._x;
PLAYERY=_root.mcPLAYER._y;
MAPX=_root.map._x;
MAPY=_root.map._y;
lives=3;
coins=0;
running = false;
jumping = true;
jump = 0;
movespeed=4;
falling=true;
fallspeed=10;
position="right";
}
//left right movement and ending stance
onClipEvent(enterFrame){
//coin score/lives and stuff
_root.Level=_root.level;
_root.Lives=lives;
_root.Coins=coins;
if(coins>59){
coins-=60;
lives+=1;
}
//start pause detection
if(_root.paused==false){
if((Key.isDown(Key.RIGHT)||Key.isDown(68))&& !Hitting){
running=true;
position="right";
this.gotoAndStop("standright");
if(!_root.barrier2.hitTest(_root.bg.barrier2)){
if(_root.barrier2.hitTest(this._x, this._y, true)){
_root.bg._x-=0;
}else{
if (_root.bg.walls.hitTest(getBounds(_root).xMax, _y, true)) {
_root.bg._x-=0;
Hitting = true;
}else{
_root.bg._x-=movespeed;
}
}
}
}else if(Key.isDown(Key.LEFT)||Key.isDown(65)){
running=true;
position="left";
this.gotoAndStop("standleft");
if(!_root.barrier.hitTest(_root.bg.barrier)){
if(_root.barrier.hitTest(this._x, this._y, true)){
_root.bg._x+=0;
}else{
if ((Key.isDown(Key.LEFT) || Key.isDown(65)) && !Hitting) {
_root.bg._x+=0;
Hitting = true;
}else{
if (_root.bg.walls.hitTest(getBounds(_root).xMin, _y, true)) {
_root.bg._x+=0;
Hitting = true;
}else{
_root.bg._x+=movespeed;
}
}
}
}
}else{
running=false;
Hitting = false;
if(position=="right"){
this.gotoAndStop("standright");
}else if(position=="left"){
this.gotoAndStop("standleft");
}
}
//jumping
if (Key.isDown(Key.UP) && !jumping && !falling||Key.isDown(87)&& !jumping && !falling) {
_root.jumpsound.start();
jumping = true;
}
if (jumping) {
this._y -= jump;
jump -= 1;
if (jump<0) {
falling = true;
}
if (jump<-12) {
jump = -12;
}
}
//falling
if (_root.bg.ground.hitTest(this._x, this._y, true) && falling) {
jump = 15;
jumping = false;
falling = false;
}
if (!_root.bg.ground.hitTest(this._x, this._y, true) && !jumping) {
this._y += fallspeed;
falling=true;
}
//speed change to running
if(_root.bg.ground.hitTest(this._x, this._y, true) && Key.isDown(Key.SHIFT)){
if(running==true){
if(movespeed!=10){
movespeed+=.2;
}
if(movespeed>10){
movespeed=10;
}
}
}
if(_root.bg.ground.hitTest(this._x, this._y, true) && !Key.isDown(Key.SHIFT)){
if(movespeed!=4){
movespeed-=.2;
}
if(movespeed<4){
movespeed=4;
}
}
//end pause detection
}
}

Then on the springs, enemies and such I put this.


onClipEvent(enterFrame){
//start pause detection
if(_root.paused==false){
if (this.hitTest(_root.mcPLAYER._x, _root.mcPLAYER._y, true) && _root.mcPLAYER.falling==true) {
this.gotoAndPlay(2);
_root.mcPLAYER.jump = 15;
}
//end pause detection
}
}