CountTime in AS3 - actionscript-3

I have received the same error after I tested the timed game:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/countTime()
function countTime(e: Event): void {
if (stopwatch.currentFrame == 61) {
seconds++;
speedSeconds++;
if (seconds > 59 && speedSeconds > 59) {
seconds = 0;
speedSeconds = 0;
speedtimerSec_txt.text = "0" + speedSeconds;
timerSec_txt.text = "0" + seconds;
minutes++;
speedMinutes++;
if (minutes > 10 && speedMinutes > 10) {
speedtimerMin_txt.text = "" + speedMinutes;
timerMin_txt.text = "" + minutes;
}
else {
speedtimerMin_txt.text = "0" + speedMinutes;
timerMin_txt.text = "0" + minutes;
}
if (minutes > 59 && speedMinutes > 59) {
stopwatch.stop();
gotoAndPlay("gameover_Timed");
}
}
else {
if (seconds >= 10 && speedSeconds >= 10) {
speedtimerSec_txt.text = "" + speedSeconds;
timerSec_txt.text = "" + seconds;
}
else {
speedtimerSec_txt.text = "0" + speedSeconds;
timerSec_txt.text = "0" + seconds;
}
}
}
}
Can you check this code to this problem?

Related

kotlin fun with string concatenation

I need help concatenating a string in a kotlin viewmodel. I am trying to return what the code should print to the status text view. The formula determines from a Int what the status should be then outputs a message to the User. I am unsure as to what method should be used to create a string that I wanted to return.
fun formula(f: Int, b: Int, l: Int, d: Int) : String {
if (f > 70 && f < 99) {
val ftxt = "Normal"
} else if (f < 71) {
val ftxt = "Hypoglycemic"
}
if (b > 140) {
val btxt = "Abnormal"
} else if (b < 71) {
val btxt = "Hypoglycemic"
}
if (l > 140) {
val ltxt = "Abnormal"
} else if (l < 71) {
val ltxt = "Hypoglycemic"
}
if (d > 140) {
val dtxt = "Abnormal"
}else if (d < 71) {
val dtxt = "Hypoglycemic"
}
else {
val ftxt = "Abnormal"
val btxt = "Normal"
val ltxt = "Normal"
val dtxt = "Normal"
}
var status = calendar.toString()
status += "\n Fasting: "
status += ftxt + "\n Breakfast: "
status += btxt + "\n Lunch: "
status += ltxt + "\n Dinner: " + dtxt
return status
}
Without entering into detail on how you could improve your function you're having the "red squiggles" because those val are declared within the scope of the if-else blocks.
You can declare them out of the block and assign values:
fun formula(f: Int, b: Int, l: Int, d: Int) : String {
var ftxt = ""
var btxt = ""
var ltxt = ""
var dtxt = ""
if (f > 70 && f < 99) {
ftxt = "Normal"
} else if (f < 71) {
ftxt = "Hypoglycemic"
}
if (b > 140) {
btxt = "Abnormal"
} else if (b < 71) {
btxt = "Hypoglycemic"
}
if (l > 140) {
ltxt = "Abnormal"
} else if (l < 71) {
ltxt = "Hypoglycemic"
}
if (d > 140) {
dtxt = "Abnormal"
}else if (d < 71) {
dtxt = "Hypoglycemic"
}
else {
ftxt = "Abnormal"
btxt = "Normal"
ltxt = "Normal"
dtxt = "Normal"
}
var status = calendar.toString()
status += "\n Fasting: "
status += ftxt + "\n Breakfast: "
status += btxt + "\n Lunch: "
status += ltxt + "\n Dinner: " + dtxt
return status
}

Actionscripts 3 Function doesn't work

