PDA

View Full Version : hittest help


gLT4FM
07-15-2007, 11:54 AM
Hello,

I need some help with my flash game. Its kind of based on "space invaders" except the hittest isn't working properly. I need it to 'gotoandplay' the explosion clip on the UFO when the bullet hits it. The code that isn't working is on the bullet MC. I wouldn't usually ask for help but this is a main piece of assessment and it's due in two days.

OK, so basically down the bottom of the game is a tank controlled by the left and right arrow buttons. When space is pressed a bullet moves upwards until it hits the top of the screen, then where it is duplicated. Towards the top a UFO moves across the screen from left to right the re-appears on the left and moves back across.

The tank is a MC
The UFO is a MC
The Bullet is a MC

Tank code:
onClipEvent(enterFrame){
if(Key.isDown(Key.RIGHT)){
this._x += 15;
this._xscale = 100;
}
if(Key.isDown (Key.LEFT)) {
this._x -=15;
this._xscale = -100;
}
}
onClipEvent(enterFrame){
_root.bullet._x=this._x;
_root.bullet._y=this._y;
}


UFO code:

onClipEvent (load) {
this._y=random;
this._x=0;
}
onClipEvent (enterFrame){
this._x= this._x+25;
}
onClipEvent (enterFrame){
if (this._x> 800){
this._x=0;
this._y= random (450);
}
}



Bullet Code:

onClipEvent ( keyUp ){
if ( Key.getCode (Key.SPACE)){shooting=false;
}
}
onClipEvent ( enterFrame){
if (Key.isDown (Key.SPACE) and !shooting){
shooting=true;
count+=1;
if ( count >=50){
count=1;
}
duplicateMovieClip (this,"b" + count,10+count);
}
if ( this._y < 0 ) {
removeMovieClip (this);
}
this._y-=20;
}

onClipEvent (enterFrame){
if (this.hitTest (_root.alien) ==true) {
_root.alien.gotoAndStop(2);
unloadMovie (_root.alien);
unloadMovie (this);
_root.score = _root.n += 100;
}
}
Please help.

Noct
07-16-2007, 03:20 PM
This is where your problem is:
onClipEvent (enterFrame){
if (this.hitTest (_root.alien) ==true) {
_root.alien.gotoAndStop(2);
unloadMovie (_root.alien);
unloadMovie (this);
_root.score = _root.n += 100;
}
}

You are telling it to gotoAndStop(2), which would be fine, except that right afterwards you are telling it to unload. The code is read much faster then the clip can show that frame, so essentially, it is just deleting the clip on impact.

Rather then telling that clip to gotoAndStop, let it play that frame, and kill itself at the end of its run. You could also give it an explosion animation spanning more then one frame this way.
Either place your code to unload the clip directly in the alien clip itself on the frame after the explosion, or you could evaluate it from the bullet with something like this:
onClipEvent (enterFrame) {
if (this.hitTest(_root.alien)) {
_root.alien.gotoAndPlay(2);
_root.alien.onEnterFrame = function() {
if (this._currentframe>=5) {
delete this.onEnterFrame;
this.unloadMovie();
}
};
_root.score = _root.n += 100;
delete this.onEnterFrame;
this.unloadMovie();
}
}