A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: hittest creates bounce off

  1. #1
    Senior Member
    Join Date
    Aug 2006
    Posts
    293

    hittest creates bounce off

    hi all,

    hopefully can either show me or point me in the right direction of an explanation of how to do the following...

    i have a number of duplicated mc's that move around the stage at random - i also have an mc that sits in the middle of the stage this mc has the name of "area" - at present the random moving mc's use hittest to change the frames within the moving mc's from frame 1 to frame 2 and then jump back to frame when the moving mc leaves the "area" mc.

    this all works great.

    but what i want to do now is (hopefully) just edit the hittest command within the moving mc's to make the moving mc's actually bounce off the "area" mc.

    here is the hittest script that sits within the moving mc's...

    Code:
    this.onEnterFrame=function(){
    if(this.hitTest(_root.area)){
    gotoAndStop(2);
    } else {
    	gotoAndStop(1);
    }}
    is there a simple way of telling the mc's that when they hit the "area" just change direction?

    or would i need to do that in the controlling random movement script as shown below?

    Code:
    function getdistance(x, y, x1, y1) {
    	var run, rise;
    	run = x1-x;
    	rise = y1-y;
    	return (_root.hyp(run, rise));
    }
    function hyp(a, b) {
    	return (Math.sqrt(a*a+b*b));
    }
    MovieClip.prototype.reset = function() {
    	//specify the width and height of the movie
    	width = Stage.width;
    	height = Stage.width;
    	//-------------------
    	var dist, norm;
    	this.x = this._x;
    	this.y = this._y;
    	this.speed = Math.random()*25+2;
    	this.targx = Math.random()*width;
    	this.targy = Math.random()*height;
    	dist = _root.getdistance(this.x, this.y, this.targx, this.targy);
    	norm = this.speed/dist;
    	this.diffx = (this.targx-this.x)*norm;
    	this.diffy = (this.targy-this.y)*norm;
    };
    MovieClip.prototype.move = function() {
    	if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
    		this.x += this.diffx;
    		this.y += this.diffy;
    	} else {
    		this.x = this.targx;
    		this.y = this.targy;
    		if (!this.t) {
    			this.t = getTimer();
    		}
    		if (getTimer()-this.t>1000) {
    			this.reset();
    			this.t = 0;
    		}
    	}
    	this._x = this.x;
    	this._y = this.y;
    };
    hope someone can put me right
    thanks in anticipation

  2. #2
    Senior Member
    Join Date
    Aug 2006
    Posts
    293
    ...might help if i post the .fla too, as i'm not sure how clear i've been!
    Attached Files Attached Files

  3. #3
    :
    Join Date
    Dec 2002
    Posts
    3,518
    maybe try something like this...
    Code:
    MovieClip.prototype.move = function() {
            if (_root.getdistance(this.x, this.y, this.targx, this.targy) >this.speed) {
                    this.x += this.diffx;
                    this.y += this.diffy;
            } else {
                    this.x = this.targx;
                    this.y = this.targy;
                    if (!this.t) {
                            this.t = getTimer();
                    }
                    if (getTimer() - this.t > 1000) {
                            this.reset();
                            this.t = 0;
                    }
            }
            if (_root.area.hitTest(this.x, this.y)) {
                    this.reset();
            } else {
                    this._x = this.x;
                    this._y = this.y;
            }
    };
    Last edited by dawsonk; 12-07-2012 at 01:55 AM.

  4. #4
    Senior Member
    Join Date
    Aug 2006
    Posts
    293
    perfect!!! thankyou dawsonk - star!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center