Blinking Random Number Generator ActionScript3 - actionscript-3

I need a random number generator on AS3 that blinks on a screen one random number per second (1-9). The random number is fine but I'm having a problem with the blinking part. It just stays permanent on the screen instead of blinking the number.
The dynamic textbox is called myNumbers. I've tried using myNumbers.visible = !myNumbers.visible on the event handler, but it didn't work.
My code:
var mytimer:Timer = new Timer(1000,10);
mytimer.addEventListener(TimerEvent.TIMER, timerHandler);
mytimer.start();
function timerHandler(event:TimerEvent):void{
var numbers:Number = Math.floor(Math.random() * (9 - 1 + 1) + 1);
myNumbers.text = numbers+"";
}
Any help is appreciated!

You can use the same Timer to generate a number (every second) and blinks your text field (every 0.5 second).
Take this code :
var number:int = 0;
var timer:Timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
function timerHandler(event:TimerEvent):void{
if(timer.currentCount % 2 == 0){
text_field.alpha = 0.2; // you can use text_filed.visible = false;
} else {
text_field.alpha = 1; // you can use text_filed.visible = true;
number = Math.floor(Math.random() * 9) + 1;
text_field.text = String(number); // you can also write it : number.toString();
}
}
Which will give you something like this :
Hope that can help.

Hide the myNumbers before start a timer:
myNumbers.visible = false;
In the timerHandler add:
myNumbers.visible = true;
setTimeout(hideText, 500);
Add hideText function:
function hideText()
{
myNumbers.visible = false;
}

Related

Flash Hit Test Object as3

Ok I need help to figure out how to make it when the stuntman collides with the hoop it adds one point but instead it detects the collision multiple times and adds 5 points.Thanks for the help.
This is my code:
stop();
// Variables to increase money
var totalmoney = 0;
var moneygain:int = 1;
var moneylimit:int = 100000;
//on collision with hoop add 1 point to money
addEventListener(Event.ENTER_FRAME, HoopCollision);
function HoopCollision(event:Event):void
{
if(startstuntman.hitTestObject(starthoop))
{
totalmoney += moneygain;
}
Total.text = totalmoney;
trace("HIT");
}
Best thing to do is to dynamically add attribute by adding:
stop();
var totalmoney = 0;
var moneygain:int = 1;
var moneylimit:int = 100000;
starthoop["hit"] = new Boolean(false); // *** initial is not hit by startstuntman ***
addEventListener(Event.ENTER_FRAME, HoopCollision);
function HoopCollision(event:Event):void
{
if(startstuntman.hitTestObject(starthoop) && starthoop.hit == false) // *** checking additional expression ***
{
totalmoney += moneygain;
starthoop.hit = true; // *** starthoop is now hit, so next time it checks, it wont increase totalmoney because of additional expression***
}
Total.text = totalmoney;
trace("HIT");
}

How to get a percentage from new Date();

