I keep looking this over the internet but I think, as far as I have searched, nothing yet had been posted (if any, please give me some links). Is this a way to make an instance this way. When I tried, there's a compilation error, a syntax error ("expecting identifier before this"). I'm happy to receive help from you guys.
Here's my code.
var mc_Names:Array = [];
function createMovieClip(index:int):void{
var nameOfMc:String = "mc_" + index;
mc_Names[index] = nameOfMc;
var this[mc_Names[index]]:MovieClip = new MovieClip(); **// this is what I'm asking if it is possible**
this[mc_Names[index]].graphics.lineStyle(20,0x00FF00, 0.5);
this[mc_Names[index]].graphics.moveTo(square_mc.x,square_mc.y);
this[mc_Names[index]].graphics.lineTo(mc3.x, mc3.y);
this[mc_Names[index]].x = 0;
this[mc_Names[index]].y = 0;
addChildAt(this[mc_Names[index]], 0);
currentIndex++;
}
Or is there any way to make it more simple or another way of declaring variable instances through this[] to make a dynamic creation of those movieclip instances.
var index:int = 1;
var A:Array = new Array();
A[index] = "mc_1";
A[index] = new MovieClip();
A[index].graphics.lineStyle(20,0x00FF00, 0.5);
A[index].graphics.moveTo(0,0);
A[index].graphics.lineTo(100, 100);
A[index].x = 0;
A[index].y = 0;
addChildAt(A[index], 0);
//it is possible to do this way. Direct substitution... :D
Related
is it possible to dynamically access variables? The code below demonstrates what I mean.
for (var i = 0; i < dataList.length; i++) {
var product1 = 20;
var product2 = 30;
product$i = 30;
}
No, that is not possible in Dart and is also not what you really want to do. Instead, you should use List or Map to contain your values if you need to access them in some dynamic way. By doing so, you can achieve statically type safety since Dart will be able to check the types before even running your program.
Im not sure if its possible or not... But you could do something like this
var product= new Map()
for (var i = 0; i < dataList.length; i++) {
var product1 = 20;
var product2 = 30;
product[i] = 30;
}
I'm new to Geb and fairly new in Java. I ask my self if its possible to call multiple methods through a loop. For example this part:
homePage.file1 = Content.Upload()
isDisplayed(homePage.clear1, true)
homePage.file2 = Content.Upload()
isDisplayed(homePage.clear2, true)
homePage.file3 = Content.Upload()
isDisplayed(homePage.clear3, true)
I had the idea to call this through a loop cause the names are very similar to each other. Only the numbers are different.
So I thought about something like this:
String[] elements = { "file1", "file2","file3"}
for( int i = 0; i <= elements.length - 1; i++){
homePage.elements[i] = Generator.fileUpload()
}
But this won't work. Is there any other way to get this to work?
Greetings
Think this is what you're trying to achieve?:
def elements = ["file1", "file2","file3"]
elements.each {
homePage."${it}" = Generator.fileUpload()
}
I'm trying to create a purely JXA-ObjC approach to getting pixel colors from image paths. Here's what I have currently:
ObjC.import('Foundation')
ObjC.import('AppKit')
var c_filePath = $(picturePath)
var c_img = $.NSImage.alloc.initWithContentsOfFile(c_filePath)
if(c_img==$()){
return []
}
var c_point = $.NSMakePoint(x,y)
c_img.lockFocus() //Error - Undefined is not a function...?
var c_color = NSReadPixel(c_point)
c_img.unlockFocus() //Error - Undefined is not a function...?
c_img.release()
var r; var g; var b; var a
c_img.getRegGreenBlueAlpha($(r),$(g),$(b),$(a))
r = ObjC.unwrap(r)
g = ObjC.unwrap(g)
b = ObjC.unwrap(b)
a = ObjC.unwrap(a)
This code is heavily based off of the code found here.
However, as shown above c_img.lockFocus() is undefined according to JXA. Oddly I can get access to c_img.lockFocusFlipped(), however I'm not sure how to use this and/or if it can be used for the same purpose as lockFocus().
Is there an obvious problem here? Or is there a better way to get the pixel colour of an image?
Any help would be grateful.
It looks like I am too used to methods requiring parenthesis. TylerGaw however told me that this is not necessarily the case.
ObjC.import('Foundation')
ObjC.import('AppKit')
var c_filePath = $(picturePath)
var c_img = $.NSImage.alloc.initWithContentsOfFile(c_filePath)
if(c_img==$()){
return []
}
var c_point = $.NSMakePoint(x,y)
c_img.lockFocus
var c_color = NSReadPixel(c_point)
c_img.unlockFocus
c_img.release
appears to work as expected.
I know this question has been asked a bunch of times, but none of the answers (or at least what i took away from them) was a help to my particiular problem.
I want to dynamically change a part of the variable path, so i don't have to repeat the same code x-times with just two characters changing.
Here's what i got:
In the beginning of my script, i'm setting the reference to PlayerData scripts, attached to the GameManager object like this:
var P1 : P1_Data;
var P2 : P2_Data;
function Start(){
P1 = GameObject.Find("GameManager").GetComponent.<P1_Data>();
P2 = GameObject.Find("GameManager").GetComponent.<P2_Data>();
}
Later, i want to access these scripts using the currentPlayer variable to dynamically adjust the path:
var currentPlayer : String = "P1"; //this actually happens along with some other stuff in the SwitchPlayers function, i just put it here for better understanding
if (currentPlayer.PlayerEnergy >= value){
// do stuff
}
As i was afraid, i got an error saying, that PlayerEnergy was not a part of UnityEngine.String.
So how do I get unity to read "currentPlayer" as part of the variable path?
Maybe some parse function I haven't found?
Or am I going down an entirely wrong road here?
Cheers
PS: I also tried putting the P1 and P2 variables into an array and access them like this:
if (PlayerData[CurrentPlayerInt].PlayerEnergy >= value){
// do stuff
}
to no success.
First of all,
var currentPlayer : String = "P1"
here P1 is just string, not the previous P1/P2 which are referenced to two scripts. So, if you want, you can change
currentPlayer.PlayerEnergy >= value
to
P1.PlayerEnergy >= value
or,
P2.PlayerEnergy >= value
But if you just want one function for them, like
currentPlayer.PlayerEnergy >= value
Then you have to first set currentPlayer to P1/P2 which I assume you are trying to do. You must have some codes that can verify which player is selected. Then, maybe this can help -
var playerSelected: int = 0;
var currentPlayerEnergy: int = 0;
.....
//Use your codes to verify which player is selected and then,
if (playerSelected == 1) {
currentPlayerEnergy = P1.PlayerEnergy;
} else if (playerSelected == 2) {
currentPlayerEnergy = P2.PlayerEnergy;
}
//Now use your favorite function
if (currentPlayerEnergy >= value) {
//Do stuff
}
As there was no reply providing the answer I needed, I'll share the solution that did the trick for me, provided by a fellow student.
Instead of having the PlayerData scripts pre-written, I generate them using a public class function in a Playermanager script. This generates the Playerdata as attached scripts, saved into an array.
I can then access them through Playermanager.Playerlist[Playernumber].targetvariable.
Which is what I wanted to do, only with the Playerdata being attached to a script instead of a gameobject. And it works great!
Here's the full code of my Playermanager Script:
//initialise max players
public var maxplayers : int = 2;
// Initialise Playerlist
static var Players = new List.<PlayerData>();
function Start () {
for (var i : int = 0; i < maxplayers; i++){
var Player = new PlayerData();
Players.Add(Player);
Players[i].PlayerName = "Player " + i;
}
DontDestroyOnLoad (transform.gameObject);
}
public class PlayerData {
public var PlayerName : String;
public var PlayerEnergy : int = 15;
public var Fleet : List.<GameObject> = new List.<GameObject>();
}
As you see, you can put any type of variable in this class.
I hope this helps some of you who have the same problem.
cheers,
Tux
I'm working with Actionscript 2 (not ready to upgrade yet, although it's irreverent to the problem) but I'm having trouble with OOP and classes.
I've got a "Tool" class, written like so:
class com.Tool {
public var self:MovieClip;
private static var Type:String;
function Tool(T:String, X:Number, Y:Number) {
Type = T;
self = _root.createEmptyMovieClip("obj"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
self._x = X;
self._y = Y;
self.width = 36;
self.height = 36;
self.onRollOver = function() {
trace(Type);
}
}
}
I create 3 of them in the main script like so:
var toolPan:Tool = new Tool("pan", 0, 0);
var toolSquare:Tool = new Tool("square", 0, 38);
var toolLine:Tool = new Tool("line", 0, 76);
It all works great, except the onRollOver. It's supposed to output the unique "Type" string, but it always outputs "line" (the last Type Tool created) regardless which one I roll over.
Needless to say, I'm still a beginner to all this. But it seems like they're all sharing the same variable :/ How do I make these variables unique to each object created?
Thank you very much!
It's because it's type static, so the value is shared by all instances of that class. Remove it and it should work.
private var Type:String;