For each loop not looping through everything - actionscript-3

I have a loop that runs in an onEnter function (bad i know). I only want it to run once but it doesnt loop through every object. I think it doesnt have time before the next frame runs. Any ideas on how to make it loop all the way?
private function onEnter(e: Event) {
if (deleteThis == true) {
if (timer == 0 || 1) {
if (checkExplosion == false) {
gotoAndStop(2)
if (Main.bloonList.length > 0) {
for each(Main.bloon in Main.bloonList) {
if (Main.areaOfCollision(Main.bloon, this, 0)) {
if (Main.bloon.currentFrame != 5) {
Main.money++;
Main.bloon.nextFrame();
Main.bloon.gotShot();
} else {
Main.money++;
Main.bloon.deleteBloon();
}
}
}
}
checkExplosion=true
}
}
}
}
Edit - deleteBloon();
public function deleteBloon()
{
this.parent.removeChild(this);
}

Firstly, this:
if (timer == 0 || 1)
Should be this:
if (timer == 0 || timer == 1)
Then you may need to change the code a little bit. Something like this could work. I do suspect your issue may actually lie in the deleteBloon function, though. Without seeing it I can't be sure, but there is nothing (technically) wrong with your for loop.
private function onEnter(e: Event) {
if (deleteThis == true) {
if (timer == 0 || timer == 1) {
if (checkExplosion == false) {
gotoAndStop(2);
if (Main.bloonList.length > 0) {
// This isn't optimised as I wasn't sure what Main.bloon could be
for each(var bloon:Main.bloon in Main.bloonList) {
if (Main.areaOfCollision(bloon, this, 0)) {
if (bloon.parent && bloon.currentFrame != 5) {
bloon.nextFrame();
bloon.gotShot();
} /*else {
// Make sure that deleteBloon is deleteing the correct array index. This is likely where the issue is.
bloon.deleteBloon();
}*/
Main.money++;
}
}
}
checkExplosion = true;
}
}
}
}
This would be in bloon:
import flash.utils.setTimeout;
function gotShot():void{
// second parameter is time in milliseconds before executing
setTimeout(deleteBloon, 250);
}

Related

Compare answer from user and possible answer in array

My input text field has two possible answers which are "luah" or "hal" but my code is not working :
stop();
//var jawapan1=Array;
txt_zuhal.addEventListener(KeyboardEvent.KEY_DOWN,handler);
function handler(event:KeyboardEvent)
{
//jawapan1=("luah", "hal");
// if the key is ENTER
if(event.charCode == 13)
{
if(txt_zuhal.text == 'luah'||'hal')
{
trace("1.correct");
}
else
{
trace("1.Sorry, Wrong answer");
}
}
}
private function handler(event:KeyboardEvent):void
{
//jawapan1=("luah", "hal");
// if the key is ENTER
if(event.charCode == 13)
{
if(txt_zuhal.text == "luah" || txt_zuhal.text == "hal")
{
trace("1.correct");
}
else
{
trace("1.Sorry, Wrong answer");
}
}
}

How to get exact data after applying filter on data provider?

