dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: time delay within functions?

Hybrid View

  1. #1
    Senior Member
    Join Date
    Mar 2001
    Posts
    527
    Can anyone tell me if it is possible to put a time delay within a function.

    I have a modular movieclip that moves another movieclip towards a target, but gives it that real motion effect.
    The script for the modular mc is set over 3 frames

    1. sets out the variables
    2. has the bones of the action as follows
    - finds out distance x and y between where the movieclip to be moved is at an instant and the works out the distance between it and the target x and y. This gives 2 variables that are the x difference and the y difference. These values are then divided by say 10 and then added to the original x and y position of the movieclip.
    3. has the gotoAndPlay (2) command

    so this modular movieclip controls the action of another.

    Can i write this as a function?

    i have a function to calculate the x and y difference and add on a fraction of this amount to the movieclip's current coordinates on press of a button. Continued presses move the movieclip towards its target in the desired way but i want all of this to happen on one press only.

    i have tried loops to do this but it sends Flash into "i am not going to do as you want me to because i am going to perform so many calculations that i am going to crash" mode!!!!!!!!!!


    i kind of new as i was coding that this was going to happen.

    would some form of time delay within the function work?
    maybe putting in a loop that only runs a certain amount of times as dictated by the initial distances calculated

    i did try these but flash calculates too quickly for the eye to see and you have a movieclip instantaneously changing position - not the desired effect

    I hope that i have explained myself clearly

    Any help would be greatly appreciated

    Happy flashing!!!!!!!!!1

  2. #2
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    just an idea: set a variable "active" to 1 to start the move.
    When the movieclip is in frame 2
    if active is still 1 and there is a difference, move - otherwise set active to 0
    frame 3: go back - or perhaps even stop if active is 0

    Hope it works ...

    Musicman

  3. #3
    Senior Member
    Join Date
    Mar 2001
    Posts
    527
    Thanks Musicman, but that was not really my intention
    i already have a movie clip that can give me the motion i want by changing x and y values and then in the next frame merely going back to this frame to re evaluate the position, but what i was asking was whether or not you could write this instruction into a function.


    Sample code

    function moveyou (clipName1, targetX) {
    Dist = (targetX-clipName1._x);
    clipName1._x = clipName1._x+(Dist/10);
    !!!!!!!!!!Looping code to go here!!!!!!!!!!!!!
    }


    i have tried the usual loops for and while to no avail


    like i say i have already achieved the motion i want in a controlling movieclip that just has actions. I was just wondering whether i could write the command into a function. i think this would be easier to call and use as opposed to making a movieclip for each motion i require.

    Happy flashing!!!!!!!!!!!!!1

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,044
    Hi,

    sure you could...
    since you want to store data per movieclip and call the function once per frame, you just have to pass "this" to the function as a reference to the current data

    Musicman

  5. #5
    Senior Member
    Join Date
    Aug 2000
    Posts
    264
    I think gilesb means delaying the execution of code through the function, not the movie!

    The only way i've done this is to use getTimer() on the movieClip calling the function and only calling your function when getTimer() = a chosen amount of time.

    eg
    Code:
    onClipEvent (load) {
    	var timer = getTimer();
    	var delayAmount = 1000;
    	var nextTrigger = timer+delayAmount;
    }
    onClipEvent (enterFrame) {
    	timer = getTimer();
    	if (timer>=nextTrigger) {
    		moveyou(clipName1, targetX);
    		nextTrigger = timer+delayAmount;
    		trace ("TRIGGERED moveyou FUNCTION");
    	}
    }
    To change the nexTrigger, just set the delayAmount within your moveyou function!

    Is this what you meant giles?

    Sam

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