Actionscript 2 : Custom Interval Function - timer

I Been messing with Actionscript 2 for about 2 days.. And i have not learned much as im blindly going along with it... But i wrote a function and im curious as to why its not working.. So can someone tell me whats wrong with this code , or even if it could work?
function Interval(Funct:Function, Seconds:Number, Duration:Number){
var MyInterval:Number;
Seconds = Seconds * 1000;
MyInterval = setInterval(Funct, Seconds, Duration);
if(Seconds >= Duration * 1000)
{
clearInterval(MyInterval);
}
return MyInterval;
}
Problem Solved , Solution Below :
function Interval(Funct:Function, Seconds:Number){
Seconds = Seconds * 1000;
_global.i = setInterval(Funct, Seconds);
}
Even though i did want a limit as to when it would stop if specified , im okay with this for now
Thank you Ali-o-kan
Edit : Ran into another problem with this function.. Whenever i enable it more than once using different keys , it leaves it on while creating more whenever i press the button again.. Any way to fix this?

Related

NAudio trimming mp3 file

I'm trying to trim a mp3 file.
using this code:
private void TrimMp3(string open, string save)
{
using (var mp3FileReader = new Mp3FileReader(open))
using (var writer = File.Create(save))
{
var startPostion = TimeSpan.FromSeconds(60);
var endPostion = TimeSpan.FromSeconds(90);
mp3FileReader.CurrentTime = startPostion;
while (mp3FileReader.CurrentTime < endPostion)
{
var frame = mp3FileReader.ReadNextFrame();
if (frame == null) break;
writer.Write(frame.RawData, 0, frame.RawData.Length);
}
}
}
"open" is the file I'm trimming and "save" is the location I'm saving.
The trimming works but not fully. The new file does start from 60 seconds but it keeps going and not stopping at 90 seconds. For example if the file is 3 minutes it will start at 1 minute and end at 3. Its like the while is always true. What am I doing wrong here?
Thanks in advance!
I have no idea what your Mp3FileReader is doing there. But your while loop looks odd. Does mp3FileRead.ReadNextFrame() also change mp3FileReader.CurrentTime ? If not then there is your problem.
You should atleast do mp3FileReader.CurrentTime + 1Frame. Otherwise your currenttime is never changed and loop will always be true
In NAudio 1.8.0, Mp3FileReader.ReadNextFrame does not progress CurrentTime, although I checked in a fix for that recently.
So you can either get the latest NAudio code, or make use of the SampleCount property on each Mp3Frame to accurately keep track of how far through you are yourself.

AlarmManager setRepeating method is not going off exactly every 60 minutes

my problem is that I have an Alarmmanager that shoudl go off every 60 minutes to a defined time.
This, however, is only working for the first time.
With every next hour passing by Alarmmanager delays its work for 2 or 3 minutes.
Here an example:
hour is set to 4 p. m.
minute is set to 32
timer is set to 60 minutes
Calendar timeOff9 = Calendar.getInstance();
timeOff9.set(Calendar.HOUR_OF_DAY, hour);
timeOff9.set(Calendar.MINUTE, minute);
am.setRepeating(AlarmManager.RTC_WAKEUP, timeOff9.getTimeInMillis(), timer*60000, pi);
Maybe someone knows why that is so?
I am using API level 15. As of documentation API 19 setRepeating == setInexactRepeating
Thank you very much!
You almost answered your own question by referring to the documentation.
API 19 did indeed infer calls to setRepeating would be treated as setInexactRepeating.
The way to handle this is to set a new alarm when you handle receiving an alarm (>= API 19).
e.g.
#SuppressLint("NewApi")
private void setAlarm(AlarmManager alarmManager, long time,
PendingIntent pIntent, boolean repeat) {
if (android.os.Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pIntent);
} else {
if (repeat) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time,
AlarmManager.INTERVAL_DAY * 7, pIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, time, pIntent);
}
}
}
And then when you receive alarm..
/***
* From version 19 (KitKat) notification that are set repeating aren't
* exact, so the notification needs to be scheduled again each time it is
* received
*/
private void scheduleNextNotification() {
if (android.os.Build.VERSION.SDK_INT < 19) {
return;
}
// set alarm that would have otherwise been repeating
}

User input for a counter

I'm totally new to this and I'm trying to learn by doing. I have some basic skills in c and I've been making small, simple apps for about 2 weeks all by studying other apps and trying to understand how they are built.
I'm trying to make a counter now that is supposed to count people going in or out.
I got everything to work perfectly except for letting the user define the maximum amount. I would like to have the user type in the max amount in the text field and then press a button called start which would give the variable max the value typed in the text box.
Can I get some help with that?
Pseudo code:
mx = Max from User;
nrPersons = 0;
While (Userinput is not ExitProgramm) do
{
if (0 < nrPersons < mx) // allwoed range
{
run your programm that works
}
else
{
if (nrPersons > mx) => take action
if (nrPersons < 0) => take action
}
}

