flash actionscript:
I have spaceships that shoot lasers, lasers are class instances extending movieclip. On every frame, lasers do a hitTest to determine if it collides with an opponent ship: function onEnterFrame() { if(this.hitTest( _root.gm.opponent) ) { _root.gm.opponent.recieveHit(this.damage); this.gotoAndPlay("hit"); } } *'this' refers to the laser instance I have also tried ( _root.gm.opponent.hitTest(this)) both return false - no collision - even when there is one anyone know what might be going wrong?
i have done the exact same thing u did if (this.hitTest( _root)){ _root.gotoAndStop( 2 );
A odd problem... where the script are localized ? Inside a frame of main timeline or in the mc ? Usually i do it: 1)select the Enemy MC 2)Call F9 3)Put this script inside the script of MC (not in the internal timeline): onEnterFrame(){ if(this.hitTest(_root.collisionObject)){ //put my code here } } 4)Or in the internal timeline (but the MC must loop in this frame): if (this.hitTest(_root.collisionObject)){ //put my code here }
the script is within the laser's class: class laser extends MovieClip{ function onEnterFrame() { if(this.hitTest( _root.gm.opponent) ) { _root.gm.opponent.recieveHit(this.damage); this.gotoAndPlay("hit"); } } }
You can out it inside the class constructor this.onEnterFrame = function(){ //your hit test code }
i actually have the class constructor already written as: function Laser() { var dist = Point.distance( new Point(0,0), gunPt ); var theta = Math.atan2( gunPt.y,gunPt.x ) + ship.heading; var cP = Point.polar( dist, theta ); this._x = ship._x + cP.x; this._y = ship._y + cP.y; origin = new Point(this._x,this._y); heading = this._rotation*Math.PI/180; initXspeed = ship.xSpeed; initYspeed = ship.ySpeed; } to create the classes as movieclips i used: var opponent = _root.attachMovie("linkID","instName",depth,vars);
A movie created with "attachMovie" is a common MovieClip object ,not your extended mc . You must associate the variable "onEnterFrame" with a function after the movie is attached. an example: ColideFunction = function(){ //my hittest function } var my_enemy = _root.attachMovie("enemy_instance","enemy1",depth,{onEnterFrame:ColideFunction} or attach the enemy mc and later my_enemy.onEnterFrame = ColideFunction;
well i left out code to lessen details, i typecast my attached mcs, therefore anythign I define in the type classes, such as onEnterFrame, is inherited automatically when typecasted: the focus of my problem lies within the hitTest function. My mcs' onEnterFrame are functioning correctly, i have a few different extended classes. hitTest merely isnt returning true when the two mcs are overlapping. I have assigned static depths for both spaceships and the lasers are created starting at 1000 depth. I think maybe the hitTest problem has something to do with the hitAreas of my MCs. Does anyone have experience using hitAreas and hitTest? What I want to do is use only the (x,y) coord at the tip of the lasershot to test if its within the spaceship's coordinates, to do so I have tried hitTest in this fashion:(*'this' refers to the lasershot instance, as the code is written inside my onEnterFrame function of the lasershot class ). _root.gm.opponent.hitTest(this._x,this._y,true)) i know this is correct usage, yet it never returns true...
i have also tried this inside my onEnterFrame for laser: //-use the ship MC as the hitArea this.hitArea = _root.gm.opponent; //-if the laser's registration point lies within the ship MC hitArea if( this.hitTest(this._x,this._y,true) ) { // collision success - call resolving functions } -this does not work unfortunatly. what i expected was to use hitTest on the laser hitArea to determine if the laser's point overlaps. hitArea is defaulted by its movieclip owner, but i reassigned it to the opponent ship, so it should still test the coords against the ship's Area, right?
You can calculate the hit by yourself by the heigt values and widht.. but it?s not easy if your mc has a form diferent than a box. Its too strange the hitTest return false ,I will see your problem later becase here (my office) i dont have Flash installed.
yea, i was about to just use a bounding box for collisions and calculate the coordinates on my own, but i am determined to get the hitTest working correctly, i really want the collisions to detect using the ship's graphic instead of a box. thanks for the help, i apprecaite it.
Ipdont shure if it?s the same problem but if you do this: mymovie = this.attachMovie(blablalal...); if(mymovie.hitTest(anyMc)){ trace("Collision"); //collision case code } else{ trace("No collision !") } this code always trace "no collision" and it?s not a bug.This occurs because the attached movie will be present after the execution of the script of the current frame .
Don't see what you're looking for? Try a search.
|