• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Fetching object from name

Explorer ,
Apr 19, 2022 Apr 19, 2022

Copy link to clipboard

Copied

so my goal is to fetch my object from just a string, I want something like this 

	var gems:int = 1;
	
	for(var i = 1; i <= gems; i++) {
		var name:String = "gem"+i; //ex: "gem2"
		var gemOBJ:Object = name;
		if (background.gemOBJ.hitTestObject(Rose)) {
			background.gemOBJ.visible = false;
			background.gemOBJ.y = 1500;
			background.gemOBJ.gotoAndStop(7);
		}
		
	}

 

if that makes sense

TOPICS
ActionScript , Code , How to

Views

87

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 20, 2022 Apr 20, 2022

if there are object instances named "gem"+i, use bracket notation to coerce the string to an object:

 

var name:String="gem"+i;

var gemObj:DisplayObject = DisplayObject(background[name]); // assuming background are the gems' parent

 

and because your gems are movieclips, you can be more specific:

 

var gem_mc:MovieClip = MovieClip(background[name]); 

Votes

Translate

Translate
Community Expert ,
Apr 20, 2022 Apr 20, 2022

Copy link to clipboard

Copied

LATEST

if there are object instances named "gem"+i, use bracket notation to coerce the string to an object:

 

var name:String="gem"+i;

var gemObj:DisplayObject = DisplayObject(background[name]); // assuming background are the gems' parent

 

and because your gems are movieclips, you can be more specific:

 

var gem_mc:MovieClip = MovieClip(background[name]); 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines