Topic: "Think" alarm clock (suggestion app)

This morning I was able to turn off my chumby alarm without even being mentally aware of doing so. This got me thinking that an alarm clock that asked you simple math calculations to turn off would be an excellent way of waking up and being able to stay up. Can this be done?

Re: "Think" alarm clock (suggestion app)

Yes, with a custom alarm ring screen - it requires some Flash expertise to create, though.

We put this easter egg in to allow people to create their own screens but there was no consensus what any particular screen should do.  Some people think the current screen is already too complicated for a drowsy person to manage, and others want a Mensa test.

Re: "Think" alarm clock (suggestion app)

Thanks you! Finally something I can use to learn flash.

However im running into problems (noob problems) can you help me get the script working.

So far I have 4 dynamic text fields, one for the first random number, another for + or - the third for the second random number and one for the input. I have 10 buttons (0-9) which will be the input and an enter button

the script so far is

function genrandom(min:Number, max:Number):Number {
    var number:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return number;
}
var firstnumber:Number = genrandom(0, 100);
var secondnumber:Number = genrandom(0, 100);
if (genrandom(1, 2) == 1) {
    this.addorsubtract = "+";
}
else {
    this.addorsubtract = "-";
}
this.Zerobutton.onRelease = function() {
    this.answer = 0;
} 

However both the + or - field (addorsubtract) or the zerobutton press is working. both of the fields are set and the right variable but I cant get it to work. can you help me out.
Ive tried var addorsubtract:string = "+'; and many other.

Re: "Think" alarm clock (suggestion app)

Hmmm, I don't think there's enough there to help you yet - can you provide the names of the dynamic text fields and buttons?

Re: "Think" alarm clock (suggestion app)

Oh sorry about being vague.

The button numbers are
Zeronumber
Onenumber
Twonumber
Threenumber
Fournumber
Fivenumber
Sixnumber
Sevennumber
Eightnumber
Ninenumber

The 4 dynamic boxes are
firstnumber -the first random number generated
addorsubtract -either + or - to add or subtract
secondnumber -the second random number
answer - the field where what you enter is displayed. IE if i hit Onenumber and Twonumber it would show 12

The enter button is just Enter

again the script is no where near complete or do I expect you do do it for me, im just trying to get ahold of the syntax and learn off of that.

Again thank you for your help

Re: "Think" alarm clock (suggestion app)

OK, some issues I see:

To put text in a TextFields, you should assign to the "text" property, ie  myTextField.text = "abc", not myTextField = "abc".  There is a deprecated method for setting text fields with just the variable name, but it's best not to use that any more.

In a button event handler, "this" is the button, not its container, so you probably would better do:

this.Zerobutton.onRelease = function() {
    this._parent.answer.text = "0";
}

Re: "Think" alarm clock (suggestion app)

Great! that did the trick
However the addorsubtract field is still not working. I tried to change the script so it uses

if (genrandom(1,2) == 1) {
   this._parent.addorsubtract.text = "+";
}
Else {
  this._parent.addorsubtract.text = "-";
}

but it still does not populate

Re: "Think" alarm clock (suggestion app)

You don't need the "_parent" there since that's not being done in a button handler.

Re: "Think" alarm clock (suggestion app)

Still nothing.

When I start the debuger, it shows ee0... for all the dynamic fields but when I hit run it goes blank

Re: "Think" alarm clock (suggestion app)

Here's what I'd do (untested):