Create a simple countdown in processing

I have searched up so many sites on Google to try and get this to work but NO ONE seems to have this anywhere , and if they do it's just NOT working with my program... What I am trying to achieve is to have a player recoil that when the player gets hit, he has a "x" amount of time between getting hit the first time and the second time.
So I have a Boolean "hit" = false and when he gets hit, it changes to true. Which means he can't get hit again until it's changed to false again.
So I'm trying to set up a function in my program to set a "timer" for "x" amount of seconds IF hit = true and once that timer hits "x" amount of seconds, hit will get switched back to false.
Anyone have any ideas?
Thanks!!
A simple option is to manually keep track of time using millis().
You would use two variables:
one to store elapsed time
one to store the wait/delay time you need
In the draw() method you would check if the difference between the current time (in millis.) and the previously stored time is greater(or equal) to the delay.
If so, this would be your cue to do whatever based on the delay chosen and update the stored time:
int time;
int wait = 1000;
void setup(){
time = millis();//store the current time
}
void draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
println("tick");//if it is, do something
time = millis();//also update the stored time
}
}
Here's a slight variation the updates a 'needle' on screen:
int time;
int wait = 1000;
boolean tick;
void setup(){
time = millis();//store the current time
smooth();
strokeWeight(3);
}
void draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
tick = !tick;//if it is, do something
time = millis();//also update the stored time
}
//draw a visual cue
background(255);
line(50,10,tick ? 10 : 90,90);
}
Depending on your setup/needs, you may choose to wrap something like this into a class that can be reused. This is a basic approach and should work with the Android and JavaScript versions as well (although in javascript you've got setInterval()).
If you're interested in using Java's utilities, as FrankieTheKneeMan suggested, there is a TimerTask class available and I'm sure there are plenty of resources/examples out there.
You can run a demo bellow:
var time;
var wait = 1000;
var tick = false;
function setup(){
time = millis();//store the current time
smooth();
strokeWeight(3);
}
function draw(){
//check the difference between now and the previously stored time is greater than the wait interval
if(millis() - time >= wait){
tick = !tick;//if it is, do something
time = millis();//also update the stored time
}
//draw a visual cue
background(255);
line(50,10,tick ? 10 : 90,90);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.4/p5.min.js"></script>

Flash Resetting array values

I was working on a test project that I will later incorporate into a much larger work that is a simple quiz game. I made an array with my questions:
var questions1:Array=["nitrogen dioxide","sulfur hexafluoride",..."]
and in a second layer I made a button that cycles through the questions randomly.
import flash.events.MouseEvent;
var qno=0;var rnd1;
function change_question(){
rnd1=Math.ceil(Math.random()*questions1.length)-1;
q.text=questions1[rnd1];
if(questions1[rnd1]=="X"){change_question();}
questions1[rnd1]="X";
}
change_question();
next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
qno++;change_question();
}
This part works great, because I was following a tutorial. Text appears in a dynamic text box I created as it should. This tutorial taught to change the value of the array to X with every choice and to ignore choose a different question every time it encountered an X
After it cycles through all the questions I basically get an infinite loop in my output section of flash because it can't find any more non-X values. I was hoping someone had information on how to press a button an change the array back to its default settings so that a teacher (because that is who it is for) has a way of resetting the file when they have reached the end of the quiz.
Thanks everyone!
As per my understanding you wanted to randomize your questions and after showing all the entire questions you wanted to reset your questions.
As per your code you get a random question and updating the same array by pushing value 'X'. Rather than doing this what you need to do is to preserve the array only randomize its position. So that you can use the same value once you cover your all questions
I have added code here.
import flash.events.MouseEvent;
var qno=0;var rnd1;
var questions1:Array=["nitrogen dioxide","sulfur hexafluoride","carbon dioxide","carbon monooxide"];
var nAttmeptedCount = 0;
var shuffledLetters:Array;
function change_question()
{
if(qno == questions1.length)
{
qno = 0;
resetQuestion()
}
else
{
q.text = questions1[shuffledLetters[qno]];
qno++;
}
}
function resetQuestion()
{
shuffledLetters = new Array(questions1.length);
for(var i=0;i<shuffledLetters.length;i++)
{
shuffledLetters[i] = i;
}
shuffledLetters.sort(randomize);
}
function randomize ( a : *, b : * ) : int {
return ( Math.random() > .5 ) ? 1 : -1;
}
resetQuestion()
change_question();
next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
change_question();
}
In above solution after showing all questions I have reset the questions automatically. you can modify code as per your requirement. If you want to do the reset questions you can put you code qno = 0;resetQuestion() on button click.
hope above solution work for you.

Resources