I am having N-Level hierarchical data.
It is the data about Products inside N-Level of Product categories.
After applying filter on the data, I want only that much data which satisfies the filter condition because I need to display counts after applying filter.
But the problem is I am not able to get such data from anywhere.
I have found such data in visibleData property of AdvanceDataGrid, but there is no way to access that property as it is defined as protected.
Please reply me if you have any other solution for my problem.
Please find my code as below:
<common:MyAdvancedDataGrid id="productsDataGrid"
width="100%"
height="100%"
focusRect="null"
borderVisible="false"
horizontalScrollPolicy="auto"
doubleClickEnabled="true"
selectionMode="multipleRows"
sortExpertMode="true"
draggableColumns="false"
defaultLeafIcon="{ null }"
displayItemsExpanded="true"
folderOpenIcon="{ null }"
folderClosedIcon="{ null }"
focusSkin="{ null }"
rowHeight="33"
sortableColumns="false"
variableRowHeight="true"
headerHeight="35"
resize="productsDataGrid_resizeHandler(event)"
keyDown="onProductDataGridKeyDown(event)"
creationComplete="onDataGridCreationComplete(event)"
selectedIndex="0">
<common:dataProvider>
<mx:HierarchicalData id="hierarchicalData"
source="{_hirarchyProductArr}"
childrenField="childCategory"/>
</common:dataProvider>
Filter Code:
<fx:Script>
<![CDATA[
private var _searchProductArr:Array;
private function onSearchChange1():void
{
_searchProductArr = new Array();
productsCount = 0;
IHierarchicalCollectionView(productsDataGrid.dataProvider).filterFunction = browseFilter;
// refresh the ADG's dataProvider
IHierarchicalCollectionView(productsDataGrid.dataProvider).refresh();
}
//Filter Function
private function browseFilter(item:Object):Boolean
{
if (item is Product)
{
var searchString:String = txtSearch.text;
if (searchString.length == 0 || item.productName.toLowerCase().indexOf(searchString.toLowerCase()) >= 0)
{
if (drdSelectProductType.selectedItem.label == ProductConstants.ALL_STR)
{
if(!isProductExist(item as Product))
{
_searchProductArr.push(item);
productsCount = _searchProductArr.length;
}
return true;
}
else if (drdSelectProductType.selectedItem.label == ProductConstants.ACTIVE_STR && (item as Product).isActive)
{
if(!isProductExist(item as Product))
{
_searchProductArr.push(item);
productsCount = _searchProductArr.length;
}
return true;
}
else if (drdSelectProductType.selectedItem.label == ProductConstants.INACTIVE_STR && !(item as Product).isActive)
{
if(!isProductExist(item as Product))
{
_searchProductArr.push(item);
productsCount = _searchProductArr.length;
}
return true;
}
else
return false;
return true;
}
else
return false;
}
else
{
var hasValidChildren:Boolean = false;
for each(var node:Object in item.childCategory)
{
if (browseFilter(node))
{
// productsCount = productsCount - 1;
hasValidChildren = true;
break;
}
}
return hasValidChildren;
}
}
private function isProductExist(value:Product):Boolean
{
for each(var product:Product in _searchProductArr)
{
if(product.productId == value.productId)
{
return true;
break;
}
}
return false;
}
]]>

An if/else issue in ActionScript 3

