An if/else issue in ActionScript 3 - 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.

Related

How to stop Touch or Click Event while Swipe Event is in action in as3

In my code i use both swipe gesture and click event at the same time. How to avoid click event or touch event while swipe gesture is in action?
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, fl_SwipeHandler);
wall.tile[0].addEventListener(MouseEvent.CLICK, showBook());
wall.tile[1].addEventListener(MouseEvent.CLICK, showBook());
public function fl_SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetY)
{
// swiped down
case 1:
{
if (swipe!=0){
Tweener.addTween(wall, {y: wall.y + 650, time:.5, transition:"easeOutSine" } );
swipe--;
}
// End your custom code
break;
}
// swiped up
case -1:
{
if (swipe<=(total/5)){
Tweener.addTween(wall, { y: wall.y - 650, time:.5, transition:"easeOutSine" } );
swipe++;
}
// End your custom code
break;
}
}
}
I usually have some boolean variable set to true when swipe is being activated, and onComplete function of tweener sets it back to false. And that boolean is one condition in my onClick functions (as false obviously):
public var swipeOn:Boolean;
public function fl_SwipeHandler(event:TransformGestureEvent):void{
switch(event.offsetY){
case 1:{
if (swipe!=0){
swipeOn = true;
Tweener.addTween(wall, {y: wall.y + 650, time:.5, transition:"easeOutSine", onComplete:doneSwipe } );
swipe--;
}
break;
}
case -1:{
if(swipe<=(total/5)){
swipeOn = true;
Tweener.addTween(wall, { y: wall.y - 650, time:.5, transition:"easeOutSine", onComplete:doneSwipe } );
swipe++;
}
break;
}
}
}
public function doneSwipe():void{
swipeOn = false;
}
public function showBook(Event:MouseEvent):void{
if(!swipeOn){
//....handle your clicks etc
}
}

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;
}
]]>

For each loop not looping through everything

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);
}

ActionScript 3 Error #1009 null object reference

I'm having some errors with a command system I built for my flash project. One of the commands is intro which essentially restarts the project, or is meant to (The intro starts at frame 0 of the first scene). But when it runs a reference error is thrown on what I have determined to be line 21 (Through commenting code out until it no longer errors) which is the thisRef.gotoAndPlay line of the intro function.
Here is my code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.system.fscommand;
import flash.events.KeyboardEvent;
import flash.media.Sound;
public class Main extends MovieClip {
/* The real frame number */
public var realFrameNo:int = 0;
/* Whether startup has been ran or not */
public var startupRan:Boolean = false;
/* Whether or not the beggining text is there */
public var placeholderPresent:Boolean = true;
/* Commands recognized by the computer [command:string,handler:void] */
public var commandList = new Array(["intro",function(thisRef:Main) {
gotoAndPlay(0,"Intro");
}],
["exit",function(thisRef:Main) {
fscommand("quit");
}]);
/* Helper functions */
public function isAlphabetic(value:String):Boolean
{
var result:String = value.match(/[a-zA-Z]*/)[0];
return value.length == result.length;
}
public function getSceneNo():int
{
return (realFrameNo > 0 ? Math.floor( (realFrameNo / stage.frameRate) ) : 0 );
}
public function getSceneName(sceneNo:int):String
{
switch(sceneNo)
{
default:
return "Intro";
case 0:
return "Intro";
case 1:
return "MainMenu";
}
}
/* Main functions */
public function Main()
{
this.addEventListener('enterFrame',function() {
if(!this.startupRan)
realFrameNo += 1;
/* Main menu init */
if(getSceneNo() == 1 && !this.startupRan && currentFrame == 2)
{
stop();
this.startupRan = true;
/* Add keyboard events */
stage.addEventListener(KeyboardEvent.KEY_DOWN,function(event:KeyboardEvent){
var letter:String = String.fromCharCode(event.charCode).toUpperCase();
if(event.keyCode == 8)
updateMonitor("");
else if(event.keyCode == 13)
evalInput();
else if(isAlphabetic(letter))
updateMonitor(letter);
});
/* Add button events */
btnQ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Q"); });
btnW.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("W"); });
btnE.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("E"); });
btnR.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("R"); });
btnT.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("T"); });
btnY.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Y"); });
btnU.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("U"); });
btnI.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("I"); });
btnO.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("O"); });
btnP.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("P"); });
btnA.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("A"); });
btnS.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("S"); });
btnD.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("D"); });
btnF.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("F"); });
btnG.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("G"); });
btnH.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("H"); });
btnJ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("J"); });
btnK.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("K"); });
btnL.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("L"); });
btnZ.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("Z"); });
btnX.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("X"); });
btnC.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("C"); });
btnV.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("V"); });
btnB.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("B"); });
btnN.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("N"); });
btnM.addEventListener(MouseEvent.CLICK,function(){ updateMonitor("M"); });
btnDel.addEventListener(MouseEvent.CLICK,function(){ updateMonitor(""); });
btnEnter.addEventListener(MouseEvent.CLICK,function(){ evalInput(); });
btnReturn.addEventListener(MouseEvent.CLICK,function(){ evalInput(); });
}
});
}
public function updateMonitor(letter:String)
{
if(this.placeholderPresent) //Remove placeholder
lblInput.text = letter;
else if(letter == '' || letter == null) //Backspace
{
lblInput.text = lblInput.text.substr(0,lblInput.text.length-1);
}
else //Append text
lblInput.appendText(letter);
if(lblInput.text == "")
{
lblInput.text = "Type something to begin..";
this.placeholderPresent = true;
}
else
this.placeholderPresent = false;
}
public function evalInput():void
{
if(this.placeholderPresent) //Don't eval the starting text
return;
lblOutput.text = "Scanning for commands...";
for(var i:int = 0; i < this.commandList.length; i++)
{
if(lblInput.text.toLowerCase() == this.commandList[i][0])
{
this.commandList[i][1](this);
lblOutput.appendText("\nCommand ran.");
return;
}
}
lblOutput.appendText("\nNo command recognized.");
}
}
}
Full stack trace:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/evalInput()[H:\Year 2\Units\Unit 43 - Multimedia Design\Flash Application\Main.as:144]
at Function/<anonymous>()[H:\Year 2\Units\Unit 43 - Multimedia Design\Flash Application\Main.as:71]

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.