Dynamically populate Text Fields in Flash (AS3) - arrays

I am trying to figure out how to dynamically populate textfields in Flash. The text in the fields will either say ON or OFF based on the value of bstatus.
var bstatus will either have a value of 0 or 1
I have the following textfields listed below.
I'm not sure if the syntax is correct, but I was thinking that the text fields would be in an array, and I would create a for loop--that will go through the array in order to have the fields populated.
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = washerTxt.text;
I'm thinking that that the textFields Array will go inside a function called populateFields()
// function populateFields ------------------------------------------------------------------
function populateFields(bstatus:int)
{
var displayText:String="";
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = washerTxt.text;
if (bstatus == 0) {
// textfield will say OFF
displayText = "OFF";
} else if (bstatus == 1) {
// textfield will say ON
displayText = "ON";
}
/*
for (var i:int=0;i<textFields.length;i++)
{
// do something
}
*/
}
Entire Code:
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.EventDispatcher; //event dispatcher
public class House extends MovieClip
{
// List Objects inside Treehouse here....
//private var comp:MovieClip; // comp is a property of TreeHouse
//private var light:MovieClip;
//var comp:HouseObjects = new HouseObjects(); //do this inside another class
var HouseObjects:Array = new Array(); // creates HouseObjects array
var onList:Array = [];
var obj_num:int = 10;
var power:int; // holds value of individual House Objects
var bstate:int; // 0 or 1 (ON or OFF)
var bstatus:int;
var userInput:int; // stores user data (of selected data); holds value of e.currentTarget.power
var currentPower:int; // stores current power
//var objName:String;
var objSelect:Object;
// Constructor--------------------------------------------------------------------
public function House()
{
var currentPower:int = 0;
HouseObjects[0] = new Comp();
HouseObjects[1] = new Light(); // bathroom light
HouseObjects[2] = new LightB(); // computer area light
HouseObjects[3] = new LightC(); // kitchen light
HouseObjects[4] = new LightD(); // living room light
HouseObjects[5] = new LightE(); // description light
HouseObjects[6] = new LightF(); // laundry room light
HouseObjects[7] = new Oven();
HouseObjects[8] = new Tv();
HouseObjects[9] = new Washer();
//HouseObjects[10] = new Car();
//onList.push(objName);
//trace(objName);
//onList.push(HouseObjects[1]);
trace("Tracing onList Array: " + onList);
// list properties of the objects -----------------------------------------------------------------
HouseObjects[0].power = 2; // amount of power
HouseObjects[0].name = "comp";
//comp.bstate = 0; // button state
HouseObjects[1].power = 1; // amount of power
HouseObjects[1].name = "light";
HouseObjects[2].power = 1; // amount of power
HouseObjects[2].name = "lightB";
HouseObjects[3].power = 1; // amount of power
HouseObjects[3].name = "lightC";
HouseObjects[4].power = 1; // amount of power
HouseObjects[4].name = "lightD";
HouseObjects[5].power = 1; // amount of power
HouseObjects[5].name = "lightE";
HouseObjects[6].power = 1; // amount of power
HouseObjects[6].name = "lightF";
HouseObjects[7].power = 3; // amount of power
HouseObjects[7].name = "oven";
HouseObjects[8].power = 4; // amount of power
HouseObjects[8].name = "tv";
HouseObjects[9].power = 5; // amount of power
HouseObjects[9].name = "washer";
//HouseObjects[9].power = 6; // amount of power
//HouseObjects[9].name = "car";
// end of list properties of the house objects -----------------------------------------------------
for (var i:int=0;i<obj_num;i++)
{
HouseObjects[i].buttonMode = true;
//add event listeners -- listens to functions that are called
HouseObjects[i].addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
HouseObjects[i].addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
HouseObjects[i].addEventListener(MouseEvent.CLICK, toggleClick);
HouseObjects[i].bstate = 0;
stage.addChild(HouseObjects[i]); // add House Objects to stage ------------------------------
}// end of for loop ------------------------------------------------------------------------
trace("tracing...");
//computer's position
HouseObjects[0].x = 585;
HouseObjects[0].y = 233;
//bathroom light's position
HouseObjects[1].x = 340;
HouseObjects[1].y = 161;
//computer lightB's position
HouseObjects[2].x = 579;
HouseObjects[2].y = 158;
//computer lightC's position
HouseObjects[3].x = 316;
HouseObjects[3].y = 368;
//computer lightD's position
HouseObjects[4].x = 657;
HouseObjects[4].y = 367;
//computer lightE's position
HouseObjects[5].x = 517;
HouseObjects[5].y = 549;
//computer lightF's position
HouseObjects[6].x = 531;
HouseObjects[6].y = 1000;
//oven's position
HouseObjects[7].x = 380;
HouseObjects[7].y = 449;
//tv's position
HouseObjects[8].x = 543;
HouseObjects[8].y = 423;
//washer's position
HouseObjects[9].x = 637;
HouseObjects[9].y = 1155;
} // end of Constructor -----------------------------------------------------------
// function rollOver --------------------------------------------------------------
function rolloverToggle(e:MouseEvent) {
if (e.currentTarget.currentFrame == 1)
e.currentTarget.gotoAndStop(2);
if (e.currentTarget.currentFrame == 3)
e.currentTarget.gotoAndStop(4);
}
// function rollOut -----------------------------------------------------------------
function rolloutToggle(e:MouseEvent) {
if (e.currentTarget.currentFrame == 2)
e.currentTarget.gotoAndStop(1);
if (e.currentTarget.currentFrame == 4)
e.currentTarget.gotoAndStop(3);
}
// function toggleClick-------------------------------------------------------------
function toggleClick(e:MouseEvent) {
// On MouseEvent gotoAndStop(Frame Number)
if (e.currentTarget.currentFrame == 2)
{
e.currentTarget.gotoAndStop(3);
e.currentTarget.bstate = 1;
}
if (e.currentTarget.currentFrame == 4)
{
e.currentTarget.gotoAndStop(1);
e.currentTarget.bstate = 0;
}
// trace statements -------------------------------------------------------
//trace("movieClip Instance Name = " + e.currentTarget); // [object Comp]
//trace(houseArray[e.currentTarget.name]); // comp
trace("using currentTarget: " + e.currentTarget.name); // comp
//trace("powerData: " + powerData); // power of user data
//trace("houseArray: " + houseArray[0]); // the 0 index of house array
// trace(e.currentTarget.power); // currentTarget's power************
//trace ("bstate in click function: " + e.currentTarget.bstate);
//objName = e.currentTarget.name;
trace("target: " + e.currentTarget);
bstatus = e.currentTarget.bstate;
userInput = e.currentTarget.power;
objSelect = e.currentTarget;
trace("objSelect: " + objSelect);
// end of trace statements -------------------------------------------------
calcPower(userInput, bstatus);
//trace("i am bstatus: " + bstatus);
//trace("i am power: " + userInput);
populateFields(bstatus);
sendPhp();
//trackItems(objSelect, bstatus);
} // end of function toggleClick ----------------------------------------------------
// function calcPower ------------------------------------------------------------------
function calcPower(userInput:int, bstatus:int):void
{
//trace("i arrived");
//trace("power: " + userInput + " and " + "bstate: " + bstatus);
if (bstatus == 0) {
trace("i have been clicked OFF");
currentPower -= userInput; // if OFF then minus
trace("currentPower: " + currentPower);
} else if (bstatus == 1) {
trace("i have been clicked ON");
currentPower += userInput; // if OFF then minus
trace("currentPower: " + currentPower);
}
}
// function populateFields ------------------------------------------------------------------
function populateFields(bstatus:int)
{
var displayText:String="";
var textFields:Array = new Array();
textFields[0] = compTxt.text;
textFields[1] = bathLightTxt.text;
textFields[2] = computerLightTxt.text;
textFields[3] = kitchenLight.text;
textFields[4] = livingLightTxt.text;
textFields[5] = descLightTxt.text;
textFields[6] = laundryLightTxt.text;
textFields[3] = ovenTxt.text;
textFields[8] = tvTxt.text;
textFields[9] = WasherTxt.text;
if (bstatus == 0) {
// textfield will say OFF
} else if (bstatus == 1) {
// textfield will say ON
}
/*
for (var i:int=0;i<textFields.length;i++)
{
// do something
}
*/
}
// function pwrPercentage ------------------------------------------------------------------
/*function pwrPercentage()
{
}
*/
// function trackItems ------------------------------------------------------------------
function trackItems(objSelect:Object, bstatus:int):void
{
trace("i arrived in trackItems");
trace("tracing objSelect in function trackItems: " + objSelect);
//trace("tracing Array onList :" + onList);
//trace("power: " + userInput + " and " + "bstate: " + bstatus);
if (bstatus == 0) {
//onList.removeItemAt(onList.getItemIndex(objSelect));
// remove from ON list (array) pop
// call function removeArrayItem
removeArrayItem(objSelect);
} else if (bstatus == 1) {
//trace("adding items to list");
onList.push(objSelect);
//add to ON list (array) push
}
trace("Array:" + onList);
//return array .... only have one array...contain objects that are ONLY ON
}
// function removeArrayItem ------------------------------------------------------------------
function removeArrayItem(objSelect:Object):void
{
var arrayLength:int = onList.length;
// Searches item in array
for (var i:int=0; i<arrayLength; i++)
{
// Finds item and removes it
if (onList[i] == objSelect)
{
onList.splice(i, 1);
///*/**/*/trace("Item Removed: " + onList[i]);
trace("Array Updated: " + onList);
}
}
}
// function frameloop ------------------------------------------------------------------
/* function frameloop(e:Event)
{
}
*/
// function sendPhp ------------------------------------------------------------------
function sendPhp(e:MouseEvent):void
{
// send vars to php
var request:URLRequest = new URLRequest("write_xml.php"); // the php file to send data to
var variables:URLVariables = new URLVariables(); // create an array of POST vars
variables['bathLight'] = compTxt.text;
variables['bathLight'] = bathLightTxt.text;
variables['computer'] = computerLightTxt.text;
variables['kitchenLight'] = kitchenLight.text;
variables['livingLight'] = livingLightTxt.text;
variables['descLight'] = descLightTxt.text;
variables['laundryLight'] = laundryLightTxt.text;
variables['oven'] = ovenTxt.text;
variables['tv'] = tvTxt.text;
variables['washer'] = washerTxt.text;
request.data = variables; // send the vars to the data property of the requested url (our php file)
request.method = URLRequestMethod.POST; // use POST as the send method
try
{
var sender:URLLoader = new URLLoader();
sender.load(request); // load the php file and send it the variable data
navigateToURL(new URLRequest("vars.xml"), '_blank'); //show me the xml
}
catch (e:Error)
{
trace(e); // trace error if there is a problem
}
}
// function called when user clicks on update button-----------------------------------
/*function updateStage():void
{
for (var i:int = 0; i<=onList.length;i++) {
addChild(onList[i]);
}
}*/
} //end of class
} // end of package

