A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to stop a random motion: need help

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Posts
    2

    How to stop a random motion: need help

    Hi there,

    I followed a random motion tutorials found somewhere on the web. I have a small word made of 10x10 black squares. When someone clicks the word, all the squares start moving around randomly.

    What I want to do now is when someone clicks on a moving square, all the squares move back to a given position (obviously, they stop moving randomly).

    Is this possible? This is the code I'm using:

    Code:
    onClipEvent (load) {
    	//data you may want to change
    	width = 500;
    	height = 400;
    	speed = Math.round(Math.random()*1)+1;
    
    }
    onClipEvent (enterFrame) {
    	//x movement
    	if (x_new>this._x) {
    		sign_x = 1;
    	} else {
    		sign_x = -1;
    	}
    	dx = Math.abs(x_new-this._x);
    	if ((dx>speed) || (dx<-speed)) {
    		this._x += sign_x*speed;
    	} else {
    		x_new = Math.random()*width;
    	}
    	//y movement
    	if (y_new>this._y) {
    		sign_y = 1;
    	} else {
    		sign_y = -1;
    	}
    	dy = Math.abs(y_new-this._y);
    	if ((dy>speed) || (dy<-speed)) {
    		this._y += sign_y*speed;
    	} else {
    		y_new = Math.random()*height;
    	}
    }
    I know that I should have it has a function, but I don't even know how to make this bit of code in a function :tired:

    Any help would be greatly appreciated.

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Posts
    2,049
    Just a guess without making a sample you could try something like this

    Code:
    onClipEvent (load) {
    	//data you may want to change
    	width = 500;
    	height = 400;
    	speed = Math.round(Math.random()*1)+1;
    	var StartX:Number = this._x;
    	var StartY:Number = this._y;
    
    }
    onClipEvent (enterFrame) {
    	//x movement
    	if (x_new>this._x) {
    		sign_x = 1;
    	} else {
    		sign_x = -1;
    	}
    	dx = Math.abs(x_new-this._x);
    	if ((dx>speed) || (dx<-speed)) {
    		this._x += sign_x*speed;
    	} else {
    		x_new = Math.random()*width;
    	}
    	//y movement
    	if (y_new>this._y) {
    		sign_y = 1;
    	} else {
    		sign_y = -1;
    	}
    	dy = Math.abs(y_new-this._y);
    	if ((dy>speed) || (dy<-speed)) {
    		this._y += sign_y*speed;
    	} else {
    		y_new = Math.random()*height;
    	}
    }
    
    onClipEvent(mouseDown){
    	delete onEnterFrame;
    	this._x = StartX;
    	this._y = StartY;
    }

  3. #3
    Junior Member
    Join Date
    Apr 2007
    Posts
    2
    Hmm the movie clip dows "jump" to another place, but it keeps moving. I asked about the functiona because I read somewhere it must de done with 'delete', but it was something like "delete this.onEnterFream" - but, like I said, I have no idea on how to make this into a function.

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