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

41

Likes

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

Adobe Community Professional , 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]); 

Likes

Translate

Translate
Adobe Community Professional ,
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]); 

Likes

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