actionscript 3 - using array in multiple classes - arrays

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);

Related

Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin

I am trying to create a Minecraft plugin with a command that will set the world name into config.yml. Except I keep getting "Cannot make a static reference to the non-static method getConfig() from the type JavaPlugin" when I attempt to set the config. I have already searched around for several way to fix this but I have not understood have to implement other situations into mine.
Here is my code:
Main.java:
package me.Liam22840.MurderRun;
import org.bukkit.plugin.java.JavaPlugin;
import me.Liam22840.MurderRun.commands.HelpCommand;
import me.Liam22840.MurderRun.commands.SetmapCommand;
public class Main extends JavaPlugin {
#Override
public void onEnable(){
loadConfig();
new HelpCommand(this);
new SetmapCommand(this);
}
public void loadConfig(){
getConfig().options().copyDefaults(true);
saveConfig();
}
}
SetmapCommand.java:
package me.Liam22840.MurderRun.commands;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import Utils.Utils;
import me.Liam22840.MurderRun.Main;
import me.Liam22840.MurderRun.getConfig;
public class SetmapCommand implements CommandExecutor{
private int count;
public SetmapCommand(Main plugin){
plugin.getCommand("Setmap").setExecutor(this);
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!(sender instanceof Player)){
sender.sendMessage("Only players can execute this command!");
return true;
}
Player p = (Player) sender;
Location b_loc = p.getLocation();
if(p.hasPermission("MurderRun.Setworld")){
Main.getConfig().set("Maps." + p.getName() + count + ".World", b_loc.getWorld().getName());
Main.saveConfig();
p.sendMessage(Utils.chat("&4Map Set"));
return true;
} else{
p.sendMessage("You do not have the required permissions to execute this command!");
}
return false;
}
}
You can't directly call the Main class, because it is not static. To call it, you should do this in your Setmap class and the constructor:
private Main plugin;
public SetmapCommand(Main plugin){
this.plugin = plugin;
plugin.getCommand("Setmap").setExecutor(this);
}
After you did this, you can use in your Setmap class:
plugin.saveConfig();

ActionScript 3.0 - Change Url import from a Class

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);

testng how to dynamically set groups from Factory?

Before I setup a test class like the code below:
1. the Factory and test Dataprovider both used excel as the dataprovider.
2. In the Factory dataprovider table, it has a list of url
3. Each time, it will find one of the url in the factory dataprovider table, and run the test in each test methods..
public class Test {
WebDriver driver;
private String hostName;
private String url;
#Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url) {
this.hostName = hostName;
this.url = url;
}
#BeforeMethod
#Parameters("browser")
public void start(String browser) throws Exception {
driver = new FirefoxDriver();
driver.get(url);
Thread.sleep(1000);
}
#Test(priority = 10, dataProvider = "dataprovider Test A", dataProviderClass = xxx.class)
public void TestA(Variable1,
Variable2,Variable3) throws Exception {
some test here...
}
#Test(priority = 20, dataProvider = "dataprovider Test B", dataProviderClass = xxx.class)
public void TestB(Variable1,
Variable2,Variable3)
throws Exception {
some test here...
}
#AfterMethod
public void tearDown() {
driver.quit();
}
Now I want to dynamically assign different group for each test for different url. I am thinking add a variable 'flag' in the #Factory dataprovider:
#Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url, String flag) {
this.hostName = hostName;
this.url = url;
this.flag = flag;
}
That when flag.equals("A"), it will only run test cases in test groups={"A"}.
When flag.equals("B"), it will only run test cases in test groups ={"B"},
When flag.equals("A,B"), it will only run test cases in test groups ={"A","B"}
Is there any way I can do that?
Thank you!
TestNG groups provides "flexibility in how you partition your tests" but it isn't for conditional test sets. For that you simply use plain old Java.
You can use inheritance or composition (I recommend the latter, see Item 16: Favor composition over inheritance from Effective Java).
Either way the general idea is the same: use a Factory to create your test class instances dynamically creating the appropriate class type with the appropriate test annotations and/or methods that you want to run.
Examples:
Inheritance
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class DemoTest {
#Factory
public static Object[] createTests() {
return new Object[]{
new FlavorATest(),
new FlavorBTest(),
new FlavorABTest()
};
}
/**
* Base test class with code for both A-tests and B-tests.
*
* Note that none of these test methods are annotated as tests so that
* subclasses may pick which ones to annotate.
*/
public static abstract class BaseTest {
protected void testA() {
// test something specific to flavor A
}
protected void testB() {
// test something specific to flavor B
}
}
// extend base but only annotate A-tests
public static class FlavorATest extends BaseTest {
#Test
#Override
public void testA() {
super.testA();
}
}
// extend base but only annotate B-tests
public static class FlavorBTest extends BaseTest {
#Test
#Override
public void testB() {
super.testB();
}
}
// extend base and annotate both A-tests and B-tests
public static class FlavorABTest extends BaseTest {
#Test
#Override
public void testA() {
super.testA();
}
#Test
#Override
public void testB() {
super.testB();
}
}
}
Composition
import org.testng.annotations.Factory;
import org.testng.annotations.Test;
public class DemoTest {
#Factory
public static Object[] createTests() {
return new Object[]{
new FlavorATest(),
new FlavorBTest(),
new FlavorABTest()
};
}
private static void testA() {
// test something specific to flavor A
}
private static void testB() {
// test something specific to flavor B
}
// only create A-test methods and delegate to shared code above
public static class FlavorATest {
#Test
public void testA() {
DemoTest.testA();
}
}
// only create B-test methods and delegate to shared code above
public static class FlavorBTest {
#Test
public void testB() {
DemoTest.testB();
}
}
// create A-test and B-test methods and delegate to shared code above
public static class FlavorABTest {
#Test
public void testA() {
DemoTest.testA();
}
#Test
public void testB() {
DemoTest.testB();
}
}
}
Your factory methods won't be as simple as you'll need to use your "flag" from your test data to switch off of and create instances of the appropriate test classes.

