StageText - How to use more than 1 StageText field? - mobile

I'm using Flash CS 6 and Air 3.6 to build an Android app.
I would like to use StageText for the input fields.
The code below is only working for the first textfield. What do I need change to make it work for both text fields?
// NO. 1
var firstfield:StageText = new StageText();
firstfield.softKeyboardType = SoftKeyboardType.DEFAULT
firstfield.stage = foobar.stage
firstfield.viewPort = new Rectangle(foobar.x, foobar.y, foobar.width, foobar.height);
// NO. 2
var secondfield:StageText = new StageText();
secondfield.softKeyboardType = SoftKeyboardType.DEFAULT
secondfield.stage = example.stage
secondfield.viewPort = new Rectangle(example.x, example.y, example.width, example.height);

Try something like this:
var firstfield:StageText = new StageText();
firstfield.softKeyboardType = SoftKeyboardType.DEFAULT
firstfield.stage = this.stage;
firstfield.viewPort = new Rectangle(0, 0, 200, 30);
firstfield.x = 0;
firstfield.y = 0;
firstfield.width = 200;
firstfield.height = 30;
addChild(firstfield);
var secondfield:StageText = new StageText();
secondfield.softKeyboardType = SoftKeyboardType.DEFAULT
secondfield.stage = this.stage;
secondfield.viewPort = new Rectangle(0, 40, 200, 30);
secondfield.x = 0;
secondfield.y = 40;
secondfield.width = 200;
secondfield.height = 30;
addChild(secondfield);

Related

Filter 2 struct/ arrays by mapping by specific