For a project I'm working on, I'm making a simple text-based adventure. At one point, the user sees two keys and a door nearby. I'm trying to make it so that if the user enters the door without both keys, it opens up the frame where it states that they will need both to open it (Frame 11).
The problem is, whether or not the user grabs them, it automatically directs them through the door as if they have opened it (Frame 12).
Here's my total coding. Don't mind commented portions, that's just me trying out some different approaches.
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
var torchb: Boolean = false;
var redkeyb: Boolean = true;
var bluekeyb: Boolean = true;
stop();
StartButtonInst.addEventListener(MouseEvent.CLICK, Page2);
function Page2(event:MouseEvent):void
{
gotoAndStop(2);
LeftButtonInst.addEventListener(MouseEvent.CLICK, Page3);
RightButtonInst.addEventListener(MouseEvent.CLICK, Page4);
}
function Page3(event:MouseEvent):void
{
gotoAndStop(3);
GoBack1Inst.addEventListener(MouseEvent.CLICK, Page2);
}
function Page4(event:MouseEvent):void
{
gotoAndStop(4);
WalkOffInst.addEventListener(MouseEvent.CLICK, Page5);
}
//function torch (e:MouseEvent){
/*if(torchb == true){
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else{
this.gotoAndStop(6);
} */
/*if(!torchb) {
torchb = true;
this.removeChild(GrabTorchInst);
//this.gotoAndStop(7);
}
else {
torchb = false;
this.gotoAndStop(6);
}*/
//GrabTorchInst.addEventListener(MouseEvent.CLICK, torch);
//This event should change the boolean if the torch is picked up
//EnterCaveInst.addEventListener(MouseEvent.CLICK, cave);
//This event should change the frame to the cave or not depending what the torch bool is.
function torch (e:MouseEvent) {
torchb = torchb==true?false:true;
this.removeChild(GrabTorchInst);
}
function cave(e:MouseEvent) {
if(torchb) {
this.gotoAndStop(7);
}
else {
this.gotoAndStop(6);
}
}
function Page5(event:MouseEvent):void
{
trace(torchb);
gotoAndStop(5);
GrabTorchInst.addEventListener(MouseEvent.CLICK, torch);
EnterCaveInst.addEventListener(MouseEvent.CLICK, cave);
//GrabTorchInst.addEventListener(MouseEvent.CLICK, Page5);
EnterCaveInst.addEventListener(MouseEvent.CLICK, Page7);
/*if (GrabTorchInst = true)
{
this.gotoAndStop(7);
} */
}
/*function torch (e:MouseEvent){
if(!torchb) {
torchb = true;
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else {
torchb = false;
this.gotoAndStop(6);
}
}*/
function Page6(event:MouseEvent):void
{
gotoAndStop(6);
GoBack2Inst.addEventListener(MouseEvent.CLICK, Page5)
}
function Page7(event:MouseEvent):void
{
gotoAndStop(7);
LightTorchInst.addEventListener(MouseEvent.CLICK, Page8)
AssassinateInst.addEventListener(MouseEvent.CLICK, Page10)
}
function Page8(Event:MouseEvent):void
{
gotoAndStop(8);
AssassinateInst.addEventListener(MouseEvent.CLICK, Page10)
RunAwayInst.addEventListener(MouseEvent.CLICK, Page9)
}
function Page9(Event:MouseEvent):void
{
gotoAndStop(9);
GoBack3Inst.addEventListener(MouseEvent.CLICK, Page7)
}
/*function redkey (e:MouseEvent){
if(!redkeyb) {
redkeyb = true;
this.removeChild(RedKeyInst);
//this.gotoAndStop(12);
}
else {
redkeyb = false;
this.gotoAndStop(11);
}
} */
function redkey (e:MouseEvent) {
redkeyb = redkeyb==true?false:true;
this.removeChild(RedKeyInst);
}
function bluekey (e:MouseEvent) {
bluekeyb = bluekeyb==true?false:true;
this.removeChild(BlueKeyInst);
}
function door(e:MouseEvent) {
if(redkey==true && bluekey==true){
(12)
this.gotoAndStop(12);
}
else {
this.gotoAndStop(11);
}
}
/*function bluekey (e:MouseEvent){
if(!bluekeyb) {
bluekeyb = true;
this.removeChild(BlueKeyInst);
//this.gotoAndStop(12);
}
else {
bluekeyb = false;
this.gotoAndStop(11);
}
} */
if(redkey==true&&bluekey==true){
(12)
}
function Page10(Event:MouseEvent):void
{
gotoAndStop(10);
RedKeyInst.addEventListener(MouseEvent.CLICK, redkey)
BlueKeyInst.addEventListener(MouseEvent.CLICK, bluekey)
DoorInst.addEventListener(MouseEvent.CLICK, door)
DoorInst.addEventListener(MouseEvent.CLICK, Page12)
}
function Page11(Event:MouseEvent):void
{
gotoAndStop(11)
GoBack4Inst.addEventListener(MouseEvent.CLICK, Page10)
}
function Page12(Event:MouseEvent):void
{
gotoAndStop(12)
GiveJakeAnAInst.addEventListener(MouseEvent.CLICK, Page13)
}
function Page13(Event:MouseEvent):void
{
gotoAndStop(13)
}
I'd really appreciate some assistance on this.
Don't know if this is what you want but you can try
if(torchb == true){
this.removeChild(GrabTorchInst);
this.gotoAndStop(7);
}
else{
this.gotoAndStop(6);
}
So if torchb is true, they would be sent to 7th frame
if it isn't, they would be sent to sixth frame.

send keyCode without key event?