You will be better off keeping track of the reference to your textboxes in order to populate them afterwards:
var textFields:Array = new Array();
textFields[0] = compTxt;
textFields[1] = bathLightTxt;
textFields[2] = computerLightTxt;
textFields[3] = kitchenLight;
textFields[4] = livingLightTxt;
textFields[5] = descLightTxt;
textFields[6] = laundryLightTxt;
textFields[3] = ovenTxt;
textFields[8] = tvTxt;
textFields[9] = washerTxt;
I tried it out this way:
import fl.controls.*;
var textFields:Array = new Array(9);
textFields[0] = new TextInput();
textFields[1] = new TextInput();
textFields[2] = new TextInput();
textFields[3] = new TextInput();
textFields[4] = new TextInput();
textFields[5] = new TextInput();
textFields[6] = new TextInput();
textFields[7] = new TextInput();
textFields[8] = new TextInput();
textFields[9] = new TextInput();
var i:uint = 0;
for(i = 0; i < textFields.length; ++i){
textFields[i].text = "Set text at " + i;
trace(textFields[i].text);
}

Related

Test class coverage not increasing after 71 %. "Bold Italic part is not covering in Test class."

enter image description hereThe trigger is working completely fine, but Its not giving 100% code coverage. The main issue with Test class, its not covering the if condition of Role__c.
Trigger:
trigger DrawingSharing on Drawing__c (after insert) {
if(trigger.isInsert){
for (Drawing__c draw : Trigger.new){
System.debug('draw: '+draw);
List<Installation__c> ListInstalltionList = new List <Installation__c>();
for(String installationActivityList :draw.Installation_Activity__c.split(';')){
Installation__c inst = new Installation__c();
inst.Drawing_ID__c = draw.Id;
inst.Installation_Activity__c=installationActivityList;
inst.Execution__c=draw.Execution_ID__c;
inst.Floor__c=draw.Floors__c;
ListInstalltionList.add(inst);
}
system.debug('ListInstalltionList==>'+ListInstalltionList);
insert ListInstalltionList;
List<Drawing__c> newList = new List<Drawing__c>();
newList.add(draw);
Map <Id,Id> DrawingProjectMap = new Map<Id,Id>();
Map<Id,Drawing__c> drawingIdObjMap = new Map<Id,Drawing__c>([select id, name,Approver__c, Execution_ID__r.Project__c from Drawing__c where id in :newList limit 1]);
for(Drawing__c Drawing : drawingIdObjMap.values())
{
DrawingProjectMap.put(Drawing.Id,Drawing.Execution_ID__r.Project__c);
}
system.debug(DrawingProjectMap);
Map<Id,RoleObjectSharing__mdt> roleobjaccessmap = new Map<Id,RoleObjectSharing__mdt>([select id,Object__c,role__c,Accesslevel__c from RoleObjectSharing__mdt where Object__c='Drawing']);
Map<String,String> RoleAccessmap = new Map<String,String>();
for(RoleObjectSharing__mdt Robj : roleobjaccessmap.values()){
RoleAccessmap.put(Robj.role__c,Robj.Accesslevel__c);
}
system.debug(RoleAccessmap.keySet());
system.debug(DrawingProjectMap.values());
Map<Id,Projects__c> teamrolemap = new Map<Id,Projects__c>([select Id,(select id, User_Lookup__c,Role__c from TeamRoles__r where Role__c in:RoleAccessmap.keySet()) from Projects__c where Id in :DrawingProjectMap.values()]);
system.debug('teamrolemap'+teamrolemap);
List <Drawing__Share> DrawShareList = new List<Drawing__Share>();
List <Drawing__c> DrawList = new List<Drawing__c>();
INTEGER countFound = 0;
for(Drawing__c drawobj : newList){
Projects__c proj = teamrolemap.get(DrawingProjectMap.get(drawobj.id));
system.debug(proj.teamroles__r);
***for(teamroles__c tr : proj.teamroles__r){
system.debug(tr);
if(tr.Role__c == 'PlanningEngineer' || tr.Role__c == 'AssistantPlanningEngineer' || tr.Role__c == 'PlanningManager' || tr.Role__c == 'AssistantPlanningManager' && countFound <=0) {
countFound = countFound + 1;
Drawing__c ddd = drawingIdObjMap.values();
ddd.Approver__c = tr.User_Lookup__c;
DrawList.add(ddd);
}
Drawing__Share recruiterShr = new Drawing__Share();
recruiterShr.ParentId = drawobj.Id;
recruiterShr.UserOrGroupId = tr.User_Lookup__c;
recruiterShr.AccessLevel = RoleAccessmap.get(tr.role__c);
recruiterShr.RowCause = tr.role__c+'__c';
DrawShareList.add(recruiterShr);***
}
system.debug(DrawShareList);
}
upsert DrawList;
Database.SaveResult[] lsr = Database.insert(DrawShareList,false);
Integer i=0;
for(Database.SaveResult sr : lsr){
if(!sr.isSuccess()){
}
i++;
}
}
}
}
Test Class:
static testMethod void Test4(){
System.debug('TestSharing = Test4');
Test.startTest();
Projects__c proj = new Projects__c();
proj.Name = 'Hello';
proj.LL_Location__c='Mumbai';
insert proj;
TeamRoles__c tr1 = new TeamRoles__c();
tr1.User_Lookup__c = UserInfo.getUserId();
tr1.Role__c = 'PlanningEngineer';
tr1.Project_Name__c = proj.Id;
insert tr1;
TeamRoles__c tr2 = new TeamRoles__c();
tr2.User_Lookup__c = UserInfo.getUserId();
tr2.Role__c = 'PlanningManager';
tr2.Project_Name__c = proj.Id;
insert tr2;
Execution__c ex = new Execution__c();
ex.Project__c = proj.Id;
insert ex;
Item_Master__c ItemeMaster = new Item_Master__c();
ItemeMaster.Item_Description__c = 'testing';
ItemeMaster.Item_Code__c = 'testing';
insert ItemeMaster;
Project_Items__c projItems = new Project_Items__c();
projItems.Total_PO_Quantity__c = 0;
projItems.ItemMaster__c = ItemeMaster.Id;
projItems.Execution__c = ex.Id;
insert projItems;
List<TeamRoles__c> lstTR = new List<TeamRoles__c>();
TeamRoles__c tr7 = new TeamRoles__c();
tr7.User_Lookup__c = UserInfo.getUserId();
tr7.Role__c = 'PlanningEngineer';
tr7.Project_Name__c = proj.Id;
lstTR.add(tr7);
List<Drawing__c> d= new List<Drawing__c>();
Drawing__c draw = new Drawing__c();
draw.Execution_Id__c = ex.Id;
draw.Floors__c='5F';
draw.Installation_Activity__c='Cable Tray';
draw.Drawing_Line_Items_Count__c = 0;
d.add(draw);
insert d;
//insert draw;
Installation__c inst = new Installation__c();
inst.Drawing_ID__c=draw.Id;
inst.Floor__c= draw.Floors__c;
inst.Installation_Activity__c='Cable Tray';
inst.As_built_Marked_Drawing_Prepared_by_Site__c =False;
inst.Execution__c = ex.Id;
insert inst;
SharingUtilityClass suc = new SharingUtilityClass();
suc.initObjSharing(lstTR);
Drawing_Line_Items__c dll = new Drawing_Line_Items__c();
dll.Drawing_Number__c = draw.Id;
dll.Project_Items__c = projItems.Id;
dll.Project_Name__c = proj.Id;
dll.Installation_Activity__c='Cable Tray';
dll.GFC_Quantity__c = 15;
dll.Shop_Drawing_Quantity__c = 20;
dll.GFC_Drawing_Line_Item_Done__c = true;
dll.Shop_Drawing_Line_Item_Done__c=true;
insert dll;
Installation_Line_Items__c instline = new Installation_Line_Items__c();
instline.Installation__c = inst.Id;
instline.Project_Items__c = projItems.Id;
instline.Installation_Activity__c='Cable Tray';
instline.Total_installed_quantity_DPR_Quantity__c = 100.00;
insert instline;
List<Drawing_Line_Items__c> listProj2 = CircularProgressController.getDrawingLineItem(dll.Id);
List<Drawing_Line_Items__c> listProj3 = CircularProgressController.getDrawingLineItemShop(dll.Id);
List<Drawing__c> listProj4 = Drawing.getDrawing(draw.Id);
List<Project_Items__c> listProj5 = Drawing.getProjectItems(ex.Id);
Test.stopTest();
}
If there are team roles being debugged from this line
for(teamroles__c tr : proj.teamroles__r){
system.debug(tr);
I would then check to see if this is evaluating to true within the test context:
if(tr.Role__c == 'PlanningEngineer' || tr.Role__c == 'AssistantPlanningEngineer' || tr.Role__c == 'PlanningManager' || tr.Role__c == 'AssistantPlanningManager' && countFound <=0)

Unity javascript array problems

I have next code
var D3s = new Array(9);
var D3n: float[];
result = Mine(result);
function Mine(block) {
D3n = new float[5];
var init = false;
if (block.startsWith("##new#")) {
block = block.replace("##new#", "");
init = true;
D3n[3] = 16;
}
var s1 = block.split("&");
s1.forEach(foreachBlocks);
if (init) {
//set default values
D3n[1] = 0;
D3s[6] = "ff";
D3n[2] = 0;
}
return TurnM79();
}
and error: CompilationErrorsException: script(9,9): BCE0005: Boo.Lang.Compiler.CompilerError: Unknown identifier: 'D3n'.
what wrong?

How do you make buttons that have a specific length?

I'm having difficulty using a customlistrenderer class and homescreen class.
Here's my Homescreen class that contains the function generateData(), which is supposed to display a scroller of 22 buttons.
private function generateData(): void{
var list: List = new List();
list.width = 235;
list.height = 380;
list.name = "HomeScreenList";
this.addChild(list);
var rows:int = 6;
var iterator:int = 0;
var num:int = 22;
var counter:int = 0;
var obj:Object;
var heroName:Array = ["ana", "bastion", "d.va", "genji", "hanzo", "junkrat", "lucio",
"mccree", "mei", "mercy", "pharah", "reaper", "reinhardt", "roadhog", "soldier76", "symmetra",
"torbjorn", "tracer", "widowmaker", "winston", "zarya", "zenyatta"];
var temp:Array = []
obj = new Object();
while(iterator < rows){
obj.name1 = heroName[counter];
obj.image1 = myResource.getImage(heroName[counter]);
obj.name2 = heroName[counter+1];
obj.image2 = myResource.getImage(heroName[counter+1]);
obj.name3 = heroName[counter+2];
obj.image3 = myResource.getImage(heroName[counter+2]);
obj.name4 = heroName[counter+3];
obj.image4 = myResource.getImage(heroName[counter+3]);
temp.push(obj);
counter+4;
iterator++;
}
var collection:ListCollection = new ListCollection(temp);
//assign the renderer
list.itemRendererType = CustomMenuListRenderer;
list.dataProvider = collection;
list.addEventListener(Event.CHANGE, onChange);
this.addChild(list);
list.validate();
}
and the CustomMenuListRenderer class, under the initialize() function
super.initialize();
firstButton = new Button();
this.addChild(firstButton);
firstButton.x = 20;
firstButton.y = 100;
firstButton.scale = 0.60;
secondButton = new Button();
secondButton.x = 92;
secondButton.y = 100;
secondButton.scale = 0.60;
thirdButton = new Button();
thirdButton.x = 163;
thirdButton.y = 100;
thirdButton.scale = 0.60;
fourthButton = new Button();
fourthButton.x = 235;
fourthButton.y = 100;
fourthButton.scale = 0.60;
firstButton.addEventListener(Event.TRIGGERED, onTrigger);
secondButton.addEventListener(Event.TRIGGERED, onTrigger);
thirdButton.addEventListener(Event.TRIGGERED, onTrigger);
fourthButton.addEventListener(Event.TRIGGERED, onTrigger);
The problem is since the loop displays the 4 buttons each loop, an error occurs at the 23rd button(since I only specified 22 buttons but I declared 4 buttons on the CustomMenuListRenderer class) in the 6th row.
My question is how do I fix this issue?
First question, why define num manually and not use heroName.lenght? Is more secure
var heroName:Array = ["ana", "bastion", "d.va", "genji", "hanzo", "junkrat", "lucio",
"mccree", "mei", "mercy", "pharah", "reaper", "reinhardt", "roadhog", "soldier76", "symmetra",
"torbjorn", "tracer", "widowmaker", "winston", "zarya", "zenyatta"];
var num:int = heroName.lenght;
To fix your error, you have to check is heroName index exists before to get it.
Something like :
obj = new Object();
while(iterator < rows){
var objCounter:int = 1;
while(objCounter <= 4 )
{
if( counter < heroName.lenght )
{
obj["name"+objCounter] = heroName[counter];
obj["image"+objCounter] = myResource.getImage(heroName[counter]);
}
objCounter++;
counter++;
}
temp.push(obj);
iterator++;
}

Flash CS3 Deleting objects on stage

Ok so I have this minigame inside my main timeline. The minigame creates a bunch of objects dynamically inside an array using addChild(new a0), new a1, new a2 etc... Anyways at the end of the game, there's an option to either restart (resets scores and goes back to starting frame) or finished (goes back a few frames to the "main screen" which is on a different layer and back a few frames. If I choose either options, any of the objects that werent deleted from playing the game (getting a match) are left on the stage even when restarting or going back to the main frame. I've tried various methods of calling removeChild, setting arrays to empty and what not and I can't seem to figure out how to remove them. With the code that I will display here, I get this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at mousiesDay_fla::MainTimeline/clearGame()[mousiesDay_fla.MainTimeline::frame258:11]
at mousiesDay_fla::MainTimeline/tryAgain()[mousiesDay_fla.MainTimeline::frame258:29]
Here is the code
stop();
scoreWindow.visible = false;
scoreWindowText.visible = false;
finBtn.visible = false;
tryBtn.visible = false;
finBtn.removeEventListener(MouseEvent.CLICK, finished);
tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);
function clearGame() {
for( var i:int = 0; i < numClips; i++ ) {
removeChild( myClip[i] );
}
myClip.length = 0;
scoreWindow.visible = false;
scoreWindowText.visible = false;
finBtn.visible = false;
tryBtn.visible = false;
finBtn.removeEventListener(MouseEvent.CLICK, finished);
tryBtn.removeEventListener(MouseEvent.CLICK, tryAgain);
}
function finished(evt:MouseEvent) {
clearGame();
gotoAndPlay(256);
}
function tryAgain(evt:MouseEvent) {
clearGame();
gotoAndPlay(257);
}
backBtn.addEventListener(MouseEvent.CLICK, goBack);
function goBack(evt:MouseEvent) {
gotoAndPlay(256);
}
import flash.utils.*;
var myTimer:Timer = new Timer(1000);
myTimer.addEventListener("timer", timedFunction);
myTimer.start();
function timedFunction(eventArgs:TimerEvent) {
var tc:int= 31 - myTimer.currentCount;
pTime.text = tc.toString();
if (myTimer.currentCount > 30) {
for (var k:Number = 0; k < numClips; k++) {
myClip[k].removeEventListener("mouseDown", pieceMove);
myClip[k].removeEventListener("mouseUp", pieceMove);
}
myTimer.reset();
myTimer.stop();
scoreWindow.visible = true;
scoreWindowText.visible = true;
addChild(scoreWindow);
addChild(scoreWindowText);
scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";
finBtn.visible = true;
finBtn.addEventListener(MouseEvent.CLICK, finished);
addChild(finBtn);
tryBtn.visible = true;
tryBtn.addEventListener(MouseEvent.CLICK, tryAgain);
addChild(tryBtn);
}
}
var mySound:Sound = new correctSound();
upgameScore = 0;
var numClips:Number = 7;
var myClip = new Array(numClips);
myClip[0] = addChild(new a0());
myClip[1] = addChild(new a1());
myClip[2] = addChild(new a2());
myClip[3] = addChild(new a3());
myClip[4] = addChild(new a4());
myClip[5] = addChild(new a5());
myClip[6] = addChild(new a6());
//myClip[7] = addChild(new a7());
//myClip[8] = addChild(new a8());
//myClip[9] = addChild(new a9());
myClip[0].name = "piece0";
myClip[1].name = "piece1";
myClip[2].name = "piece2";
myClip[3].name = "piece3";
myClip[4].name = "piece4";
myClip[5].name = "piece5";
myClip[6].name = "piece6";
//myClip[7].name = "piece7";
//myClip[8].name = "piece8";
//myClip[9].name = "piece9";
var nph = new Array(numClips);
nph[0] = nph0_mc;
nph[1] = nph1_mc;
nph[2] = nph2_mc;
nph[3] = nph3_mc;
nph[4] = nph4_mc;
nph[5] = nph5_mc;
nph[6] = nph6_mc;
//nph[7] = nph7_mc;
//nph[8] = nph8_mc;
//nph[9] = nph9_mc;
var tpg = new Array(numClips);
tpg[0] = tpg0_mc;
tpg[1] = tpg1_mc;
tpg[2] = tpg2_mc;
tpg[3] = tpg3_mc;
tpg[4] = tpg4_mc;
tpg[5] = tpg5_mc;
tpg[6] = tpg6_mc;
//tpg[7] = tpg7_mc;
//tpg[8] = tpg8_mc;
//tpg[9] = tpg9_mc;
var x0 = myClip[0].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y0 = myClip[0].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x1 = myClip[1].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y1 = myClip[1].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x2 = myClip[2].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y2 = myClip[2].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x3 = myClip[3].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y3 = myClip[3].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x4 = myClip[4].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y4 = myClip[4].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x5 = myClip[5].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y5 = myClip[5].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x6 = myClip[6].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y6 = myClip[6].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
/*var x7 = myClip[7].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y7 = myClip[7].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x8 = myClip[8].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y8 = myClip[8].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
var x9 = myClip[9].x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
var y9 = myClip[9].y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;*/
var j:Number;
for (var k:Number = 0; k < numClips; k++) {
myClip[k].addEventListener("mouseDown", pieceMove);
myClip[k].addEventListener("mouseUp", pieceMove);
}
function pieceMove(evt:Event):void {
if (evt.type == "mouseDown") {
//mySound.play();
evt.target.startDrag();
}
else if (evt.type == "mouseUp") {
//mySound.play();
evt.target.stopDrag();
for (j = 0; j < numClips; j++) {
if (evt.target.name == "piece" + j &&
evt.target.hitTestObject(nph[j]) == true) {
removeChild(myClip[j]);
nph[j].alpha = 0;
tpg[j].alpha = 100;
if (j == 2) {
setChildIndex(tpg[j], 1);
}
upgameScore++;
}
else if (evt.target.name == "piece" + j) {
evt.target.x = Math.floor(Math.random()*(1+530-20))+20;//Math.random()*400+50;
evt.target.y = Math.floor(Math.random()*(1+380-20))+20;//Math.random()*50+50;
}
}
scor.text = upgameScore.toString();
if (upgameScore == 10) {
msgbox.text = "Congratulations !";
for (var k:Number = 0; k < numClips; k++) {
myClip[k].removeEventListener("mouseDown", pieceMove);
myClip[k].removeEventListener("mouseUp", pieceMove);
}
myTimer.reset();
myTimer.stop();
scoreWindow.visible = true;
scoreWindowText.visible = true;
addChild(scoreWindow);
addChild(scoreWindowText);
scoreWindowText.text = "Congratulations. You got " + upgameScore + " / 10. \nClick FINISHED to go back or TRY AGAIN to restart.";
}
}
}
I should mention that if you look near the end of the code where I do the testHitObject and then call removeChild after that, THAT particular delete works and removes the object from the frame.
Solved this one too. I should probably spend a bit more time before I post these.
As it turns out, when objects were being matched they were being removed as per the removeChild() function that was working. What I was doing then was iterating through the array and attempting to remove some objects that were already removed. So what i did was kept an array that matched the objects and when they were removed, changed a flag to 0. Then at the end, iterate through the new array and if there's a 1, remove the child object from the array with the same index. If there's a 0, ignore it. Now it works.

