View Full Version : [As3] Variable name from another variable
Broly
12-26-2006, 10:24 AM
Hi all,
I'm new in this forum :)
I've a problem in the Actionscript 3 Flash 9 Alpha.
I'm trying to give to a variable a name taken from another variable, for example
a = "pippo"
this[a] = 10
trace(pippo)
and this
b=1
this["pippo"+b] = 10
trace(pippo1)
works in Actionscript 2...but it doesn't work in Actionscript 3.
I tried in many ways but it doesn't work :(
Any help will be appreciated :)
Thanks!
TheCanadian
12-26-2006, 11:56 AM
var a = "pippo"
this[a] = 10
trace(this.pippo)
var b=1
this["pippo"+b] = 10
trace(this.pippo1)
Broly
12-26-2006, 12:05 PM
ActionScript Code:
var a = "pippo"
this[a] = 10
trace(this.pippo)
var b=1
this["pippo"+b] = 10
trace(this.pippo1)
Oh My God...
After your post, I made two considerations/conclusions.
1) I need holidays
2) I've to kill myself
Now I go to make my choice between two options...
Thanks The Canadian...and excuse for the stupid question :look:
TheCanadian
12-26-2006, 12:09 PM
:lol: No problem.
Tigerfisk
04-04-2009, 09:53 AM
Hello, sorry for this big buuump. But i have a following questions to what was written before.
I want to make a variable from another variable.
Example
var number:int= new int(1);
this["enemy"+number]; <--- (is this needed?)
var ["enemy" +number]:MovieClip = new MovieClip(); <-- i know this is wrong.
I want to make variables like enemy1, enemy2, enemy3...
But I have no idea what I am doing wrong. Can this be solved?
Thanks in advance.
// Olof
Valaran
04-04-2009, 11:00 AM
You cannot create single variable names from another variable straight off the bat, the thing you can do, like shown before, is add a variable to a dynamic display object, like the MovieClip, an object, like the following:
// Example one:
var enemies:Object = {};
var num:int = 0;
var str:String = "enemy" + num;
var clip:MovieClip = new MovieClip();
enemies[str] = clip;
// Example two, you could skip the entire string and use an array:
var enemies:Array = [];
var clip:MovieClip = new MovieClip();
enemies.push(clip);
Tigerfisk
04-04-2009, 12:09 PM
Hello. i think the example 2 is better in this case. But i cant get it really to work.
I am to learn flash and make a "eskiv" clone.
If you dont know what eskiv is, Play it here. http://www.arcadecabin.com/play/eskiv.html
I show parts of the code so you can understand easily.
(Swedish to English translate)
Fiende = enemy
Fiender = enemies
this is in the top of the code.
var uppellerner:Array=new Array();
var fiender:Array=new Array();
var fiende:MovieClip = new MovieClip();
fiende.graphics.beginFill(0x8B1A0E);
fiende.graphics.drawCircle(70,60,6);
fiende.graphics.endFill();
fiende.x = 80;
fiende.y = 60;
fiender.push(fiende); // adds fiende to an array. But dont know what to do with it
uppellerner.push("true");
addChild(fiende);
This works on the firstball i add to the game. But on ball 2 the movieclip needs another name.
And this is in the everyframe function
while (i < uppellerner.length) {
whiletrue = uppellerner[i];
if (whiletrue == "true") {
fiende.y-=datorfart;
}
if ( whiletrue == "false") {
fiende.y+=datorfart;
}
if (fiende.hitTestObject(topp) ) {
whiletrue = "false";
}
if (fiende.hitTestObject(bottom) ) {
whiletrue = "true";
}
uppellerner[i] = whiletrue;
i++;
}
}
Same here, The first ball moves perferct.
Full fla file http://oloflolof.se/arkiv/trendo1314.rar
I hope someone there is someone who can help me.
Thanks in advance
// Olof
Tigerfisk
04-04-2009, 12:11 PM
I have tried a little more with arrays now.
This code creates 2 balls and put in array. But the problem is still that both have the same movieclip name.
And the "while" as you see further down, just a ball and coordinates the ball two would have.
var fiende:MovieClip = new MovieClip();
//create ball 1
fiende.graphics.beginFill(0x8B1A0E);
fiende.graphics.drawCircle(70,60,6);
fiende.graphics.endFill();
fiende.x = 80;
fiende.y = 60;
fiender.push(fiende);
uppellerner.push("true");
//create ball 2
fiende.graphics.beginFill(0x8B1A0E);
fiende.graphics.drawCircle(70,60,6);
fiende.graphics.endFill();
fiende.x = 180;
fiende.y = 60;
fiender.push(fiende);
uppellerner.push("true");
var o:int = new int(0);
while( o < fiender.length){
addChild(fiender[o]);
o++
}
Valaran
04-04-2009, 02:47 PM
Looopy loopy! Instead of doing it all manually, since we are already using arrays, you should just loop through it all, I haven't tested this, but I think it should run :P
// Adding everything
var fiender:Array = [];
var totalLen:int = 20;
for(var i:int = 0; i < totalLen; i++)
{
// At this time, I'm not sure what your "uppellerner" array holds, but I'm gonna guess it
//decides if the enemy is moving up or down, so I'm gonna show you one of many better
//ways to deal with it:
var fiende:MovieClip = new MovieClip();
fiende.x = 80 + i * 100;
fiende.y = 60;
var obj:Object = { clip:fiende, direction:true };
fiender.push(fiende);
}
// Run on every frame
var enemyLen:int = fiender.length;
for(var i:int = 0; i < enemyLen; i++)
{
var check:Boolean = fiender[i].direction;
// Ternary statement, the same as an IF, can only have 1 statement if evaluated true and one if false
// (check)? is the same as if(check){
// and the : (semicolons) are the same as } else {
fiender[i].y = (check)? fiender[i].y - datorfart : fiender[i].x + datorfart;
if(fiender[i].hitTestObject(topp))
{
check = true;
}
else
{
// Only need to check this if the above one returns false
// The object can't be at two points at the same time after all!
if(fiender[i].hitTestObject(bottom)) check = false;
}
fiender[i].direction = check;
}
Tigerfisk
04-04-2009, 04:58 PM
Hi again Valaran.
I am really glad that someone helps me and appreciatte everything you do for me.
I added this in the beginning so i could see the balls.
fiende.graphics.beginFill(0x8B1A0E);
fiende.graphics.drawCircle(70,60,6);
fiende.graphics.endFill();
And edited this so the ball was moving
fiender[i].y = (check)? fiender[i].y -=datorfart : fiender[i].x +=datorfart;
But now i got more problems...
This is how the script works now:
Adds a number of balls.
But they do not roll straight down without a little wrong.
And they do not bounce back against the wall.
I think it is something in this code.
fiender[i].y = (check)? fiender[i].y -=datorfart : fiender[i].x +=datorfart;
Live demo: http://oloflolof.se/arkiv/trendobollarullarinterakt.swf
Thanks in advance
// Olof
Tigerfisk
04-04-2009, 05:14 PM
edit:
I changed the if to the orginell way and now is it working. :P
if(fiender[i].direction == true){
fiender[i].y += datorfart;
}
else if(fiender[i].direction == false){
fiender[i].y -= datorfart;
}
Will test it more now and i will write here if its works or not when i am done. :)
Valaran
04-04-2009, 11:52 PM
If you hadn't changed my ternary statement, it would had worked just like the if-statement..
Tigerfisk
04-05-2009, 02:24 AM
If you hadn't changed my ternary statement, it would had worked just like the if-statement..
You mean this code?
fiender[i].y = (check)? fiender[i].y - datorfart : fiender[i].x + datorfart;
The ball was not moving with that code.
Valaran
04-05-2009, 10:45 AM
That is very strange, since:
fiender[i].y -= datorfart;
// is exactly the same as
fiender[i].y = fiender[i].y - datorfart;
// So this:
fiender[i].y = fiender[i].y -= datorfart
// is basically
fiender[i].y = fiender[i].y = fiender[i].y - datorfart;
Makes no sense at all, well nevermind, use whatever works for you.
Tigerfisk
04-05-2009, 03:27 PM
Yeah i know, Its weird.
Anyway, i have a new problen now.
if the blueball (player) hits an enemie i want all enemies to disapear.
Code
var enemyLen:int = fiender.length;
// the first for to loop out all enemies
for (var i:int = 0; i < enemyLen; i++) {
var ledboolean:Boolean = fiender[i].direction;
addChild(fiender[i]);
if (ball.hitTestObject(fiender[i])) {
lost=1;
// second for to loop out all enemies again to remove them.
for (var o:int = 0; o < enemyLen; o++) {
removeChild(fiender[o]);
}
... the enemieball movement code.
}
But the problem is...
Pretend that there are two enemy balls in the game...
If you hit ball1, only ball1 disappears. But if i hit ball2 bothdisappears.
Weird.
The reason I want to do this is when the player will restart a game when the player lost.
Otherwise will the red balls remain in the game.
Thanks!
Olof
(This issue is a bit offtopic than what I ask before, you may think I should start a new thread? )
EDIT:
Yay! Moved the for to a function outside the everyframe function and now is's working!
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.