Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to assign new object to class?

Explorer ,
Jan 30, 2015 Jan 30, 2015

So I know how to do this:

var classexample:objectexample = new objectexample();

But I'm attempting to make a game where you can select a character and that selected character's object assigns to the class, but I don't know how to get it to change. As far as I know I can only assign it once and then that's it, I dunno how to change it. Is there a way how?

Example, I know this doesn't work, but I'd like to do something like this:

if(character=="example1"){

var classexample:objectexample1 = new objectexample1();

}

if(character=="example2"){

var classexample:objectexample2 = new objectexample2();

}

Any ideas?

TOPICS
ActionScript
346
Translate
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

Explorer , Jan 31, 2015 Jan 31, 2015

I got it to work!

All I had to do was set up the initial var for the object, so:

var p1;

Then, if the variable changed in the character selection, all I had to do was:

if(p1_character=="character1"){

p1 = new character1();

}

Bam!

Translate
Community Expert ,
Jan 30, 2015 Jan 30, 2015

for different objects (eg, characters) that will share common characteristics/code (eg, a player class) you would assign each a different (eg, character1, character2 etc) class but have each extend the same super (eg, player) class.

Translate
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
Explorer ,
Jan 30, 2015 Jan 30, 2015

Alright, makes sense. However, is there a way I can change the instance names of these classes for AS3 to access them? Because in each character's function script, they use the same identifier name (like even though the characters are different, all the characters for player 1 will still use "p1" and for player 2 "p2"), it makes it easier for them to interact with each other.

This is what I use to set up the characters manually:

var p1:character1 = new character1();

"character1" being the class of the MC. I want to make a script that, when a different character is selected, I can change the class to character2, character3 etc, how would I be able to change that class for "p1"? (Sorry if I'm getting classes and objects confused, I think I am)

Translate
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
Community Expert ,
Jan 30, 2015 Jan 30, 2015

p1 can be assigned to any character (or anything else) class.  you can also coerce strings into classes using getDefinitionByName() function.

eg, if you have something like a combobox where users select a character, you can use a string for the class name and assign something like:

var charClass:String = combobox.selectedItem.label;

var p1:*=Class(getDefinitionByName(charClass));

Translate
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
Explorer ,
Jan 30, 2015 Jan 30, 2015

Looks interesting, I could try something with that.

The current method I have though is when player 1 selects a character, it changes a "p1_character" variable to the character's name, example: p1_character = "character1";

Now if player 1's character is "character1", is there a way I can assign the character1 class to the p1 object with that condition? And then the same thing for character2, character3 and so forth.

Translate
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
Explorer ,
Jan 31, 2015 Jan 31, 2015
LATEST

I got it to work!

All I had to do was set up the initial var for the object, so:

var p1;

Then, if the variable changed in the character selection, all I had to do was:

if(p1_character=="character1"){

p1 = new character1();

}

Bam!

Translate
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