I have a digital clock for now, and I need the corn to know when 10 seconds as passed, this way it can go to the next frame. Im having difficulty finding out how to gather the 10 secs and make it out of 100% for example
its 9:30:21 and i pushed the button
at 9:30:31 it should be done
but I want to create a percentage bar based on that 10sec.. heres my code
farmSlot1.addEventListener(MouseEvent.CLICK, farmClick1);
var startTime: Date = new Date();
var startSec: Number = startTime.seconds;
function farmClick1(e: MouseEvent): void {
addChild(menu);
menu.x = 400;
menu.y = 90;
menu.buyCornBtn.addEventListener(MouseEvent.CLICK, buyCorn1);
}
function buyCorn1(e: MouseEvent): void {
var startTime: Date = new Date();
startSec = startTime.seconds;
menu.buyCornBtn.addEventListener(Event.ENTER_FRAME, cornloading1);
farmSlot1.progressB.visible = true;
menu.buyCornBtn.removeEventListener(MouseEvent.CLICK, buyCorn1);
removeChild(menu);
}
function cornloading1(event: Event): void {
var now: Date = new Date();
var hr: Number = now.hours;
var min: Number = now.minutes;
var sec: Number = now.seconds;
var finished: Number = startSec + 5
var percent = Math.round((finished-sec) * 100)
if(sec < finished){
farmSlot1.loader_txt.text = percent
Object(root).farmSlot1.progressB.bar.scaleX = percent;
trace("hit");
}else if (sec == finished && farmSlot1.currentLabel != "corn") {
removeEventListener(Event.ENTER_FRAME, cornloading1);
trace("It did it");
farmSlot1.loader_txt.text = ""
farmSlot1.gotoAndStop("corn");
}
}
At first, as I see you are writing the code inside Flash frames. Incapsulating your code within classes is better approach which allows to avoid tons of problems in the future. Here is a link described some basic approaches for using classes:
http://www.kirupa.com/developer/as3/classes_as3_pg1.htm
In answer to your question:
You can simply use flash.utils.Timer class instead of using Date and ENTER_FRAME event: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html
Some piece of code:
//Import timer class
import flash.utils.Timer;
...
//In the class's body
//Create a timer instance. Timer will be executed 10 times every 100 milliseconds
private var timer:Timer = new Timer(100, 10);
...
//Somewhere in class's method (usually in the constructor)
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
//Add click handler
button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
...
private function buttonClickHandler(event:MouseEvent):void
{
//Do some stuff and start timer.
timer.start();
}
private function timerHandler(event:TimerEvent):void
{
//Update progress indicator
}
private function timerCompleteHandler(event:TimerEvent):void
{
//1 second is over so do some stuff.
}

Timer to load random frame not working

I have three frames with "800", "450", and "635"
I updated the codes and its just jumps to frame to frame every 1 seconds now. Thats not what I need. I need the counter to reach to 0 and it jumps to ONE frame and stops right there. Thats it.
[UPDATED 2] See banner - http://magnixsolutions.com/clients/OT/9995MB-Scoreboard-April-160x600.swf
AS3 -
var fromFrame:int = 1;
var myTimer:Timer = new Timer(1000, nCount);
var frameNum:int = Math.ceil(Math.random() * mcYourScore.totalFrames)
timer_txt.text = nCount.toString();
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
function getRandomFromRange(minValue:Number, maxValue:Number):int {
return Math.round(minValue + Math.random() * (maxValue - minValue));
}
function countdown(e:TimerEvent):void {
//Display countdown
timer_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
//if End of countdown, start from 2 frame
fromFrame = (myTimer.repeatCount == myTimer.currentCount) ? 2 : 1;
mcYourScore.gotoAndStop(getRandomFromRange(fromFrame, mcYourScore.totalFrames));
}
Math.round(Math.random()) doesn't have any sense, it will return only 0 or 1 values.
var frameNum:int = Math.round(1 + Math.random() * (mcYourScore.totalFrames-1));
If you want to visit random frame on every tick, this construction should help you:
var seconds:int;
function getRandomFromRange(minValue:Number, maxValue:Number):int {
return Math.round(minValue + Math.random() * (maxValue - minValue));
}
function countdown(e:TimerEvent):void {
//Display countdown
seconds = myTimer.repeatCount - myTimer.currentCount;
timer_txt.text = seconds.toString();
//if End of countdown, start from 2 frame
mcYourScore.gotoAndStop(getRandomFromRange(((seconds == 0) ? 2 : 1), mcYourScore.totalFrames));
}
If you want to visit random frame only at the end of countdown, you could use TimerEvent.TIMER_COMPLETE event handler, or change countdown logic:
function countdown(e:TimerEvent):void {
//Display countdown
timer_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
//if End of countdown, pick random frame, from 2 frame
if(myTimer.repeatCount == myTimer.currentCount){
mcYourScore.gotoAndStop(getRandomFromRange(2, mcYourScore.totalFrames));
}
}

How do you stop setInterval() after certain number of iterations