function setupProblem():Void {
    this.firstnumber.text = random(100);
    this.secondnumber.text = random(100);
    this.addorsubtract.text = random(2)==0 ? "+" : "-" ;
    
    if (this.addorsubtract.text=='+') {
        this.result = int(this.firstnumber.text)+int(this.secondnumber.text);
    else {
        this.result = int(this.firstnumber.text)-int(this.secondnumber.text);
    }
    this.clearAnswer();
}

function setupButtons():Void {
    this.Zerobutton.onRelease = function() { this._parent.appendNumber('0'); }
    this.Onebutton.onRelease =  function() { this._parent.appendNumber('1'); }
    … etc…
    this.ClearButton.onRelease = function() { this._parent.clearAnswer(); }
}

function clearAnswer():Void {
    this.answer.text = '';
}

function appendNumber(c:String):Void {
    this.answer.text += c;
    checkAnswer();
}

function checkAnswer():Void {
    if (this.result==int(this.answer.text)) {
        // do the right thing, whatever that is
    }
}

setupProblem();
setupButtons();

Re: "Think" alarm clock (suggestion app)

Oh, wow! Thanks.

After countless hours of debugging and adding some tweaks i have this

/*var button:Array = [this.button_0.onRelease, this.button_1.onRelease, this.button_2.onRelease, this.button_3.onRelease, this.button_4.onRelease, this.button_5.onRelease, this.button_6.onRelease, this.button_7.onRelease, this.button_8.onRelease, this.button_9.onRelease];*/
function genrandom(min:Number, max:Number):Number
{
    var rand:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return rand;
}
function setup():Void
{
    var fir:Number = genrandom(0, 100);
    var sec:Number = genrandom(0, 100);
    var AoS:String = genrandom(1, 2) == 1 ? "+" : "-";
    if (AoS == "+")
    {
        this.resultt = (int(fir) + int(sec));
        this.firstnumber.text = fir;
        this.secondnumber.text = sec;
    }
    else
    {
        this.resultt = (int(fir) - int(sec));
        if (int(this.resultt) < 0)
        {
            this.resultt = (int(sec) - int(fir));
            this.firstnumber.text = sec;
            this.secondnumber.text = fir;
        }
        else
        {
            this.firstnumber.text = fir;
            this.secondnumber.text = sec;
        }
    }
    this.answer.text = "";
    this.AoSText.text = AoS;

}
function setupButtons():Void
{
    /*for(var i = 0; i < button.length; i++) {
    button[i] = function() {this._parent.appendNumber(i);}
    }*/
    this.button_0.onRelease = function()
    {
        this._parent.appendNumber('0');
    };
    this.button_1.onRelease = function()
    {
        this._parent.appendNumber('1');
    };
    this.button_2.onRelease = function()
    {
        this._parent.appendNumber('2');
    };
    this.button_3.onRelease = function()
    {
        this._parent.appendNumber('3');
    };
    this.button_4.onRelease = function()
    {
        this._parent.appendNumber('4');
    };
    this.button_5.onRelease = function()
    {
        this._parent.appendNumber('5');
    };
    this.button_6.onRelease = function()
    {
        this._parent.appendNumber('6');
    };
    this.button_7.onRelease = function()
    {
        this._parent.appendNumber('7');
    };
    this.button_8.onRelease = function()
    {
        this._parent.appendNumber('8');
    };
    this.button_9.onRelease = function()
    {
        this._parent.appendNumber('9');
    };
    this.Enter.onRelease = function()
    {
        this._parent.checkanswer();
    };
    this.Sleep.onRelease = function()
    {
        this._parent.sleep();
    };
}
function appendNumber(c:String):Void
{
    this.answer.text += c;
}
function checkanswer():Void
{
    if (int(resultt) == int(this.answer.text))
    {
        function() {this._parent._chumby_alarm.stopAlarm();}
    }
    else
    {
        this.answer.text = "";
    }
}
function sleep():Void
{
    function() {this._parent._chumby_alarm.snoozeAlarm();}
}
setup();
setupButtons();

I did change many of the button names around but everything is working EXCEPT turning off the alarm or even snoozing it. debug says it is getting to the right spot and the button is being clicked but nothing.

Re: "Think" alarm clock (suggestion app)

Well, all this:

    function() {this._parent._chumby_alarm.snoozeAlarm();}

does is *declare* a function.

Change to simply

this._chumby_alarm.stopAlarm();

This assumes that all of this is on the main timeline of the movie.

Re: "Think" alarm clock (suggestion app)

That was it! I tried without the function to start with but kept the _parent part it.

Thank you so much for your time! I could not be happier now smile