I got this function (in the document class)
public function kyz(event:KeyboardEvent):void{
trace(event.keyCode);
switch (event.keyCode){
case 65:{
if (ppm.currentFrame<200 || ppm.currentFrame>300) {
ppm.gotoAndStop(301);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
pd.play();
}else {
pd.play();
ppm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 68:{
if (ssm.currentFrame<200 || ssm.currentFrame>300) {
ssm.gotoAndStop(301);
ppm.gotoAndStop(302);
llm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
sd.play();
}else {
sd.play();
ssm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 74:{
if (llm.currentFrame<200 || llm.currentFrame>300) {
llm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
mmm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
ld.play();
}else {
ld.play();
llm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
case 76:{
if (mmm.currentFrame<200 || mmm.currentFrame>300) {
mmm.gotoAndStop(301);
ppm.gotoAndStop(302);
ssm.gotoAndStop(302);
llm.gotoAndStop(302);
myTimer.stop();
mTimer.stop();
md.play();
}else {
md.play();
mmm.gotoAndPlay(10);
tlrnc-=10;
}
break;
}
}
}
that receives key event's and do stuff. now I'm trying to pass they keyCode from another function (to avoid changing the whole code for adding click functionality) got any suggestions?
you could dispatch a KeyboardEvent
stage.dispatchEvent(
new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, myCharCode, myKeyCode));
just add the needed keyCode as a parameter
You cannot send any Keyboard or Mouse event without interactivity with user. You can handle this in another private function:
private function keyDownHandler(event : KeyboardEvent) : void {
this.handleEvent(event.keyCode);
}
private function handleEvent(keyCode : uint) : void {
//some actions
}
And when you need to make specific actions, you can just call handleEvent function without user side interactivity.

Need the update to always run

I'm using Actionscript 3 and not doing classes, only the actionscript for each panel. I'm trying to make a character creation screen that tells the player how many points they have left after they add them to one of six categories. The score is working, BUT the part that updates the player as to how many points they have left ISN'T. I need it to constantly update to tell the player how many points are left.
var score=0;
var strscore=1;
scorenum.text= update(score); //How I want it displayed.
function update(score) //Brings in the score variable
{
while(score<66) //As you can see I'm trying to make it constantly call here.
//No Good. Calls once.
{
trace("update Score "+score)
var num= 65-score;
scorenum.text= num;
return num;
}
}
strMinus.addEventListener(MouseEvent.CLICK, strMinusFn);
function strMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (strscore >1 && strscore<=50)
{
strscore--;
score--;
}
else
{
strscore==strscore;
}
}
sX.text=strscore;
return score;
}
strPlus.addEventListener(MouseEvent.CLICK, strPlusFn);
function strPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (strscore >=1 && strscore<50)
{
strscore++;
score++;
}
else
{
strscore==strscore;
}
}
sX.text=strscore;
return score;
}
var dexscore=1;
dexMinus.addEventListener(MouseEvent.CLICK, dexMinusFn);
function dexMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (dexscore >1 && dexscore!=50)
{
dexscore--;
score--;
}
else
{
dexscore==dexscore;
}
dX.text=dexscore;
}
return score;
}
dexPlus.addEventListener(MouseEvent.CLICK, dexPlusFn);
function dexPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (dexscore >=1 && dexscore<50)
{
dexscore++;
score++;
}
else
{
dexscore==dexscore;
}
dX.text=dexscore;
}
return score;
}
var intscore=1;
intMinus.addEventListener(MouseEvent.CLICK, intMinusFn);
function intMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (intscore >1 && intscore!=50)
{
intscore--;
score--;
}
else
{
intscore==intscore;
}
iX.text=intscore;
}
return score;
}
intPlus.addEventListener(MouseEvent.CLICK, intPlusFn);
function intPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (intscore >=1 && intscore<50)
{
intscore++;
score++;
}
else
{
intscore==intscore;
}
iX.text=intscore;
}
return score;
}
var conscore=1;
conMinus.addEventListener(MouseEvent.CLICK, conMinusFn);
function conMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (conscore >1 && conscore!=50)
{
conscore--;
score--;
}
else
{
conscore==conscore;
}
cX.text=conscore;
}
return score;
}
conPlus.addEventListener(MouseEvent.CLICK, conPlusFn);
function conPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (conscore >=1 && conscore<50)
{
conscore++;
score++;
}
else
{
conscore==conscore;
}
cX.text=conscore;
}
return score;
}
var wisscore=1;
wisMinus.addEventListener(MouseEvent.CLICK, wisMinusFn);
function wisMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (wisscore >1 && wisscore!=50)
{
wisscore--;
score--;
}
else
{
wisscore==wisscore;
}
wX.text=wisscore;
}
return score;
}
wisPlus.addEventListener(MouseEvent.CLICK, wisPlusFn);
function wisPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (wisscore >=1 && wisscore<50)
{
wisscore++;
score++;
}
else
{
wisscore==wisscore;
}
wX.text=wisscore;
}
return score;
}
var chascore=1;
chaMinus.addEventListener(MouseEvent.CLICK, chaMinusFn);
function chaMinusFn(event:MouseEvent)
{
if (score<=59)
{
if (chascore >1 && chascore!=50)
{
chascore--;
score--;
}
else
{
chascore==chascore;
}
hX.text=chascore;
}
return score;
}
chaPlus.addEventListener(MouseEvent.CLICK, chaPlusFn);
function chaPlusFn(event:MouseEvent)
{
if (score<=58)
{
if (chascore >=1 && chascore<50)
{
chascore++;
score++;
}
else
{
chascore==chascore;
}
hX.text=chascore;
}
return score;
}
You could use a while loop that will update the remainder every time you put a score into a "attribute".
Something along the lines of
while score > 0
if attribute_variable > variable_counter
score -= 1
variable_counter += 1
So the while loop will check if you have any score to put into your attributes. The if statement will be checking to see if you put a point into the specific attribute and the statements inside the if are to update the score and the counter for that specific counter so that the counter recognizes you have put a point into that variable.