Action Script 3. Assign Movie Clips to other Movie Clips - arrays

So I'm creating game where will be shown world map with active countries as buttons and in top of screen should randomly pop-up product which should be assigned to specific country. Idea of game is that player should answer from which country this product Is, If answer correct should pop-up other product with assigned country.
So I have to create each country and products as MovieClips, just how to assign to each products correct country? If country clicked It should check If products correct.
I need to put all countries to one array and products to other array? Just I'm confused how to make that checking.
package
{
Import Object1;
Import Object2;
//all objects...
Import Canada;
//all countries...
public class MapGame extends MovieClip
{
private var object1:Object1;
private var object2:Object2;
private var object3:Object3;
private var object4:Object4;
private var object5:Object5;
//etc.....
private var canada:Canada;
private var lithuania:Lithuania;
private var uk:UK;
private var italy:Italy;
//etc.....
private function CreateMap()
{
//Here should be code to add countries as buttons
}
private function SpawnProduct()
{
//Here should be code to spawn random Object
}
private function CheckProducts()
{
// Here should be checking If products correct.
}
Also should be ability to assign multiple products to one country.

I will try to help you.
Firstly let's create two Object's.
private var _countries:Object = {};
private var _objects:Object = {};
_countries assigns an objects to the country. Example:
_countries[USA] = [object1, object2, object3];
So, USA contains three objects.
Same for the object:
_objects[object1] = [USA, Italy, UK];
Next. This is a function that assigns an objects to the country and vice versa.
public function assignCountryAndObject(country:MovieClip, object:MovieClip):void
{
if (_countries[country] is Array && _countries[country].indexOf(object) == -1)
_countries[country].push(object);
else
_countries[country] = [object];
if (_objects[object] is Array && _objects[object].indexOf(country) == -1)
_objects[object].push(country);
else
_objects[object] = [country];
}
So you can call this method this way:
assignCountryAndObject(country1, obj3);
assignCountryAndObject(country2, obj2);
assignCountryAndObject(country2, obj4);
assignCountryAndObject(country2, obj2);
assignCountryAndObject(country3, obj4);
assignCountryAndObject(country3, obj1);
assignCountryAndObject(country3, obj2);
Next two functions are used to get all countries of an object or all objects of the country.
public function getCountriesOfObject(object:MovieClip):Array
{
return _objects[object];
}
public function getObjectsOfCountry(country:MovieClip):Array
{
return _countries[country];
}
For checking you can use next functions:
This function checks is object belongs to the country:
public function isObjectBelongsCountry(object:MovieClip, country:MovieClip):Boolean
{
return _objects[object].indexOf(country) != -1;
}
This function checks is country belongs to an object:
public function isCountryBelongsObject(country:MovieClip, object:MovieClip):Boolean
{
return _countries[country].indexOf(object) != -1;
}

I would suggest using one array of items with all necessary information about countries and their products. Each element of that array should hold country name and array of products associated with that country. This is an example of what I'm talking about:
package
{
import flash.display.MovieClip;
public class Main extends MovieClip
{
//Array that holds a country name and the array of products associated woth that country
//(each item is an instance of Item.as)
private var allItems:Array;
public function Main()
{
this.allItems = new Array();
//Create all items
var item1:Item = new Item("Italy", ["Pizza", "Pasta"]);
var item2:Item = new Item("Lithuania", ["Šaltibarsčiai"]);
//Push those items into main allItems array
this.allItems.push(item1, item2);
//This traces all information in allItems array
for(var i:int = 0; i < allItems.length; i++)
{
trace("Country:", allItems[i].country + ", " + "Products:", allItems[i].products);
}
}
}
}
And the Item.as class:
package
{
import flash.display.MovieClip;
public class Item
{
public var country:String;
public var products:Array;
public function Item(country:String, products:Array)
{
this.country = country;
this.products = products;
}
}
}
I should also add that country and products doesn't necessarily have to be String's. They can be anything you want, like say MovieClips.
Linkėjimai :)

Related

Storing objects in array (Haxe)

How would I go about sorting an new instance of an object into an array in Haxe?
For example I have a class called weapon and in the player class I gave an array inventory.
So how would I store this?
private void gun:Weapon
gun = new Weapon; //into the array
I think you are looking for this:
private var inventory:Array<Weapon>;
This is an array of type Weapon.
To add stuff to it use push(), as seen in this example:
class Test {
static function main() new Test();
// create new array
private var inventory:Array<Weapon> = [];
public function new() {
var weapon1 = new Weapon("minigun");
inventory.push(weapon1);
var weapon2 = new Weapon("rocket");
inventory.push(weapon2);
trace('inventory has ${inventory.length} weapons!');
trace('inventory:', inventory);
}
}
class Weapon {
public var name:String;
public function new(name:String) {
this.name = name;
}
}
Demo: http://try.haxe.org/#815bD
Found the answer must be writen like this
private var inventory:Array;
With Weapon being the class name.

How to refresh Context in Entity framework

I cannot get updated item in ListView after modifying existing database item. Though, once I reload the application one can see updated item in ListView.
I have binded to an ObservableCollection for the ListView
This is my interface
public interface IService
{
IEnumerable<Employee> GetDetails();
IEnumerable<Employee> GetDetailsById(int MatchID);
}
I have implemented IService IEmployeeServiceData class.
public class IEmployeeServiceData:IService
{
private EmployeeContext Context
{
get;
set;
}
public IEmployeeServiceData()
{
Context = new EmployeeContext();
}
public IEnumerable<Model.Employee> GetDetails()
{
return Context.Employees;
}
public IEnumerable<Model.Employee> GetDetailsById(int MatchID)
{
var q = from f in Context.Employees
where f.EMPLOYEE_ID == MatchID
select f;
return q.AsEnumerable();
}
}
This is my VM
public void RefereshData()
{
var e = EmployeeService.GetDetails();
if (SelectedIndexValue == 1)
{
var Data = from e1 in e
where e1.LOCATION == "Pune"
select e1;
EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
}
else if(SelectedIndexValue==2)
{
var Data = from e1 in e
where e1.LOCATION == "Bangalore"
select e1;
EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
}
else
{
EmployeeMasterData = new ObservableCollection<Model.Employee>(e);
}
}
Updating Exiting Item:
public void UpdateEmployee()
{
try
{
Context = new EmployeeContext();
Employee Emp = Context.Employees.First(i => i.EMPLOYEE_ID == FindId1);
Emp.FIRST_NAME = this.FIRSTNAME;
Emp.FAMILY_NAME = this.FAMILY_NAME;
Emp.EXTERNAL_ID = this.EXTERNALID;
Emp.DB_SPONSOR = this.DBSPONSOR;
Emp.LEGAL_ENTITY = this.LEGAL_ENTITY;
Emp.COST_CENTER = this.COST_CENTER1;
Emp.STATUS = this.StatusCategory;
Emp.ENTRY_DATE = this.ENTRYDATE;
Emp.LOCATION = this.LocationCategory1;
Context.SaveChanges();
Clear();
AlertMessage1 = "Employee Record is Updated Sucessfulyy !!!";
IsVisible1 = true;
timer.Start();
}
catch(Exception ex)
{
Console.WriteLine(ex.InnerException);
}
}
Existing Item
Updated Item
Changes done to an entity in Entity Framework will not be reflected on screen because the two instances are not related in your example. Yes the have the same values, but they are two different distinct reference locations in memory. **For the ObservableCollection is a copy of the list and not the actual list being manipulated in your example.
Hence they are not related.
To show a change you have these options:
Change the actual object's property(ies) held by the observable collection to mirror the change done in to the other EF entity. Also the EF entity must adhere to INotifyPropertyChange or also the data property change won't be seen on the screen.
Or delete the screen entity and add the changed entity into the list.
Or delete the whole observable list and re-add it with the latest version from EF. (You mention that you do this and that is an option as well).

Finding objects index in a multidimensional array

Im making an inventory and Im adding stacks but ive hit an issue
below is what I want compared to what works
I just want to find the index of the object I pass through
myArray[0] = [item:object,StackAmmount:int]
var myArray:Array = new Array();
myArray[0] = ["name1",1];
myArray[1] = ["name2",1];
myArray[2] = ["name3",1];
trace("Name" , myArray[0][0]);
//traces "name1"
trace("Stack" , myArray[0][1]);
//traces "1"
trace("Index of Object" , myArray.indexOf("name2"));
//traces -1
// Not Working (NOT FOUND)
//How can I find the index of "item1" or "item2" in the above example
var myOtherArray:Array = new Array();
myOtherArray[0] = "name1";
myOtherArray[1] = "name2";
myOtherArray[2] = "name3";
trace("Name" , myOtherArray[0]);
//traces "name1"
trace("Index of Object" , myOtherArray.indexOf("name2"));
//traces 1
//Working
perhaps there is a better way of dealing with stacks?
Paste Bin Link: http://pastebin.com/CQZWFmST
I would use a custom class, therefore a 1D vector would be enough. The class would contain the name of the item, and the stack. You could subclass this class to override the maxStack variable of the item, and then the searches would be easier aswell, you could just iterate through the vector and check the name.
public class InventoryItem
{
protected var _name:String;
protected var _stack:int;
protected var _maxStack:int;
public function InventoryItem():void {
}
public function get name():String {
return _name;
}
public function get stack():int {
return _stack;
}
public function set stack(value:int):void {
_stack = value;
}
public function get maxStack():int {
return _maxStack;
}
}
...
public class InventoryWeapon extends InventoryItem
{
public function InventoryWeapon(__name:String, startStack:int) {
_maxStack = 64;
_name = __name;
_stack = startStack;
}
}

actionscript 3 load URL from array

I'm making a mp3/steaming radio with MVC, where I'm trying to load an URL from a array.
I have a radio class:
public class Radio
{
private var titel:String;
private var url:URLRequest;
private var cover:Bitmap;
public function Radio(titel:String, url:URLRequest, cover:Bitmap)
{
this.titel = titel;
this.url = url;
this.cover = cover;
}
public function getTitel():String {
return titel;
}
public function getURL():URLRequest {
return url;
}
public function getCover():Bitmap {
return cover
}
}
In the controller i have this:
public function selectRadio(radio:Radio):void{
model.selectRadio(radio);
}
In view I have the button with the eventlistner:
radio.addEventListener(MouseEvent.CLICK, function():void {
controller.selectRadio(model.getRadio(0));
});
And finally in the model i have:
private var radio:Radio = new Radio("P3", new URLRequest("http://live-icy.gss.dr.dk:80/A/A05L.mp3"), drp3);
private var radioArray:Array = new Array(radio);
private var r:Number;
public function selectRadio(radio:Radio):void {
var s:Sound = new Sound();
var chan:SoundChannel = s.play();
s.load();
trace("radio");
}
public function getRadios():Array {
return radioArray;
trace("All radio channels collected");
}
public function getRadio(radioNumber:int):Radio {
r = radioNumber;
return radioArray[radioNumber];
trace("Actual radio collected");
}
The problem is in the selectRadio function. I don't know how to load the URL in the arrays. It should be s.load(--something in here--); The reason why I'm doing this, is because I want to have multiple radio stations.
Hope you can help :)
var s:Sound = new Sound(radio.getURL());
var chan:SoundChannel = s.play();
load function will be called automatically by the constructor, also don't forget to stop previous SoundChannel.
and here: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html you will find everything about Sound

