Javascript loop with event action listeners and buttons - titanium

I am trying to write an app that has a multiple choice quiz in it. I am writing it in a simple and somewhat hardcoded way. I have created an array of questions and a 2-d array of answers as my "database". My problem is that when i am iterating over the loop, my app immediately goes to the last question, even though if statements that in an ideal world should let the user interact with every questions.
my while loop is
var i = 0;
while i<10 then
make the question view
make the answer view
make the answers clickable
calculate scoring
if the next button is pushed and i < 8 then i+=1
/*this prevents the app from building but when i put the i+=1 outside this control statement it goes directly to the last question in my database*/
end While
any ideas? my code is really long and do not know if i should post it

Rather than doing it all in a while loop, you should take a slightly different approach.
Create a function that does the while-loop-block above, and use a variable to keep track of the currently displayed question and answer. Then when the user clicks next, advance to the next pair, until the user is done.
var current = 0, until = 10;
function showCurrent() {
// make the question view
// make the answer view
// make the answers clickable
// calculate scoring
}
function goToNext() {
current += 1;
if (current === until) {
// carry on with whatever is next
}
else {
showCurrent();
}
}
showCurrent();

Related

Go To Definition - getting back again

I am looking for a simple means of jumping back from a Go To Definition action, which goes directly back to the point from which I made the GTD jump.
The problem with the current workbench.action.navigateBack feature is that it revisits other locations over which I have moved the cursor (after making the GTD jump) before returning me to the point where I started. For instance:
public takeoff() {
this.landing();
}
public landing() {
let x = 1;
let y = 2;
...
...
}
If I choose Go To Definition to move from takeoff() to landing() and then move the cursor up and down the lines of code in landing(), when I issue the workbench.action.navigateBack I simply want to return to takeoff(), but instead I have to revisit various lines within landing() along the way. Aaargh!
Do you know how to achieve the functionality which I am looking for?

ngui dynamic text advice (from a noob)

Sorry in advance, this is an extremely noobie question (but i'm just getting into NGUI with unityscript and can't find many answers/tutorials/docs).. Also my untiyscript skills are sub-par.
I have a TCG/Playing card game object with some basic RPG stats (strength, dexterity) that currently display on the card in GUIlabel and trying to convert this to NGUI. I'm adding a UILabel as a child to the card (which contains the stats script)
Looking for some advice on going about this, the only way I've even remotely gotten something to display correctly is, unfortunately I have to attach the stats script to the label too:
var strLbl : UILabel;
function Start() {
var strLbl = GetComponent(UILabel);
}
function OnGUI() {
strLbl.text = strength.ToString();
}
This is throwing numberous 'nullreferenceexception: object reference not set to an instance of an object (for the stats script)
Do I need to make a separate label for each stat or is there a way
to aggregate it into one label? (seems when I try to add strength
,then dexterity it overrides it)
is OnGUI the correct course for NGUI or is there a more efficient
function?
Is this script attached to the object that the UILabel is on? You should do a check for
if(strLbl != null)
strLbl.text = strength.ToString();
You could aggregate them into one label (though if individual stats update I would advise against it), assuming you want each stat on a newline then your next would be: strLbl.text += "\n" + dexterity.ToString()
No need to use OnGUI with NGUI. Especially not for setting things. You probably want to do this whole stage in Start() and have another method called for updating the label.

Flash as2 bullet removing enemy?