I write some code for getting current date and compare it with a future date for application limitation. I don't know why this function doesn't work.
getYYMMDD();
function getYYMMDD(): String {
var dateObj: Date = new Date();
var year: String = String(dateObj.getFullYear());
var month: String = String(dateObj.getMonth() + 1);
if (month.length == 1) {
month = "0" + month;
}
var date: String = String(dateObj.getDate());
if (date.length == 1) {
date = "0" + date;
}
return year.substring(0, 4) + month + date;
trace(year + ":" + month + ":" + date);
if (int(year) > 2017 && int(month) > 5 && int(date) > 31) {
trace("SYSTEM TIME IS OFF.");
} else {
trace("SYSTEM TIME IS ON.");
}
}
(1) Since your function returns data of String type...
function getYYMMDD(): String
Make sure that returned data is also being received by a String... ie: someString = getYYMMDD(); means someString now has returned value from function.
(2) You return (exit the function) too soon...
Put return as last command to allow all other code inside your function to run.
(3) You should consider returning a Boolean type (true/false)...
var can_Start : Boolean = false; //# assume false before checking
can_Start = getYYMMDD(); //# use function to update status to true/false
if (can_Start == true) { run_Program(); }
else { trace("Sorry time has expired"); }
function getYYMMDD(): Boolean
{
var dateObj: Date = new Date();
var year: String = String(dateObj.getFullYear());
var month: String = String(dateObj.getMonth() + 1);
if (month.length == 1) { month = "0" + month; }
var date: String = String(dateObj.getDate());
if (date.length == 1) { date = "0" + date; }
trace(year + ":" + month + ":" + date);
if(int(year) == 2017)
{
if(int(month) >= 05 && int(date) > 31)
{ trace("SYSTEM TIME IS OFF."); can_Start = false; } //# can_Start == false;
else { trace("SYSTEM TIME IS ON."); can_Start = true; } //# can_Start == true;
}
return can_Start;
}

Function not functioning properly

I have a question about my function.
This is my function :
function checkKind()
{
var i:Number = new Number;
for (i=0 ; i<9 ; i++)
{
if (pValue[i] >= 1 && pValue[i] <= 13)
{
pKind[i] = 1;
}
else if (pValue[i] >= 14 && pValue[i] <= 26)
{
pKind[i] = 2;
}
else if (pValue[i] >= 27 && pValue[i] <= 39)
{
pKind[i] = 3;
}
else if (pValue[i] >= 40 && pValue[i] <= 52)
{
pKind[i] = 4;
}
}
}
pKind[i] is always filled up with number 1, so pKind[i] = 1.
I've already checked the pValue[i] which is properly filled.
Can someone help me to see what is wrong in my code ?

how to convert seconds to minutes:seconds in as3

hi to all i am new in action script 3 in flash cs6 im creating a game with a timer
i want to make the seconds to minutes:seconds format
example:
120 seconds to 2:00
this is my code:
var countDownDec:Number = 1;
var totalSecs = 120;
var countDownSecs = totalSecs;
counter.text = countDownSecs;
var time:Timer = new Timer(countDownDec*1000);
time.addEventListener(TimerEvent.TIMER, tick);
time.reset();
countDownSecs = totalSecs;
counter.text = countDownSecs;
time.start();
var frameLbl:FrameLabel;
function tick(e:TimerEvent):void {
time.start();
if(countDownSecs == 0){
time.stop();
countDownSecs = totalSecs;
gotoAndPlay('timesUp');
TimesUp.play();
}
else{
countDownSecs = countDownSecs - countDownDec;
counter.text = countDownSecs;
}
}
please help me to my problem
Code 100% working :
var count_down_interval:Number = 1
var total_seconds:Number = 120
var count_down_seconds:Number = total_seconds
var timer:Timer = new Timer(count_down_interval * 1000)
timer.addEventListener(TimerEvent.TIMER, timer_on_Tick)
function timer_on_Tick(e:TimerEvent):void {
if(count_down_seconds == 0){
timer.stop()
count_down_seconds = total_seconds
trace('game over')
} else{
count_down_seconds -= count_down_interval
counter.text = convert_time(count_down_seconds)
}
}
timer.start();
counter.text = convert_time(count_down_seconds)
function convert_time(time) {
var h, m, s:Number, t:String, a:Array
if(isNaN(time)){ // 05:37 -> 337 seconds
t = time
a = t.split(':')
if(a.length > 2){
h = int(a[0]), m = int(a[1]), s = int(a[2])
} else {
h = 0, m = int(a[0]), s = int(a[1])
}
return h*3600 + m*60 + s
} else { // 337 -> 05:37
t = ''
h = int(time/3600)
m = int((time-(h*3600))/60)
if(time >= 3600) t += (h<10 ? '0' : '') + h + ':'
t += (m<10 ? '0' : '') + m + ':'
t += (time % 60<10 ? '0' : '') + int(time % 60)
return t
}
}
Divide by 60 and round down for minutes, modulo (%) 60 for seconds
var totalSecs = 120;
var minutes = Math.floor(totalSecs / 60);
var seconds = totalSecs % 60;
var timeInMinutesSeconds = minutes + ":" + seconds;
EDIT:
Try this:
var countDownDec:Number = 1;
var totalSecs:int = 120;
var countDownSecs:int = totalSecs;
counter.text = getCountDownSecsText(countDownSecs);
var time:Timer = new Timer(countDownDec*1000);
time.addEventListener(TimerEvent.TIMER, tick);
var frameLbl:FrameLabel;
function tick(e:TimerEvent):void {
if(countDownSecs == 0) {
time.stop();
countDownSecs = totalSecs;
gotoAndPlay('timesUp');
TimesUp.play();
} else {
countDownSecs = countDownSecs - countDownDec;
counter.text = getCountDownSecsText(countDownSecs);
}
}
private function getCountDownSecsText(currentAmountSecs:int):String{
var minutes:int = Math.floor(currentAmountSecs / 60);
var seconds:int = currentAmountSecs % 60;
var prependString:String = "";
if( minutes > 9 ) {
prependString = "0";
}
return prependString + minutes + ":" + seconds;
}

