A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: setInterval problem

Hybrid View

  1. #1

    setInterval problem

    In a valliant attempt to finally start using some MX code, i tried to recode my random moving script from flash 5 to MX. It works, but doesn't seem to ease in and out anymore. I've tried adding updateAfterEvent, but that doesn't seem to work..


    This is the Flash 5 code:

    Code:
    onClipEvent(load){
    	targetX = Math.round(Math.random()*550);
    	targetY = Math.round(Math.random()*400);
    }
    
    onClipEvent(enterFrame){
    	xDistance= targetX - _parent.block._x;
    	yDistance= targetY - _parent.block._y;
    	
    
    	if(xDistance > 10 || xDistance < -10){
    		_parent.block._x += xDistance*0.09;
    	} else {
    		targetX = Math.round(Math.random()*550);
    	}
    	
    	if(yDistance > 10 || yDistance < -10){
    		_parent.block._y += yDistance*0.09;
    	} else {
    		targetY = Math.round(Math.random()*400);
    	}
    
    
    }
    and this is the MX code

    Code:
    targetX = Math.round(Math.random()*550);
    targetY = Math.round(Math.random()*400);
    
    function checkPos(){
    	xDistance= targetX - blok._x;
    	yDistance= targetY - blok._y;
    	
    	if(xDistance> -10 || xDistance < 10){
    		targetX = Math.round(Math.random()*550);
    	}
    	
    	if(yDistance> -10 || yDistance < 10){
    		targetY = Math.round(Math.random()*400);
    	}
    }
    
    function move(){
    	xDistance= targetX - blok._x;
    	yDistance= targetY - blok._y;
    	blok._x += xDistance*0.09;
    	blok._y += yDistance*0.09;
    }
    
    setInterval(checkPos, 88);
    updateAfterEvent();
    setInterval(move, 88);
    updateAfterEvent();
    does anyone have any suggestions as to how I can improve the MX script to make it move smoothly again?

  2. #2
    Junior Member
    Join Date
    Sep 2002
    Location
    Adelaide, South Australia
    Posts
    8

    G'day

    Hey!

    I use this code for everything, and it works wickidly in MX.
    You can just cut and paste it onto any movieclip you want without changing a thing......good luck!





    onClipEvent (load) {
    targetx = Math.round(Math.random()*400);
    targety = Math.round(Math.random()*400);
    }
    onClipEvent (enterFrame) {
    xdiv = Math.round ((targetx - this._x)/5);
    this._x = Math.round (this._x + xdiv);

    ydiv = Math.round ((targety - this._y)/5);
    this._y = Math.round (this._y + ydiv);

    if (xdiv == 0){
    targetx = Math.round (Math.random ()*400);
    }
    if (ydiv == 0){
    targety = Math.round (Math.random ()*400);
    }
    }
    cheers

  3. #3
    Thanks for the script, Drum109, but I was more looking for a way to employ those set Interval things in MX

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