Skip to main content
alexr58234163
Known Participant
January 3, 2019
Answered

Very Simple Little Beginners Question

  • January 3, 2019
  • 1 reply
  • 365 views

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".

This topic has been closed for replies.
Correct answer JoãoCésar17023019

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

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
January 3, 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 us if this is what you wanted to know.

Regards,

JC

alexr58234163
Known Participant
January 3, 2019

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

JoãoCésar17023019
Community Expert
Community Expert
January 3, 2019

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