I am trying to make a simple as2 shooter but when I try to shoot an enemy the bullet just go thru it and doesn't remove the enemy. I tried to put
if (hitTest(_root.vihollinen)==true
){
_root.vihollinen.remove();
this.removeMovieClip();
}
but nothing happens
Most of the code is just copy/paste because I don't know much about coding but I'm trying to learn!
https://www.dropbox.com/s/58u34tbeve6oile/game.zip
The most significant issue is that your enemy needs an Instance Name in order for the code to understand it. Simply click on your enemy movieclip, and add vihollinen to the Instance Name field at the top.
Next, your bullet code is close, but needs some adjustments. Here is your code:
_root["bullet" + bulletsFired].onEnterFrame = function(){
this._x += this.xmov;
this._y += this.ymov;
if (hitTest(_root.vihollinen)==true){
_root.vihollinen.remove();
this.removeMovieClip();
}
};
If you're ever using an onEnterFrame handler like that with a function, it's good practice to always refer to the current object with this, like so: if(this.hitTest(_root.vihollinen) == true){
remove() is not an ActionScript2 function. Try using unloadMovie() instead, like this: _root.vihollinen.unloadMovie()
Changing those three things will make your code function. Be sure that you try to go through each part of your code and understand it to the best of your ability - it'll make things much easier in the long run, even if it takes a long time to figure out why each part is there!

AutoHotKey Global Variable that can be accessed and modified by different macros?

I've seen a similar topic on sof but its solution did not help me. This is ticking my mind and basically all i want is to have some method of accessing and modifying a value that will maintain its last changed state through out my macros in my single .ahk file.
See example below ,
~Home::Suspend
XButton1::
tog()
return
LButton::
shot()
return
var := "1"
tog(){
var *= -1
}
shot(){
If (var = "1") {
Loop, 1 {
Send {k}
Sleep 65
Send {WheelDown}
Sleep 100
Send {WheelUP}
Sleep 10
}
} Else {
Send {k}
}
}
I am aware that the above is incorrect, and i tried to use"global" in my functions but i just couldn't get my desired effect.
Using the "global" should work. Something like:
shot(){
global var
If (var = "1") {
That points the 'var' variable in the shot() function to the existing 'var' variable defined outside the function.
I had the same issue and after some trial and error I found my mistake which is the same as in the provided code:
The correct way to declare a global is before other functions
var := "1"
XButton1::
; code...
return
The code in the OPs script will hit the return first and never declare the variable
XButton1::
; code...
return ; Returns Here
var := "1" ; Incorrect Will Not Be Declared
I just wanted to provide this as an answer because while I did see this information in one of the comments, I didn't see it until after I'd already spent an additional hour figuring it out myself. As this is the answer I needed, having it as an actual prominent answer may help someone else save time.
What I did, especially since I sometimes have multiple scripts running that need to access the same variable, is to place the var in a .ini file. I also use this to preserve the variable value after a restart. The solution is somewhat slower since the data is saved to the hard disk / SSD, but it works beautifully.
Example of writing the value "S" to variable "State" in group "Finish"
IniWrite, S, C:\2Podcasts\FinishOptions.ini, Finish, State
In an other script (other AutoHotKey instance), I read the value and assign it to the variable "FinishOption".
IniRead, FinishOption, C:\2Podcasts\FinishOptions.ini, Finish, State
If you want to toggle values (True/False), you could use this. This will do an IF on the current value of the variable AND set the variable to the opposite value.
If (MyLoop := !MyLoop) ; Toggle the variable "MyLoop" True/False
{
Do something
}
Else
{
Do something else
}
return

Advice how to present calculation of #records

I have an app that have a lot of records that i am loading into a core data DB. The actual loading is done in a module called "loadDBRecords". This module is called from "MainViewController", which is connected to "MainViewController.xib".
As the loading takes quite some time i have added an UIActivityIndicatorView to the .xib during the load. However, i would like to use a UIProgressView. When i look at this i do not see any way of using that unless i move the loading code from "loadDBRecords" to the "MainViewController".
My question is: What is the best way of leveraging the UIProgressView with my setup?
You could add a "progressHandler" block to the -loadDBRecords method. Execute the block for every record you load, and pass back a floating-point value between 0.0 and 1.0 indicating the progress thus far. The block can then assign that value to the progress bar, or print it to the console, or whatever you need it to do. (Traditionally, this would be done with a function pointer or a delegate/selector combination, but I've found blocks tend to make your intent clearer—or at least make it easier to show your intent.)
- (void)loadDBRecords:(void (^)(CGFloat progress))progressHandler {
NSUInteger count = /* number of records*/;
for (NSUInteger i = 0; i < count; i++) {
/* load the record */
progressHandler((CGFloat)n / (CGFloat)count);
}
}