Determining the movieclip assigned to a variable in an array AS3 - arrays

Please forgive my terminology, Im not educated on the proper.
Lets say I have multiple movieclip variables
var rblock1:MovieClip = new Rblock();
var rblock2:MovieClip = new Rblock();
var rblock3:MovieClip = new Rblock();
var yblock1:MovieClip = new Yblock();
var yblock2:MovieClip = new Yblock();
var yblock3:MovieClip = new Yblock();
I have them added to an array
var blockarray:Array = new Array(rblock1, rblock2, rblock3, yblock1, yblock2, yblock3);
var block
I want to create a for loop with an if statement that triggers if a variable is Rblock and not Yblock, for example
for each (block in blockarray)
{
if (block==Rblock)
{
trace("rblock");
}
}
The issue is that obviously "if (block==Rblock)" doesnt work.
How should this be written?

You apparently want to check if a block is red or yellow by checking against its class name. You can do it with this:
if (block is Rblock) {...} // yes, red

I have figured out a work around not really a perfect solution, which will only work for certain scenarios...
if each class has a unique trait you can identify it that way, for example...
if all variables defined by the Rblock class are wider than the Yblock class you could say
if (block.width>x) { trace(Rblock); }
Like I said this is only a work around though and only works for movieclip variables defined by classes that are different, if anyone has the actual solution please post

Related

What is the best and quickest way to save a large Class Array in existing project? Realm isn't working

First off, I'm a relative amateur with app design having taught myself high-level swift/xcode last year, so apologies for my code in advance!
I've developed a game which has an array of a 'Player' Class called playerList. I currently convert this playerList array to JSON via Encoder and then save to device...however as my array grows, this exercise is beginning to take a long time, so I'm looking for an alternative. I presume the best solution is to rewrite the app to use CoreDate, SQLite etc, but I'm looking for a quick solution for now.
I could have used userDefaults, however steered away from this as large array and am instead trying to fudge a solution using Realm.
I've attempted the below, but whenever I look at my playerList after loading it is empty. Am I missing something obvious here, or alternatively is there a much better approach than using Realm?
class PlayerArray: Object {
var iden: Int = 0
var allThePlayers: [Player] = playerList
}
func saveViaRealm() {
// Get the default Realm
let realm = try! Realm()
// Define player list
let realmPlayerList = PlayerArray()
realmPlayerList.allThePlayers = playerList
realmPlayerList.iden = 1
// Write to realm
try! realm.write {
realm.add(realmPlayerList)
}
}
func loadViaRealm() {
// Get the default Realm
let realm = try! Realm()
//Retrieve objects from realm
let realmOutputPlayerList = realm.objects(PlayerArray.self)
// Filter to iden required
let realmFiltered = realmOutputPlayerList.filter{$0.iden == 1}[0]
// Assign to playerList
playerList = realmFiltered.allThePlayers
}
I would take a read through the Realm documentation once more around Lists and declaring variables. In your object class are you getting any errors? RealmSwift should be declared with #objc dynamic vars. Also, you shouldn't need but one let = realm. Here is the link to Realm.io documentation.

Is it possible to apply changes to all objects in Array without using "for each" or "for" in Swift 3?

for example
var imageViewArray:UIImageView = [imageView1,imageView2,imageView3]
I want to chage sameimageView.image = img or imageView.isUserInteractionEnabled = false to all Image View inside the array
1. It's an array
First of all it's not
var imageViewArray:UIImageView
but
var imageViewArray:[UIImageView]
because you want an array of UIImageView right?
2. Naming conventions
Secondly is Swift we don't name a variable after it's type so imageViewArray becomes imageViews.
3. map
Now if you really hate the for in and the foreach your can write
imageViews = imageViews.map { imageView in
imageView.isUserInteractionEnabled = true
return imageView
}
or as suggested by Bohdan Ivanov in the comments
imageViews.map { $0.isUserInteractionEnabled = true }
4. Wrap up
This answer shows you how to use the wrong construct (map) to do something that should be made with the right construct (for in).
That's the point of having several constructs, everything could be made with an IF THEN and a GOTO. But a good code uses the construct that best fits that specific scenario.
So, the best solution for this scenario is absolutely the for in or the for each
imageViews.forEach { $0.isUserInteractionEnabled = true }