How to export (dump) WebSQL data

I'm working on a Chrome Extension that uses WebSQL to store historical data.
Being WebSQL, the DB is stored on the client.
I'd like to add an option to export/import such data so that the user can share/use this data with other users, or with other PCs.
These are my first steps on a client-only database, so I wonder how to do this.
I was thinking to convert the DB to a huge json string that the user can copy/paste but doesn't look very user-friendly.
Is there any better solution?
I got a single table dump solution working on a HTML5 database client I wrote a few days ago.
Check out http://html5db.desalasworks.com/script.js and scroll down to SqlClient.exportTable, this has an example that needs to be expanded to cover the whole database.
The steps are:
Step 1: Create the schema:
SELECT sql FROM sqlite_master
Step 2: Get a list of tables:
SELECT tbl_name from sqlite_master WHERE type = 'table'
Step 3: Loop through each of them and create an INSERT script with the results
transaction.executeSql("SELECT * FROM " + _tbl_name + ";", [],
function(transaction, results) {
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
var row = results.rows.item(i);
var _fields = [];
var _values = [];
for (col in row) {
_fields.push(col);
_values.push('"' + row[col] + '"');
}
_exportSql += ";\nINSERT INTO " + _tbl_name + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ")";
}
}
}
);
Hope this is useful.
UPDATE JAN 2016 - WHOLE DB EXPORT
I've got a JS websqldump library that you can download from github as well.
This one will export the whole database. Check out the code on:
https://github.com/sdesalas/websqldump
Usage as follows
websqldump.export({
database: 'NorthwindLite',
success: function(sql) {alert(sql);}
});
Not the most elegant way, yet most convenient.
Just paste the script in chrome debugger tools then call c(), and you should get the file.
var looongSQL = "";
var x = function (options) {
if (options.n < options.sqlTables.length) {
onTheMove.openLocalDatabase().transaction(
function (tx) {
var sqlStatement = "SELECT * FROM " + options.sqlTables[options.n];
tx.executeSql(sqlStatement, [],
function (tx, rslt) {
if (rslt.rows) {
for (var m = 0; m < rslt.rows.length; m++) {
var dataRow = rslt.rows.item(m);
var _fields = [];
var _values = [];
for (col in dataRow) {
_fields.push(col);
_values.push('"' + dataRow[col] + '"');
}
looongSQL += "INSERT INTO " + options.sqlTables[options.n] + "(" + _fields.join(",") + ") VALUES (" + _values.join(",") + ");\n";
}
}
options.n++;
x(options);
}
);
});
}else
{
document.location = 'data:Application/octet-stream,' +
encodeURIComponent(looongSQL);
}
};
var c = function () {
onTheMove.openLocalDatabase().transaction(
function (transaction) {
transaction.executeSql("SELECT sql FROM sqlite_master;", [],
function (transaction, results) {
var sqlStatements = [];
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
console.log(results.rows.item(i));
var row = results.rows.item(i);
if (row.sql != null && row.sql.indexOf("CREATE TABLE ") != -1 && row.sql.indexOf("__") == -1) {
var tableName = row.sql.replace("CREATE TABLE ", "").split(/ |\(/)[0];
sqlStatements.push('DROP TABLE IF EXISTS ' + tableName);
}if(row.sql != null && row.sql.indexOf("__") == -1){
sqlStatements.push(row.sql);}
}
}
for (var j = 0; j < sqlStatements.length; j++) {
if (sqlStatements[j] != null) {
looongSQL += sqlStatements[j] + ';\r\n';
}
}
transaction.executeSql("SELECT tbl_name from sqlite_master WHERE type = 'table'", [],
function (transaction, res) {
var sqlTables = [];
for (var k = 0; k < res.rows.length; k++) {
if (res.rows.item(k).tbl_name.indexOf("__") == -1) {
sqlTables.push(res.rows.item(k).tbl_name);
}
}
x({
sqlTables: sqlTables,
n: 0
});
});
}
);
});
};
Another version that exports it as JSON
var looongSQL = "[\n";
var stringidiedLocalStorage = JSON.stringify(JSON.stringify(localStorage));
looongSQL += "/* 1 */ " + stringidiedLocalStorage + ",\n";
var x = function (options) {
if (options.n < options.sqlTables.length) {
onTheMove.openLocalDatabase().transaction(
function (tx) {
var sqlStatement = "SELECT * FROM " + options.sqlTables[options.n];
tx.executeSql(sqlStatement, [],
function (tx, rslt) {
if (rslt.rows && rslt.rows.length > 0) {
var _fields = [];
for (var col in rslt.rows.item(0)) {
_fields.push(col);
}
var insertTableSQL = "\"INSERT INTO " + options.sqlTables[options.n] + "(" + _fields.join(",") + ") ";
looongSQL += "/* " + options.count + " */ " + insertTableSQL;
for (var m = 0; m < rslt.rows.length; m++) {
var dataRow = rslt.rows.item(m);
var _values = [];
for (var col in dataRow) {
_values.push('\'' + dataRow[col] + '\'');
}
looongSQL += "SELECT " + _values.join(",");
if (m < rslt.rows.length - 1 && (m % 499 != 0 || m == 0)) {
looongSQL += " UNION ALL ";
}
if (m % 499 == 0 && m != 0) {
options.count++;
looongSQL += "\",\r\n/* " + options.count + " */ " + insertTableSQL;
}
}
looongSQL += "\",\r\n";
options.count++;
}
options.n++;
x(options);
}
);
});
} else {
looongSQL += ']';
document.location = 'data:Application/octet-stream,' +
encodeURIComponent(looongSQL);
}
};
var c = function () {
onTheMove.openLocalDatabase().transaction(
function (transaction) {
transaction.executeSql("SELECT sql FROM sqlite_master;", [],
function (transaction, results) {
var sqlStatements = [];
var count = 2;
if (results.rows) {
for (var i = 0; i < results.rows.length; i++) {
console.log(results.rows.item(i));
var row = results.rows.item(i);
if (row.sql != null && row.sql.indexOf("CREATE ") != -1) {
var objectType = row.sql.replace("CREATE ", "").split(/ |\(/)[0];
if (row.sql.indexOf("CREATE " + objectType + " ") != -1 && row.sql.indexOf("__") == -1) {
var objectName = row.sql.replace("CREATE " + objectType + " ", "").split(/ |\(/)[0];
sqlStatements.push('/* ' + count + ' */ "DROP ' + objectType + ' IF EXISTS ' + objectName + '"');
count++;
}
if (row.sql != null && row.sql.indexOf("__") == -1) {
sqlStatements.push('/* ' + count + ' */ "' + row.sql.replace(/(\r\n|\n|\r)/gm, " ") + '"');
count++;
}
}
}
}
for (var j = 0; j < sqlStatements.length; j++) {
if (sqlStatements[j] != null) {
looongSQL += sqlStatements[j] + ',\r\n';
}
}
transaction.executeSql("SELECT tbl_name from sqlite_master WHERE type = 'table'", [],
function (transaction, res) {
var sqlTables = [];
for (var k = 0; k < res.rows.length; k++) {
if (res.rows.item(k).tbl_name.indexOf("__") == -1) {
sqlTables.push(res.rows.item(k).tbl_name);
}
}
x({
sqlTables: sqlTables,
n: 0,
count: count
});
});
}
);
});
};