Button and String incompatibility from an Array in AS3

So I have an inventory with items and the array has the instance name of the items, which are movieclips. I want to make it so that all the items will have their button mode become true.
Everything works up to i.buttonMode = true. I get this:1119: Access of possibly undefined property buttonMode through a reference with static type String. But if I use the instance name, something like Inv_1.buttonMode = true works.
So the main question is I guess, how can you iterate through an array and make each of the instance names into buttons?
(I also tried getChildByName.(i).buttonMode = true;) and that didn't work. :S
package {
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip {
var Inventory:Array = ["Inv_1", "Inv_2", "Inv_3", "Inv_4t", "Inv_5"];
public function Drag():void {
for (var i:String in Inventory){
i.buttonMode = true;
}
}
}
}
Your Inventory array is a collection of strings, not MovieClips.
If those are instance names of child display objects, implement getChildByName as a function, not dot notation.
Also note getChildByName returns DisplayObject, which does not define buttonMode. Cast the object as MovieClip or appropriate type.
package {
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip {
var Inventory:Array = ["Inv_1", "Inv_2", "Inv_3", "Inv_4t", "Inv_5"];
public function Drag():void {
for (var i:String in Inventory) {
MovieClip(getChildByName(i)).buttonMode = true;
}
}
}
}
you have created an array of strings, not movie clip instances.
declare your instance names and add them to a vector:
package
{
import flash.display.*;
import flash.events.*;
public dynamic class Drag extends MovieClip
{
private var Inv_1:MovieClip;
private var Inv_2:MovieClip;
private var Inv_3:MovieClip;
private var Inv_4:MovieClip;
private var Inv_5:MovieClip;
public function Drag():void
{
var Inventory:Vector.<MovieClip> = new <MovieClip>[Inv_1, Inv_2, Inv_3, Inv_4t, Inv_5];
for (var i:MovieClip in Inventory)
{
i.buttonMode = true;
}
}
}
}

Resources