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;
}
}
}
}
Related
I want to import an XML file and add it to a grid. That part I have done, but I want to use the class to change the XML file. Now, the file is stuck at the original file.
Example class:
package {
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import flash.net.*;
import flash.events.*;
import fl.controls.DataGrid;
import fl.data.DataProvider;
import flash.display.Stage;
import flash.display.MovieClip
public class Tabellkamp extends MovieClip {
public var link1: String = new String("_blank");
public var request: URLRequest;
public var loader: URLLoader;
public var gridd: DataGrid = new DataGrid();
public function Tabellkamp() {
loader.load(request);
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
var loader: URLLoader = new URLLoader();
loader.load(new URLRequest(link1));
}
function loaderCompleteHandler(event: Event): void {
var teamXML: XML = new XML(loader.data);
var firstCol: DataGridColumn = new DataGridColumn("somthing on the xml");
firstCol.headerText = "first";
gridd.columns = [firstCol];
addChild(gridd);
}
}
}
Main timeline/ other class:
public class Lagegridd extends MovieClip {
public function Lagegridd() {
btn_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event: MouseEvent) {
var newXML: Tabellkamp = new Tabellkamp();
newXML.link1="newxmlfile.xml";
addChild(newXML);
}
Maybe some other problems with the code, but the main question is how to change the url, and then addChild(). Never mind the grid, it is just an example.
I appreciate some help, on how to use the class several times.
Pretty simple. Destroy the existing instance and create a new one. Also, don't declare functions inside other functions.
public class Lagegridd extends MovieClip
{
public function Lagegridd()
{
btn_1.addEventListener(MouseEvent.CLICK, onClick);
}
private var currentGrid:Tabellkamp;
private var gridSource:Array = ["file1.xml", "file2.xml", "file3.xml"];
private function onClick(e:MouseEvent):void
{
// Obtain the first element from the list.
var anUrl:String = gridSource.shift();
changeGrid(anUrl);
}
private function changeGrid(url:String):void
{
if (currentGrid)
{
removeChild(currentGrid);
// Another cleanup routines here, if necessary.
}
currentGrid = new Tabellkamp;
// You need to define this function instead of loading
// data from "link1" inside the object constructor.
currentGrid.loadData(url);
addChild(currentGrid);
}
}
UPD: OOP example.
package
{
import flash.display.Sprite;
import flash.system.System;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;
// Don't use MovieClip if you don't have frames and timelines.
public class LasteinnXML extends Sprite
{
public var url:String;
public var loader:URLLoader;
public var dataProvider:XML;
public function load(path:String):void
{
url = path;
loader = new URLLoader;
loader.addEventListener(Event.COMPLETE, onData);
// Always handle erroneous cases. Last three arguments are there
// because it is wise to not unsubscribe from these events but
// let the garbage collector decide when to destroy them.
loader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
// You don't need to store the request object.
loader.load(new URLRequest(url));
}
private function onData(e:Event):void
{
try
{
dataProvider = new XML(loader.data);
finishLoading();
}
catch (fail:Error)
{
// There's another erroneous case even if loading is fine:
// Invalid XML data. Always chack for it as well.
onError(null);
}
// Handle the SUCCESSFUL case here.
}
private function finishLoading():void
{
// Cleanup routines. Dispose of your objects once you don't need them.
if (!loader) return;
loader.removeEventListener(Event.COMPLETE, onData);
loader = null;
}
private function onError(e:Event):void
{
// In case the object was already destroyed
if (!loader) return;
finishLoading();
// Handle the ERRONEOUS case here.
}
public function destroy():void
{
finishLoading();
// The recommended way of cleaning XML data up.
if (dataProvider)
{
System.disposeXML(dataProvider);
dataProvider = null;
}
}
}
}
Usage:
var A:LasteinnXML = new LasteinnXML;
var B:LasteinnXML = new LasteinnXML;
var C:LasteinnXML = new LasteinnXML;
addChild(A);
addChild(B);
addChild(C);
A.load("filea.xml");
B.load("fileb.xml");
C.load("filec.xml");
C.destroy();
removeChild(C);
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.
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 :)
So I have 2 files, where I want to be able to access an array from 1 file in the other.
package code {
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.ui.Keyboard;
import code.*;
public class Init extends MovieClip {
public var _solidObjects: Array;
public function Init() {
_solidObjects = [wall01, wall02, wall03, wall04];
}
}
}
How would I be able to access the _solidObjects array from another class in a seperate file?
Any help would be appreciated as I have been trying for a while with no success, thanks.
Constructors can be passed variables. For example:
First class:
package code {
public class Init extends MovieClip {
public var solidObjects: Array;
public function Init() {
solidObjects = [wall01, wall02, wall03, wall04];
}
}
Second class:
package code {
public class SomeClass extends MovieClip {
public var solidObjects: Array;
public function SomeClass(param:Array) {
this.solidObjects = param;
}
}
}
Usage context:
var initObj:Init = new Init();
var secondObject:SomeClass = new SomeClass(initObj.solidObjects);
I have a list of buttons:
agreeButton
disagreeButton
container.clickButton1
container.clickButton2
Container is another movieclip and inside of it are the last 2 buttons.
How can I put it in array and have all the same listeners applied to each of them?
var buttonArray:Array = new Array("agreeButton", "disagreeButton", "container.clickButton1", "container.clickButton2");
for (var i:int=0; i<buttonArray.length; i++) {
this[buttonArray[i]].addEventListener(MouseEvent.ROLL_OVER, mouseRollOver);
this[buttonArray[i]].addEventListener(MouseEvent.ROLL_OUT, mouseRollOut);
this[buttonArray[i]].addEventListener(MouseEvent.CLICK, mouseClick);
}
Keep a reference to the buttons and add them to an array.
var agreeButton:Button = new Button();
var disagreeButton:Button = new Button();
//... Code that will add the above instantiated buttons to the canvas
var buttonArray:Array = new Array(agreeButton, disagreeButton);
for (var i:int = 0; i < buttonArray.length; i++) {
buttonArray[i].addEventListener(MouseEvent.CLICK, mouseClick);
}
private function mouseClick(event:MouseEvent):void {
Alert.show("Boom!");
}
I would make a button class that is extended by each of these buttons. In the button class you add eventListeners.
Example:
forgive me if there are things wrong with this, I haven't coded in AS3 for a while.
class MyButton extends Button
{
public function MyButton()
{
this.addEventListener(MouseEvent.ROLL_OVER, mouseRollOver);
this.addEventListener(MouseEvent.ROLL_OUT, mouseRollOut);
this.addEventListener(MouseEvent.CLICK, mouseClick);
}
//... Add mouseRollOver, mouseRollOut, mouseClick methods
}
Your Individual Buttons
class DisagreeButton extends MyButton
{
public function DisagreeButton()
{
}
}
class AgreeButton extends MyButton
{
public function AgreeButton()
{
}
}
Once you instantiate the buttons all the event listeners will be applied to each.