AS3 Copy multidimensional arrays/Vector

I have two Vectors one called "SET_grid" which should never me changed and one called "tmp_grid" which can, but how do i copy SET_grid to tmp_grid without binding it to the original, so if tmp_grid change then the SET_gird doesn't, these Vectors are both multidimensional e.g.
public var tmp_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
public var SET_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
so i would use them like this....
tmp_grid[x][y].sayhello();
tmp_grid = SET_grid does not work
tmp_grid = SET_grid.concat(); // nor does this one
Any help would be great
Nested arrays cannot be cloned without iterations. It's because they're nested :)
What this means is that you have to use nested loops and push to second vectors..

Code Igniter: Access array elements from controller

I have written this function in CI and for various reasons, I need to assign the contents of the array to variables to use later in the controller.
Because of the way this legacy code is set up, I need to get to the elements of the array from the controller. How do I get to the array elements in $data['oneResult'] from function below. I have tried a few things like the element() helper. Nothing works. In debug mode, I see the data I need and at this point, I need to assign so I want to do this:
$holdID = $data['oneResult']['contact_id'];
$holdLoc = $data['oneResult']['location']; etc.
public function getOneValue(){
$this->load->model('get_contents');
$data['oneResult'] = $this->get_contents->getSpecificRow();
$data['title'] = 'One Record - Contacts table view';
$this->load->view('contacts_view', $data);
}
I am testing this in CI 2, but will need it to work in CI 1.7
Can anyone show me how to do this please?
make a global variablw and assign the data to it..
class something extend CI_controller{
var $holdID ='';
var $holdLoc = ''; etc.
public function __construct() {
....
}
public function index(){
...
}
public function getOneValue(){
$this->load->model('get_contents');
$data['oneResult'] = $this->get_contents->getSpecificRow();
$this->holdID = $data['oneResult']['contact_id']; //<----here assing value to global var
$this->holdLoc = $data['oneResult']['location']; //<--here
$data['title'] = 'One Record - Contacts table view';
$this->load->view('contacts_view', $data);
}
}
I suppose you want the data to be available the next time you are visiting the controller.
In that case, global variables won't hold the value because of the architecture and functioning of codeigniter.
Here are possible ways, that i can suggest:
1) declare session variables and use.(not a very efficient one)
2) declare variable in config file.(preferred and might just do the trick for you).
If either of the above two does not resolve your issue, elaborate a little on your use-case.
Will give it a fresh attempt.
-- seekers01

Accessing instance properties inside of an array

I've imported several images into an actionScript 3 document. I've turned them all into symbols (movie clips) and given them instance names to reference from ActionScript.
Ok, so I'm putting the instances into an array so I can loop through them easily, but for some reason, whenever I'm putting in the instance name, I do a trace on the value in the array and it's giving me the symbol object back, rather than the instance object.
Basically trying to loop through the array to make each instance's visibility = false
Here's a sample:
var large_cap_extrusion_data: Array = new Array();
large_cap_extrusion_data[0] = large_cap_extrusion_menu_button;
large_cap_extrusion_data[1] = extrusion_border_large_cap
large_cap_extrusion_data[2] = "Large Cap";
large_cap_extrusion_data[3] = large_cap_main_menu_button;
var extrusion_data: Array = new Array();
extrusion_data[0] = large_cap_extrusion_data;
trace(extrusion_data[0][0]);
The traces gives:
[object large_cap_menu_button]
(the parent symbol)
rather than:
"large_cap_extrusion_menu_button"
I'd be very grateful if someone could tell me where I'm going wrong...
when you trace and object, by default it describes it type. What you want is the "name" property of the object.
Try this:
trace(extrusion_data[0][0].name);
that should give you the instance nema of the large_cap_menu_button rather than the class description. Either way, you have the right object I bet.

Resources