accessing array elements in another frame as3

I have created an Array in frame 1 and dynamically create a list of movieclips with it , and I want to create the same Array in frame 3 and those movieclips again if something is true or false.is it possible ? because when I'm trying to do this , I will get this error :
1151: A conflict exists with definition i in namespace internal.
here is my code at frame 1 :
stop();
import flash.display.MovieClip;
var p_x:uint = 90;
var p_y:uint = 108;
var my_list:String = "Games,Videos, Medias,Images,Photos,Personal Photos,Social Media,Private,Social,None,Names,Families";
var myListString:Array = my_list.split(",");
var myArray:Array=new Array ();
var listObject = 1;
for (var i:uint=0; i<12; i++)
{
var myItemList:mrb=new mrb();
myItemList.x = p_x;
myItemList.y = p_y + 80;
myItemList.y = (p_y + (listObject * 65));
myArray.push(myItemList);
myItemList.txt.text = i.toString();
myItemList.txt.text = myListString[i].toString();
myItemList.name = "item"+i;
addChild(myItemList) as MovieClip ;
listObject++;
}
and here is code at frame 3 :
var tmpCurFrame:int = currentFrame;
this.addEventListener(Event.ENTER_FRAME, handleUpdate)
function handleUpdate(e:Event):void {
if (tmpCurFrame != currentFrame) {
this.removeEventListener(Event.ENTER_FRAME, handleUpdate);
return;
}
if (so.data.fav1 != undefined)
{
if ( so.data.fav1 == "on")
{
for (var i:int = 0; i < myListString.length;)
{ if (myListString[i].indexOf() > -1)
{
var myRectangle:mrb=new mrb();
trace("found it at index: " + i);
myRectangle.x = p_x;
myRectangle.y = (p_y + (findobject * 50));
trace('p_y+80 =' + (p_y+(findobject*80)) + 'p_x = ' + p_x );
myArray.push(myRectangle);
myRectangle.txt.text = myListString[i].toString();
trace(my_array2[i].toString() );
addChild(myRectangle);
}
}
}
}
else
{
fav_1.fav_on.visible=true;
}
}
This error message simply means that you use twice the same variable i. You just have to give them differents names.

Resources