I'm making a game in AS3.
I was wondering if it's possible to count how many times a function is called and do something when it has been called 5 times for exemple. (and then it's restarting to count at 0).
If we take an exemple, what would it be ?
Somethink like :
movieClip.addEventListener(MouseEvent.CLICK, functionExemple, false, 0, true);
function functionExemple(e:MouseEvent):void{
//do something;
count 1;
if (count = 5){
doThat();
count = 0;
}
So the function doThat would be called every 5 times.
I know the code is wrong. It's just in order to explain as good as possible what I meant.
Thank you for your help,
There are some errors in your example.
var count:int=0; //variable declaration
function functionExemple(e:MouseEvent):void{
//do something;
count++;//<=>count 1;
if (count == 5){//<=>if (count = 5){
doThat();
count = 0;
}
}
My initial guess would be to set a global variable that acts as a counter then when the function is called - increment its value. When value hits 5 take action. I would also guess using some form of "getter" to get the counter value would also help. This is my initial stab at it.
Related
I am trying to count the number of rows in an advanceddatagrid
I need a function which could count all item with or without filterFunction.
I tried some solution but none works. The best that I found, is to expand all items and use a cursor for looping.
But, when we have a lot of data, expanding all is not a good solution.
Do you have an idea on how to do that ?
Thank you
The only way I came up was investigating dataProvider
// current not expanded data row lenght
grid.dataProvider.lenght;
// expanded length
// I assume you use xml as your data provider
// then you can count it like this
xmlListTotalSize(new XMLList(grid.dataProvider.source.source));
// or with casts
xmlListTotalSize(new XMLList((IHierarchicalCollectionView(view.grid.dataProvider).source as HierarchicalData).source));
and xmllist traversal function might look something like this:
private static function xmlListTotalSize(x:XMLList):int
{
var i:int = x.length();
for each(var xChild:XML in x.children())
i += xmlListTotalSize(xChild.children());
return i;
}
I am using Zend fetch method to fetch huge number of records from database for creating reports.Since fetchAll is costly as compared to fetch i am using it.And if its the last row i need to add some additinoal logic.So my question is that is there a way to check if next record exists or not inside the while loop. I am using it like the following
//$select is the select query
$objDb = Zend_Registry::get('db');
$objAchQry = $objDb->query($select);
while($arrResult = $objAchQry->fetch()) {
//Do something
//I need to do something here if its the last record like
/*
if($last_rec)
do something
*/
}
Is there a way to check if the current one is last record or if any other record exists. I know to do it by taking count of records and incrementing a counter inside the loop.But i dont need it.Any solutions.?
"is there a way to check if next record exists" The condition on your while loop does exactly just that. The loop won't execute any more if no more rows exist to fetch.
Think of it this way:
while($arrResult = $objAchQry->fetch()) {
//Do something
}
// Now I'm just after the last record
/*
do something
*/
If you really need to do something before the last row is processed, you could modify your code to
$total_records = // get total no of rows
$counter = 0;
while($arrResult = $objAchQry->current()) {
//Do something
$counter ++;
//I need to do something here if its the last record like
if( ! $bojAchQry->next()) {
// The row currently being processed is the last one.
} else {
break;
}
}
One way - You can track with counter,
$total_records = // get total no of rows
$counter = 0;
while($arrResult = $objAchQry->fetch()) {
//Do something
$counter ++;
//I need to do something here if its the last record like
if($counter == $total_records ) // this iteration will be the last one.
//do something
}
I have a list of cases in the queue that I need to grab. As you can imagine, it's a bit repetitive and time consuming. I'm new at programming and haven't figured a way to create a script that auto-click/grab these cases. Can someone help?
Code to:
1) Search and Click "Grab"
- will take 4 seconds for the page to refresh
2) Click grab again
3) stop after 50 cases are grabbed
This code doesn't work
window.setTimeout("pushSubmit()",3000);
function pushSubmit()
{document.getElementById('Grab').click();
Assuming your page is not refreshed in the process, you could keep a counter of how many "Grabs" you have done:
var counter = 0;
var maxCount = 50;
function pushSubmit() {
if(counter++ < maxCount) {
document.getElementById('Grab').click();
window.setTimeout(pushSubmit,3000);
}
}
//start the process
pushSubmit();
Here is a jsfiddle example
EDIT:
Or what I would probably prefer, set up the function so it can be used with any number of iterations.
function pushSubmit(max, count) {
count = typeof count !== 'undefined' ? count : 1;
if(count <= max) {
document.getElementById('Grab').click();
window.setTimeout(function() { pushSubmit(max, ++count) },3000);
}
}
//start the process with the max number of iterations it should perform
pushSubmit(50);
Example
I'm working on a match-3 style puzzle game using Flixel, and so I'm working on checking each row and column to see if there is a match at any given time. However, I have 6 different pieces (as of right now) that are active, and each piece is identified by an integer. Given that, I can check, for each and every single piece, by doing something like this:
public function matchingCheck():void
{
if (piecesArray[0][1] == 1 && piecesArray[1][1] == 1 && piecesArray[2][1] == 1) {
FlxG.log("Yay!");
}
}
However, this is rather unwieldy and would basically cause way too much repetition for my liking.
At the very least, I would like to be able to check if the values in these arrays are equal to one another, without having to specify which value it is. At the very best, I'd love to be able to check an entire row for three (or more) adjacent pieces, but I will settle for doing that part manually.
Thanks for your help!
EDIT: Nevermind, my edit didn't work. It was just checking if piecesArray[2][1] == 1, which makes me a sad panda.
EDIT 2: I've selected the correct answer below - it's not exactly what I used, but it definitely got me started. Thanks Apocalyptic0n3!
You could cut down on that code a little bit by using another function
private function checkValid( arrayOfItemsToCheck:Array, value:* ):Boolean {
for ( var i:Number = 0; i < arrayOfItemsToCheck.length; i++ ) {
if ( arrayOfItemsToCheck[i] != value ) {
return false;
}
}
return true;
}
Then you just do this in your if statement:
if ( checkValid( [ piecesArray[0][1], piecesArray[1][1], piecesArray[2][1] ], 1 ) ) {
FlxG.log("Yay!");
}
That does assume all items need to be equal to 1, though. It's still a lot of code, but it cuts out one set of "= 1 &&" for each check.
How about something like this which would tell you both if a match existed and what match it was:
public function checkForMatch():void{
var rows:int = piecesArray.length;
for(var i:int=0; i<rows; i++){
var match:int = checkRow(piecesArray[i]);
if(match > -1) {
FlxG.log("Yay you matched " + match);
}
}
}
private function ckeckRow(row:Array):int{
if(row[0] == row[1] == row[2]){
return row[0];
}
return -1;
}
I am trying to sort big array using actionscript 3.
The problem is that i have to use custom sorting function which is painfully slow and leads to flash plugin crash.
Below is a sample code for custom function used to sort array by length of its members:
private function sortByLength():int {
var x:int = arguments[0].length;
var y:int = arguments[1].length;
if (x > y){
return 1;
}else if (x < y){
return -1;
}else{
return 0;
}
}
Which is called like this:
var txt:Array = ["abcde","ab","abc","a"];
txt.sort(sortByLength);
Please advise me how can this be done faster ?
How to change application logic to avoid Flash plugin crashes during sorting ?
try to use strong typing whenever possible, here tell your function that you are waiting two strings.
you could rewrite your function in two way one fastest than the other if you know that all your element are not null:
function sortByLength(a:String, b:String):int {
return a.length-b.length // fastest way not comparison
}
and if you can have null check for it (this one will put null in front of all element):
function sortByLengthWithNull(a:String, b:String):int {
if (a==null) return -1
if (b==null) return 1
return a.length-b.length
}
If you need super-fast sorting, then it might be worthwhile not using an array at all and instead using a linked-list. There are different advantages to each. Primarily, with a linked-list, index-access is slow, while iterating through the list is fast, and linked-lists are not native to AS3 so you'll have to roll your own.
On the upside, you may well be able to use some of Polygonal Labs' code: http://lab.polygonal.de/as3ds/.
Sorting is very, very fast for nearly-sorted data with a linked list, as this article discusses: http://lab.polygonal.de/2007/11/26/data-structures-more-on-linked-lists/.
This solution gives you lots more work, but will eventually give you lots more sort-speed too.
Hope this helps.
-- additional --
I noticed your question in the comments of another answer about "One question however is unanswered - how to perform greedy computations in Flash without hanging it?"
For this, essentially the answer is to break your computation over multiple frames, something like this:
public function sort():void
{
addEventListener(Event.ENTER_FRAME, iterateSort);
}
private function iterateSort():void
{
var time:int = getTimer() + TARGET_MILLISECONDS_PER_FRAME;
var isFinished:Boolean = false;
while (!isFinished && getTimer() < time)
isFinished = continueSort();
if (isFinished)
removeEventListener(Event.ENTER_FRAME, iterateSort);
}
function continueSort():Boolean
{
... implement an 'atom of sort' here, whatever that means ...
}
sortByLength should have two parameters, shouldn't it? I guess that's what you mean by the arguments array...
This looks fine to me, unless arguments is not a local variable, but instead a member variable, and you're just looking at its [0] and [1] elements on each function call. That would at least produce undesired results.