Accessing a Parents Array from child added inside movieClip in AS3

I have a movieClip object(Creature), and inside it, i have hitpoints that I would like to add to Creatures Array HitPoints. The way I would like to do this is to have every Parent that may have hitPoints, to have an array called HitPoints so that I can have each point add it self to the Array, regardless of the parent. IE, a skeleton creature and a goblin creature could use the same type of hitPoint objects inside of their base movieClip and still be accessable.
I have omitted some of the unnecessary parts of this class
package Assets.Creatures
{
import Assets.Points.HitPoint;
import Assets.GameStates.Main;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class BaseAI_ extends MovieClip
{
//Characteristics
protected var Health : int = 10;
protected var SPEED : int = 3;
protected var Intellect : int = 3;
protected var Strength : int = 3;
protected var Dexterity : int = 3;
protected var AttackRange : int = 70;
protected var Temperment : String = "Agressive";
//public var Temperment : String = "Passive";
//public var Temperment : String = "Defensive";
//ARRAYS
public var HitPoints : Array = new Array;
// AttackTimers
protected var canAttack : Boolean = false;
protected var AttackSpeed : int;
protected var AttackTimer : Timer;
protected var AbilityCooldownsUP : Boolean = false;
//protected var ABILITY : Timer;
//AI STATES
protected var CURRENT_STATE : int = 0;
protected static var STATE_IDLE : int = 0;
protected static var STATE_ENGAGE : int = 1;
protected static var STATE_ATTACK : int = 2;
protected static var STATE_FLEE : int = 3;
//Reference
protected var Target : MovieClip;
//Directions
protected var SlopeY : Number;
protected var SlopeX : Number;
protected var EngagePosition : Point;
protected var RunDirection : Number;
public function BaseAI_()
{
addEventListener(Event.ADDED_TO_STAGE, Awake);
} // constructor code
private function Awake (e : Event) : void
{
AttackSpeed = (5000 / Dexterity);
AttackTimer = new Timer (AttackSpeed, 0);
AttackTimer.addEventListener(TimerEvent.TIMER, AttackSpeedTimer);
AttackTimer.start();
Main.main.EnemyArray_2.push(this);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function AddHitPoints (hitPoint : MovieClip) : void
{
HitPoints.push(hitPoint);
}
}
and the hitPoint class is
package Assets.Points
{
import Assets.Creatures.BaseAI_;
import Assets.Creatures.Player_;
import Assets.Creatures.Player_Human;
import flash.display.MovieClip;
import flash.events.Event;
public class HitPoint extends MovieClip
{
public function HitPoint()
{ // constructor code
addEventListener(Event.ADDED_TO_STAGE, Awake);
} // constructor code
private function Awake (e : Event) : void
{
trace("HitPoints parent is " + parent);
parent.AddHitPoints(this);
//parent.HitPoints.push(this)
//trace("my parent has " + parent.HitPoints.length);
}
}
}
So in the child class (hitPoints) I am adding the hitPoints directly to the ParentClass BaseAI_ on the stage, I have tried a few different ways to add HitPoints both directly with parent.AddHitPoints(this); and parent.HitPoints.push(this)
I think you need to use this.parent and not just parent.

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