I have tried following code and its working but how do i stop when its reach 130 ?
var textValue:Number = 67.1;
var addValue:Number = .1;
my_txt.text = textValue.toString();
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
}
setInterval(counter, 10);
setInterval returns a unique ID as an unsigned int (uint). You can use clearInterval with this ID to stop the interval. The code:
var textValue:Number = 67.1;
var addValue:Number = .1;
var myInterval:uint;
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
if( textValue >= 130 ) {
clearInterval(myInterval);
}
}
myInterval = setInterval( counter, 10 );
You can stop an interval by using clearInterval. Try this:
var textValue:Number = 67.1;
var addValue:Number = .1;
my_txt.text = textValue.toString();
function counter(){
textValue += addValue;
my_txt.text = textValue.toString();
//check for end value
if (textValue>=130)
{
//clear the interval
clearInterval(intervalID);
}
}
//store the interval id for later
var intervalID:uint = setInterval(counter, 10);
Since it seems like you may be using actionscript 3, I suggest not using an interval at all. A Timer object may be better as it can offer better control, such being able to set the number of times it fires off before stopping itself and being able to easily start, stop, and restart the timer as needed.
Example of using a Timer object and adding an event listener for each tick
import flash.utils.Timer;
import flash.events.TimerEvent;
// each tick delay is set to 1000ms and it'll repeat 12 times
var timer:Timer = new Timer(1000, 12);
function timerTick(inputEvent:TimerEvent):void {
trace("timer ticked");
// some timer properties that can be accessed (at any time)
trace(timer.delay); // the tick delay, editable during a tick
trace(timer.repeatCount); // repeat count, editable during a tick
trace(timer.currentCount); // current timer tick count;
trace(timer.running); // a boolean to show if it is running or not
}
timer.addEventListener(TimerEvent.TIMER, timerTick, false, 0, true);
Controlling the timer:
timer.start(); // start the timer
timer.stop(); // stop the timer
timer.reset(); // resets the timer
Two events it throws:
TimerEvent.TIMER // occurs when one 'tick' of the timer has gone (1000 ms in the example)
TimerEvent.TIMER_COMPLETE // occurs when all ticks of the timer have gone (when each tick has happened 11 times in the example)
API Documentation: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Timer.html

action script 3.0 .I had tried a program that should move the dragger as well as the content movieclip,

In this code i need a progressbar to progress through while the imported .swf file is playing, meanwhile i should able to drag the dragger in the progress bar (i.e the rate of movement of dragger should sync with rate of .swf file playing). I got Argument error: #2109 Frame label 459.99 not found in scene1.
var loader:Loader = new Loader();
playBtn.visible = true;
pauseBtn.visible = false;
btn_00.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_01.addEventListener(MouseEvent.CLICK, fileLoaded);
btn_02.addEventListener(MouseEvent.CLICK, fileLoaded);
function fileLoaded(evt:MouseEvent):void
{
var fileName:String = evt.currentTarget.name;
var fileNumber:String = fileName.split("_")[1];
var urlPath:String = "assets/file_" + fileNumber + ".swf";
loader.load(new URLRequest(urlPath));
addChild(loader);
}
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
function swfLoaded(event:Event):void
{
addEventListener(Event.ENTER_FRAME, trackPlayback);
}
function trackPlayback(event:Event):void
{
var perPlayed:Number = MovieClip(loader.content).currentFrame / MovieClip(loader.content).totalFrames;
progressbar.drag.x = (progressbar.bar.width - progressbar.drag.width) * perPlayed;
}
progressbar.drag.buttonMode = true;
var dragClicked:Boolean = false;
var xpos:Number = progressbar.bar.x * progressbar.drag.width;
progressbar.drag.addEventListener(MouseEvent.MOUSE_DOWN,dragMouseDown);
function dragMouseDown(evt:MouseEvent):void
{
trace(" inside mouse down ");
dragClicked = true;
progressbar.drag.startDrag(false,new Rectangle(xpos,0,progressbar.width-progressbar.drag.width,0));
}
progressbar.drag.addEventListener(MouseEvent.MOUSE_UP,dragMouseUp);
function dragMouseUp(evt:MouseEvent):void
{
dragClicked = false;
progressbar.drag.stopDrag();
var cnt:Number = (progressbar.drag.x/(progressbar.width-progressbar.drag.width))*MovieClip(loader.content).totalFrames;
MovieClip(loader.content).gotoAndPlay(cnt);
}
Pls solve my issue.
Thanks in advance.
To access a specific frame, you need to provide an integer value where at the moment you are using a floating-point value.
A simple fix would be to cast the Number to an int:
MovieClip(loader.content).gotoAndPlay(int(cnt));