Im a new user using swift 3 and xcode 8.3. Currently facing a problem to filter 2 array/struc where in console output as below:
A_List : Optional([117, 115, 18])
B_List : Optional([{
URL = "169.jpeg";
categories = "A";
description = "description XXX";
height = 128;
id = 1;
likes = "1.00";
name = "Cake - Baked";
price = "13.78";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "Baked till golden";
height = 128;
id = 15;
likes = "1.00";
name = "Croissant";
price = "3.71";
width = 128;
}, {
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "1622.jpeg";
categories = "A";
description = "A ";
height = 128;
id = 103;
likes = "1.00";
name = "America Pie";
price = "2.12";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
I want to return B_List full info as var filtered_List = [AnyObject]() where only contains of A_List id number 117, 115, and 18 which look like as below:
filtered_List : Optional([{
URL = "11.jpeg";
categories = "A";
description = "description Crispy.";
height = 128;
id = 18;
likes = "1.00";
name = "Plain";
price = "2.65";
width = 128;
}, {
URL = "11.jpeg";
categories = "B";
description = "Puff";
height = 128;
id = 115;
likes = "1.00";
name = "Mini Puff";
price = "2.12";
width = 128;
}, {
URL = "168.jpeg";
categories = "C";
description = "description YYY";
height = 128;
id = 117;
likes = "1.00";
name = "Normal";
price = "3.18";
width = 128;
}])
I have tried few code and read tutorial in youtube, but unfortunately did not find any solution and it limited to swift2 sample.
Currently, my code tried as below:
var filtered_List = [AnyObject]()
let fullrList = B_List?.map{$0["id"] as! String}.map{_ in A_List}
filtered_List.append(fullrList as AnyObject )
print("result :\(filtered_List)")
Very Appreciated if someone expert can guide or give your solution here.
You should store your desired IDs in a Set, rather than an Array. You're just need a simple filter operation:
let desiredIds: Set = [117, 115, 18]
B_List.filter{ $0["id"].map{ desiredIds.contains($0) } ?? false } as [AnyObject]
Thanks for everyone who reply this topic specially to #Alexander. Here my solutions to share that may not the perfect one as others.
var resultAnyObject = [AnyObject]()
var aListIds = [String]()
for i in A_List! {
aListIds.append(i as! String)
}
//#Alexander : thanks for code below:
let desiredIds = aListIds
let fullList = B_List?.filter{ $0["id"].map{desiredIds.contains($0 as! String) } ?? false };
resultAnyObject.append(fullList as AnyObject)
print("Result of filtered_List :\(resultAnyObject)")

Can i display multiple copies of a movieclip inside a array at once

Is there a way to make the code below work properly? When I use this code it only shows one movieclip:
var tempHead:head001 = new head001();
var mcArr:Array = new Array( tempHead );
var firstHead:MovieClip = mcArr[0];
firstHead.y = 30;
addChild(firstHead);
var secondHead:MovieClip = mcArr[0];
secondHead.y = 180;
addChild(secondHead);
`
You were just assigning a reference to the MovieClip. That'y, its not working.
First take instance of head001 class using new operator as much you want and store it to an array, then you can access very easily.
var tempHead: head001;
var mcArr: Array = new Array();
for (var i: uint = 0; i < 2; i++) {
tempHead = new head001();
addChild(tempHead);
mcArr.push(tempHead);
mcArr[i].y = mcArr[i].height * i;
}

AS3 Add array to another array

Example of the my problem.
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
// and the out come I want it to be is this
trace(array_1[0]) // 2,4,6,8,10,12,14,16,20,22,24
// I did try array_1[0] += array_2[0] but it didn't work currently
Any suggestion would be great.
This will perform what you are looking for and also allows you to add multiple rows of data to array_1 or array_2
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:Array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
var combinedArray:Array = new Array();
for( var i:int = 0; i < array_1.length; i++ ) {
combinedArray = combinedArray.concat(array_1[i]);
}
for( i = 0; i < array_2.length; i++ ) {
combinedArray = combinedArray.concat(array_2[i]);
}
trace(combinedArray);
As stated in the comments, you can use the concat method:
var array_1:Array = new Array();
array_1[0] = [2,4,6,8];
var array_2:array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];
array_1[0] = array_1[0].concat(array_2[0]).concat(array_2[1]);
This, of course, is very messy looking. I am wondering why you are storing arrays inside of other arrays for no discernible reason.

Actionscript 3, Flash CC: Placing Objects In An Array From The Library Onto The Stage

Hello programming gurus of stackoverflow, I am hoping that at least one of you will be able to help me with my coding problem. This is the first time I'm posting on this site, so if I miss something with the structure of my post, or anything please let me know (preferably not in a condescending matter) and I will gladly change it.
I actually had a different problem I was going to ask about, but I recently realized that some objects from my library weren't showing up on my stage. Hopefully, if this gets solved I won't have my other problem.
I am creating a learning module app using Flash CC and Actionscript 3, I like to think I am fairly proficient with Flash, but right now all my code is on the timeline because when I started I wasn't aware of the package setup. When I finish with the learning module I'll try and move everything to an AS package, so please bear with me.
This current frame of the module is a drag and drop game where the user drags the correct food, for the animal they chose in the previous frame, to the animal in the middle. The animal is dynamically placed on the stage, as well as an array of six possible food choices, all MovieClips pulled from the library. The array of food elements is actually not what I'm having problem with, they appear on my stage with no problems at all. The problem I'm having is when the user drags the correct food onto the animal, and the win condition is met, the array of balloon elements does not show up on the stage. I find it weird because I'm using near identical code for both the food and balloon array.
Here is my full code:
import flash.display.MovieClip;
import flash.events.MouseEvent;
foodPet();
function foodPet():void {
//all of my pet, food, and balloon library objects have been exported for AS
var theBird:pet_bird = new pet_bird;
var theCat:pet_cat = new pet_cat;
var theChicken:pet_chicken = new pet_chicken;
var theDog:pet_dog = new pet_dog;
var theDuck:pet_duck = new pet_duck;
var theGuinea:pet_guinea = new pet_guinea;
var theHamster:pet_hamster = new pet_hamster;
var birdSeed:food_bird_seed = new food_bird_seed;
var catFood:food_cat_food = new food_cat_food;
var chickenFeed:food_chicken_feed = new food_chicken_feed;
var chocolate:food_chocolate = new food_chocolate;
var dogFood:food_dog_food = new food_dog_food;
var duckFood:food_duck_food = new food_duck_food;
var animalList:Array = [theBird, theCat, theChicken, theDog,
theDuck, theGuinea, theHamster];
var food1Array:Array = [birdSeed, catFood, chickenFeed,
chocolate, dogFood, duckFood, 4];
var xPosFood:Array = new Array();
var yPosFood:Array = new Array();
xPosFood = [32, 71, 146, 363, 431, 512];
yPosFood = [304, 222, 123, 123, 222, 304];
var animalClip:MovieClip;
animalClip = animalList[chosenAnimal];
addChild(animalClip);
animalClip.x = 256;
animalClip.y = 287;
animalClip.name = "selectedAnimal";
for (var i:uint = 0; i < food1Array.length - 1; i++){ //Where the food gets added
var isItRight:Boolean = false;
var foodName:String = ("food" + i);
var foodClip:MovieClip;
foodClip = food1Array[i];
foodClip.x = xPosFood[i];
foodClip.y = yPosFood[i];
foodClip.name = foodName;
addChild(foodClip);
trace(foodClip.parent);
foodDragSetup(foodClip, animalClip, food1Array[food1Array.length - 1], isItRight);
}
}
function foodDragSetup(clip:MovieClip, targ:MovieClip, correctNum:uint, isItRight:Boolean) {
var beingDragged:Boolean = false;
var xPos:Number = clip.x;
var yPos:Number = clip.y;
clip.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
function beginDrag(event:MouseEvent):void
{
clip.startDrag();
if (int(clip.name.substr(4)) == correctNum){
isItRight = true;
}
this.beingDragged = true;
setChildIndex(clip, numChildren - 1);
clip.addEventListener(MouseEvent.MOUSE_UP, endDrag);
}
function endDrag(event:MouseEvent):void
{
if (this.beingDragged) {
this.beingDragged = false;
clip.stopDrag();
if ((isItRight) && (clip.hitTestPoint(targ.x, targ.y, true))){
trace(targ.name + " has been hit.");
clip.x = targ.x;
clip.y = targ.y;
win_animal_food();
} else {
isItRight = false;
clip.x = xPos;
clip.y = yPos;
}
}
}
}
function win_animal_food():void {
const BALLOON_ROW:int = 4;
var count:uint = 0;
var altX:uint = 0;
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
var balloonY:Array = [144, -205, -265, -325];
var balloonX:Array = [0, 140, 284, 428, 68, 212, 356, 500];
for (var ballY:uint = 0; ballY < balloonY.length; ballY++){ //Where balloons
for (var ballX:uint = altX; ballX < altX + BALLOON_ROW; ballX++){ //get added
var balloonName:String = ("balloon" + count);
var balloonClip:MovieClip;
balloonClip = balloonList[count];
balloonClip.x = balloonX[ballX];
balloonClip.y = balloonY[ballY];
balloonClip.name = balloonName;
addChild(balloonClip);
trace(balloonClip.parent);
trace(balloonClip + " has been added!");
balloonClip.addEventListener(MouseEvent.CLICK, balloonPop);
count++;
}
if (altX == 0) {
altX = BALLOON_ROW;
} else {
altX = 0;
}
}
function balloonPop(event:MouseEvent):void {
event.target.play();
event.target.removeEventListener(MouseEvent.CLICK, balloonPop);
}
}
I thought there might have been a problem with my balloon MovieClips, so I subbed them in the food array:
var birdSeed:blue_balloon = new blue_balloon;
var catFood:green_balloon = new green_balloon;
var chickenFeed:orange_balloon = new orange_balloon;
var chocolate:purple_balloon = new purple_balloon;
var dogFood:red_balloon = new red_balloon;
var duckFood:yellow_balloon = new yellow_balloon;
They all showed up on the stage, so there's nothing wrong with the MovieClips.
Added: The first values of balloonXArray and balloonYArray were originally -4 and -145 respectively, but when I started having problems I wanted to make sure the balloons were showing up so I set the first values to 0 and 144 the balloon height and width are both 144 and their cross (not sure on it's name) is in the top left corner.
Added: The reason why there are multiple instances of the same balloon in the balloonList is because I need four rows of four balloons, but only have six different balloons.
I know the balloons are on the stage because the debug display shows their x and y values on the viewable stage. Using trace(foodClip.parent) and trace(balloonClip.parent) shows that the balloons and food all have the same parent, MainTimeline, so I know the balloons aren't getting added to some different space.
I have searched online, but have not come across anyone with a similar problem. Thus, I am asking on this forum if anyone can tell me why my balloons will not show up on the stage.
Please and thank you.
One thing I see straight off in the baloonList is that you have the same object instances listed multiple times. Each instance can only exist on stage exactly once. If you addChild() an instance that is already on stage, the instance is first removed, then re-added at the top of the display list.
You should change:
var bBalloon:blue_balloon = new blue_balloon;
var gBalloon:green_balloon = new green_balloon;
var oBalloon:orange_balloon = new orange_balloon;
var pBalloon:purple_balloon = new purple_balloon;
var rBalloon:red_balloon = new red_balloon;
var yBalloon:yellow_balloon = new yellow_balloon;
var balloonList:Array = [bBalloon, gBalloon, oBalloon,
pBalloon, rBalloon, yBalloon, bBalloon, gBalloon,
oBalloon, pBalloon, rBalloon, yBalloon, bBalloon,
gBalloon, oBalloon, pBalloon];
to:
var balloonList:Array = [
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon,
new red_balloon,
new yellow_balloon,
new blue_balloon,
new blue_balloon,
new green_balloon,
new orange_balloon,
new purple_balloon
];

Search Multidimensional Array ActionScript 3

If I have two arrays like the example below, how can I search the first part of each node in the 'score' array with the values in the 'search' array and return the value that is the second part of each node in the 'score' array? Basically in this case I'd want to get 5 and 7.
var score:Array = new Array();
score[0] = ["cat", "3"];
score[1] = ["dog", "5"];
score[2] = ["fish", "0.5"];
score[3] = ["bird", "0.25"];
score[4] = ["horse", "10"];
score[5] = ["cow", "15"];
score[6] = ["iguana", "7"];
var search:Array = ["dog", "iguana"];
Try with this code (using Dictionary):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private var score:Dictionary = new Dictionary();
private var search:Array = ["dog", "iguana"];
public function init():void{
score["cat"] = "3";
score["dog"] = "5";
score["fish"] = "0.5";
score["bird"] = "0.25";
score["horse"] = "10";
score["cow"] = "15";
score["iguana"] = "7";
var t1:String = score[search[1]];
var t2:String = score[search[0]];
Alert.show(t1 + ' ' + t2); //prints 5 7
}
]]>
</mx:Script>
</mx:Application>
Or you can do this too:
var t1:String = score["dog"];
var t2:String = score["iguana"];
Or without Dictionary:
<?xml version="1.0" encoding="utf-8"?>
public function init():void{
score[0] = ["cat","3"];
score[1] = ["dog","5"];
score[2] = ["fish","0.5"];
score[3] = ["bird","0.25"];
score[4] = ["horse","10"];
score[5] = ["cow","15"];
score[6] = ["iguana","7"];
for(var j:int = 0;j<search.length;j++){
for(var i:int = 0;i<score.length;i++){
var temp:Array = score[i] as Array;
if(temp[0] == search[j]){
Alert.show(temp[1]);
}
}
}
}
]]>
</mx:Script>
Also you have not used an appropriate way to declare arrays, I recommend using my method or changing the array declaration.

Resources