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

Very Simple Little Beginners Question

New Here ,
Jan 03, 2019 Jan 03, 2019

Copy link to clipboard

Copied

Hi. If I have two files about a game, a main flash file and a monster class "class" file, can I have the monster class code interact with the main flash file's properties by way of typeing in the line.

(this.parent as MainFileClass).characterHP -= 10

or

attackProcess((this.parent as MainFileClass.character),

the way I would the opposite direction using (enemyMonsterBox[0] as Monster).monsterType= "ice".

TOPICS
ActionScript

Views

233

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 , Jan 03, 2019 Jan 03, 2019

Hi.

You have to instantiate your characters first. Assuming that your class is in the same folder as the FLA:

var hero:Hero = new Hero();

hero.hp -= 10;

addChild(hero);

OR

var hero:Hero = new Hero();

addChild(hero);

var monster:Monster = new Monster();

monster.type = "ice";

addChild(monster);

monster.attack(hero); // hero loses 10 HP points

And, yeah, if you have an array holding references to monster instances, you would have to do like you did here:

(enemyMonsterBox[0] as Monster).type= "ice";

Please tell u

...

Votes

Translate

Translate
Community Expert ,
Jan 03, 2019 Jan 03, 2019

Copy link to clipboard

Copied

Hi.

You have to instantiate your characters first. Assuming that your class is in the same folder as the FLA:

var hero:Hero = new Hero();

hero.hp -= 10;

addChild(hero);

OR

var hero:Hero = new Hero();

addChild(hero);

var monster:Monster = new Monster();

monster.type = "ice";

addChild(monster);

monster.attack(hero); // hero loses 10 HP points

And, yeah, if you have an array holding references to monster instances, you would have to do like you did here:

(enemyMonsterBox[0] as Monster).type= "ice";

Please tell us if this is what you wanted to know.

Regards,

JC

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
New Here ,
Jan 03, 2019 Jan 03, 2019

Copy link to clipboard

Copied

What I wanted to know is if you could put in the Monster Class code something to interact with the character thats located as a variable in the main class code. The answer is either, yes, you can do it but you would do it by referring to "this"'s "parent" as mainflashcode class, or no flash does not allow. get me? As in I want the Monster class to handle a portion of coding calculations for hit penalties and stuff, and I want the monster class code to come "come get" the variable for the characters hp from the main class code, the main flash

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
Community Expert ,
Jan 03, 2019 Jan 03, 2019

Copy link to clipboard

Copied

LATEST

You don't have to use this.parent because your Main class becomes the root.

I have an example here.

[Main.as // document class]

package

{

    import flash.display.MovieClip;

    public class Main extends MovieClip

    {

          public var hero:Hero;

          public function Main()

          {

              hero = new Hero();

              addChild(hero);

          }

    }

}

[Hero.as]

package

{

    import flash.display.MovieClip;

    public class Hero extends MovieClip

    {

          public var hp:int = 0;

          public function Hero()

          {

          }

    }

}

[Monster.as]

package

{

    import flash.display.MovieClip;

    public class Monster extends MovieClip

    {

          public var attackForce:uint = 10;

          public function Monster()

          {

          }

          public function attack(target:Hero):void

          {

              target.hp -= attackForce;

              trace("Monster attack! Hero's HP is now " + target.hp + ".");

          }

    }

}

[FLA code in the first frame of the main timeline]

var monster:Monster = new Monster();

addChild(monster);

monster.attack(hero);

I hope this helps clarifying things.

Regards,

JC

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