Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Flash: AS3 need (noob) help!
New Posts  All Forums:Forum Nav:

Flash: AS3 need (noob) help!

post #1 of 4
Thread Starter 
i got some help with a code on stackoverflow, but I'm not really sure how to apply it to my instance?

I'm trying to generate a random number (1-9) every millisecond, having it applied to my textbox, which would then display the rapidly changing random number.

heres the code:


Quote:
import flash.utils.setInterval;
import flash.utils.clearInterval;

//variable for the intervalID,
//and the variable that will be assigned the random number
var rnGenIID:uint, rn:uint;

//function to update the rn variable
//to the newly generated random number
function updateRN(){
rn = random0to9();
//the trace is here to show you the newly updated variable
trace(rn);
}
function random0to9 ():uint{
return Math.floor(Math.random()*10);
}


//doing this assigns rnGenIID a number representing the interval's ID#
//and it set it up so that the function updateRN will be called every 1 ms
rnGenIID = setInterval(updateRN,1);

where do I plug in my instancename.text thing at?
I probably don't even have the right approach at this because I'm very new to flash.. so feel free to correct me!

Thanks!
    
CPUMotherboardGraphicsRAM
Phenom II X4 940 @3.8GHz 1.46v DFI 790FXB-M2RS Sapphire HD4870 1G 2x2G Dominators @ 1066mhz 
Hard DriveOptical DriveOSMonitor
500G WD Sony w7 Ultimate 22" Widescreen Acer 
KeyboardPowerCaseMouse
Logitech G15 (old version) OCZ GameXtreme 700w CM 690 Modded Razer Diamondback3G 
Mouse Pad
QcK Steelpad 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom II X4 940 @3.8GHz 1.46v DFI 790FXB-M2RS Sapphire HD4870 1G 2x2G Dominators @ 1066mhz 
Hard DriveOptical DriveOSMonitor
500G WD Sony w7 Ultimate 22" Widescreen Acer 
KeyboardPowerCaseMouse
Logitech G15 (old version) OCZ GameXtreme 700w CM 690 Modded Razer Diamondback3G 
Mouse Pad
QcK Steelpad 
  hide details  
Reply
post #2 of 4
You should use a timer like this:

Code:
//creates variable
var randomnumber = 0

//creates timer and sets it at 1 milisecond
var timer:Timer = new Timer(1);
timer.addEventListener(TimerEvent.TIMER, updatenumber);
timer.start();

//this keeps picking a random number 1-9 and updates the textbox with it. this function is triggered by the timer.
function updatenumber (event:TimerEvent)
{
    randomnumber = Math.floor(Math.random()*(1+9-1))+1;
    randomnum.text = (randomnumber)
}
First Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD x3 425 OCed @ 3.6 GHz Gigabyte MA78LM-S2 MSI GTX 460 Cyclone 768 MB 2 GB 
Hard DriveOSMonitorKeyboard
Hitachi 1 TB 7200 RPM Windows 7 Ultimate 32 Bit Emprex 22" 1680x1050 Standard Compaq Keyboard 
PowerCaseMouse
Thermaltake 650 Watt Coolermaster CM 690 Logitech G5 
  hide details  
Reply
First Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD x3 425 OCed @ 3.6 GHz Gigabyte MA78LM-S2 MSI GTX 460 Cyclone 768 MB 2 GB 
Hard DriveOSMonitorKeyboard
Hitachi 1 TB 7200 RPM Windows 7 Ultimate 32 Bit Emprex 22" 1680x1050 Standard Compaq Keyboard 
PowerCaseMouse
Thermaltake 650 Watt Coolermaster CM 690 Logitech G5 
  hide details  
Reply
post #3 of 4
Thread Starter 
nevermind. THANK YOU, that is perfect!


If I were to use this script multiple times on one frame, for different instances, would I have to change the name of certain variables or what?
Edited by Wasting Away - 2/2/11 at 7:11pm
    
CPUMotherboardGraphicsRAM
Phenom II X4 940 @3.8GHz 1.46v DFI 790FXB-M2RS Sapphire HD4870 1G 2x2G Dominators @ 1066mhz 
Hard DriveOptical DriveOSMonitor
500G WD Sony w7 Ultimate 22" Widescreen Acer 
KeyboardPowerCaseMouse
Logitech G15 (old version) OCZ GameXtreme 700w CM 690 Modded Razer Diamondback3G 
Mouse Pad
QcK Steelpad 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Phenom II X4 940 @3.8GHz 1.46v DFI 790FXB-M2RS Sapphire HD4870 1G 2x2G Dominators @ 1066mhz 
Hard DriveOptical DriveOSMonitor
500G WD Sony w7 Ultimate 22" Widescreen Acer 
KeyboardPowerCaseMouse
Logitech G15 (old version) OCZ GameXtreme 700w CM 690 Modded Razer Diamondback3G 
Mouse Pad
QcK Steelpad 
  hide details  
Reply
post #4 of 4
Depends, do you want the different instances to have different random numbers (1-9)? Also by different instance, you mean like another textbox that displays these random numbers right.
First Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD x3 425 OCed @ 3.6 GHz Gigabyte MA78LM-S2 MSI GTX 460 Cyclone 768 MB 2 GB 
Hard DriveOSMonitorKeyboard
Hitachi 1 TB 7200 RPM Windows 7 Ultimate 32 Bit Emprex 22" 1680x1050 Standard Compaq Keyboard 
PowerCaseMouse
Thermaltake 650 Watt Coolermaster CM 690 Logitech G5 
  hide details  
Reply
First Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD x3 425 OCed @ 3.6 GHz Gigabyte MA78LM-S2 MSI GTX 460 Cyclone 768 MB 2 GB 
Hard DriveOSMonitorKeyboard
Hitachi 1 TB 7200 RPM Windows 7 Ultimate 32 Bit Emprex 22" 1680x1050 Standard Compaq Keyboard 
PowerCaseMouse
Thermaltake 650 Watt Coolermaster CM 690 Logitech G5 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Flash: AS3 need (noob) help!