
We may use cookies to help customize your experience, including performing
analytics and serving ads.
Learn More
11 | 8615 |
I hate having to do this so much, having to ask questions to solve the problem without anywhere to start.
Anyway, I have a movieclip set to visible = false. (not the exact code) I want it to appear when triggered by a hitTest to appear for 3 seconds. Does anyone have any idea?
Have a number variable for the timer. For every frame make the timer one less of its value. (timer -= 1) When the object is 'hit' set the timer to 3 times your frame rate. (So if your frame rate is 30, your timer would be set to 90) Inside the same hit test, make your object visible. Also, have another piece of code checking if the timer equals 0, and when it does, make the object invisible. The timer will continue to count down, but that does not matter, as it will not hit 0 again until it is reset back to 90 (Or whatever you use) until it is hit again.
Well that would work for 'this' instance but I plan on using a timer where that could not work. Thanks though.
You could use an imitation timer. By that i mean, have a variable number, and have an enterframe event that starts when the hit test is set off, and makes the number increase by one every frame (also, lets assume your project is 10 fps). Then have it check every frame if the number is equal to thirty (i.e. 3 seconds)
var myNumber:number = 0;
addEventListener(Event.ENTER_FRAME, myEvent)
function myEvent(e:Event)
{
myNumber += 1;
if(myNumber >= 30)
{
visible = false;
myNumber = 0;
removeEventListener(yourevent)
}
(note: this code doesn't make the object visible, you need to make a separate function for that)
After the hit test, your number starts at 0 and increases every frame. After 30 frames (3 seconds) your number will be at 30, and when this happens it sets off functions in the if() statement. This function makes it turn back to invisible, resets the number variable, and stops the event.
(another note: i didn't check if this code actually works, so don't use it as reference. It was just an example and you will hopefully get the idea.)
Well this was just like what Aegis said, I need it to be an actual timer if possible. I want this to exist in one frame because this is a game I am developing, and I do not want to have 30 frames that add to the filesize.
Well this was just like what Aegis said, I need it to be an actual timer if possible. I want this to exist in one frame because this is a game I am developing, and I do not want to have 30 frames that add to the filesize.
Depends on which version of ActionScript you are using. Off the bat, you want to set the clip to visible after the hitTest.
If you are looking for AS3 it is a little more straightforward. You want to create a Timer object. When you call getTimer,
// AS3
import flash.utils.*;
var hitTimer:Timer = new Timer(3000, 1); // 3 seconds = 3000 miliseconds, run 1 time
hitTimer.addEventListener(âtimerâ, afterHitTest);
function afterHitTest(eventArgs:TimerEvent)
{
//movieclip set to visible = false
}
hitTimer.start();, after you set the movieclip to visible. When the timer runs out, it will call the afterHitTest() function and do whatever code you put in there.
Ignore the very last sentence fragment (How I wish we could preview/edit our posts). Also, for some reason, the forum did not want to display the quotes in that last post. Here is what the code should be.
// AS3
import flash.utils.*;
var hitTimer:Timer = new Timer(3000, 1); // 3 seconds = 3000 miliseconds, run 1 time
hitTimer.addEventListener("timer", afterHitTest);
function afterHitTest(eventArgs:TimerEvent)
{
//movieclip set to visible = false
}
Strike what I said about AS3 being more straightforward, fore some reason I though the setTimeout and setInterval functions were more complicated. For this, you want setTimeout.
// AS2
var timerInt:Number = setTimeout(afterHitTest, 3000); // 3 seconds
function afterHitTest(){
//movieclip set to visible = false
}
In that line you are defining a variable. Basically we are saying that timerInt is a Number (we can actually call timerInt anything we want, it's our variable).
Unlike in AS3, in AS2 it is not required that you predefine the variable type. So if it is too bothersome or too confusing, leave it out.
So step by step:
* define a variable using var followed by the variable name (in this case timerInt)
* set the variable using the setTimeout function
* set the two arguments of the setTimeout function:
- function to execute after the timeout [in this case afterHitTest()]
- number of milliseconds to delay (in this case 3000 for 3 seconds)
Then all you have to do is define the afterHitTest() function somewhere in your code.
You must be logged in to post a reply!
We may use cookies to help customize your experience, including performing
analytics and serving ads.
Learn More